Problem p00223 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

bool went[50 + 10][50 + 10][50 + 10][50 + 10] = {0};

int map[50 + 10][50 + 10] /* = { 0 }*/;

int gx[] = {1, 0, -1, 0, -1, 0, 1, 0};
int gy[] = {0, 1, 0, -1, 0, -1, 0, 1};

struct ST {
  int tx, ty;
  int kx, ky;
  int tim = 0;
};

bool ChekNow(ST);

ST GoMap(ST, int);

void test(ST ex, int h, int w) {
  int i, j;
  cout << endl;
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      if ((i == ex.ky && j == ex.kx) || (i == ex.ty && j == ex.tx))
        cout << "*";
      else
        cout << map[i][j];
    }
    cout << endl;
  }
  cout << ex.tim << endl;
}

int main() {
  ST now, tmp;

  queue<ST> nx;

  int w, h;
  int tsx, tsy;
  int ksx, ksy;

  bool flg;
  int i, j;

  while (cin >> w >> h, w != 0 && h != 0) {
    flg = true;

    cin >> tsx >> tsy;
    cin >> ksx >> ksy;

    for (i = 1; i <= h; i++) {
      map[i][0] = map[i][w + 1] = 1;

      for (j = 1; j <= w; j++) {
        cin >> map[i][j];
      }
    }

    for (i = 0; i <= w + 1; i++) {
      map[0][i] = 1;
      map[h + 1][i] = 1;
    }

    now.tx = tsx;
    now.ty = tsy;
    now.kx = ksx;
    now.ky = ksy;
    now.tim = 0;

    nx.push(now);

    while (!nx.empty()) // && now.tim<100)
    {
      now = nx.front();
      nx.pop();

      if (now.tim >= 100) {
        break;
      }

      // if (ChekNow(now))
      //{
      //	continue;
      // }
      // went[now.tx][now.ty][now.kx][now.ky] = true;

      // test(now, h, w);

      if (now.tx == now.kx && now.ty == now.ky) {
        cout << now.tim << endl;

        flg = false;

        break;
      }

      for (i = 0; i < 4; i++) {
        // tmp=GoMap(now, i);

        tmp = now;

        if (map[now.ty + gy[i]][now.tx + gx[i]] == 0) {
          tmp.ty += gy[i];
          tmp.tx += gx[i];
        }
        if (map[now.ky + gy[i + 4]][now.kx + gx[i + 4]] == 0) {
          tmp.ky += gy[i + 4];
          tmp.kx += gx[i + 4];
        }

        if (!ChekNow(tmp)) {
          tmp.tim++;

          went[tmp.tx][tmp.ty][tmp.kx][tmp.ky] = true;

          nx.push(tmp);
        }
      }
    }

    if (flg) {
      cout << "NA\\n";
    }

    while (!nx.empty()) {
      nx.pop();
    }

    memset(went, 0, sizeof(went));
  }
  return 0;
}

bool ChekNow(ST ex) { return went[ex.tx][ex.ty][ex.kx][ex.ky]; }

ST GoMap(ST ex, int i) {
  if (map[ex.ty + gy[i]][ex.tx + gx[i]] == 0) {
    ex.ty += gy[i];
    ex.tx += gx[i];
  }
  if (map[ex.ky + gy[i + 4]][ex.kx + gx[i + 4]] == 0) {
    ex.ky += gy[i + 4];
    ex.kx += gx[i + 4];
  }

  return ex;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

bool went[50 + 10][50 + 10][50 + 10][50 + 10] = {0};

int map[50 + 10][50 + 10] = {0};

int gx[] = {1, 0, -1, 0, -1, 0, 1, 0};
int gy[] = {0, 1, 0, -1, 0, -1, 0, 1};

struct ST {
  int tx, ty;
  int kx, ky;
  int tim = 0;
};

bool ChekNow(ST);

ST GoMap(ST, int);

void test(ST ex, int h, int w) {
  int i, j;
  cout << endl;
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      if ((i == ex.ky && j == ex.kx) || (i == ex.ty && j == ex.tx))
        cout << "*";
      else
        cout << map[i][j];
    }
    cout << endl;
  }
  cout << ex.tim << endl;
}

int main() {
  ST now, tmp;

  queue<ST> nx;

  int w, h;
  int tsx, tsy;
  int ksx, ksy;

  bool flg;
  int i, j;

  while (cin >> w >> h, w != 0 && h != 0) {
    flg = true;

    cin >> tsx >> tsy;
    cin >> ksx >> ksy;

    for (i = 1; i <= h; i++) {
      map[i][0] = map[i][w + 1] = 1;

      for (j = 1; j <= w; j++) {
        cin >> map[i][j];
      }
    }

    for (i = 0; i <= w + 1; i++) {
      map[0][i] = 1;
      map[h + 1][i] = 1;
    }

    now.tx = tsx;
    now.ty = tsy;
    now.kx = ksx;
    now.ky = ksy;
    now.tim = 0;

    nx.push(now);

    while (!nx.empty() && tmp.tim < 100) {
      now = nx.front();
      nx.pop();

      // if (ChekNow(now))
      //{
      //	continue;
      // }
      // went[now.tx][now.ty][now.kx][now.ky] = true;

      test(now, h, w);

      if (now.tx == now.kx && now.ty == now.ky) {
        cout << now.tim << endl;

        flg = false;

        break;
      }

      for (i = 0; i < 4; i++) {
        tmp = GoMap(now, i);

        if (!ChekNow(tmp)) {
          tmp.tim++;

          went[tmp.tx][tmp.ty][tmp.kx][tmp.ky] = true;

          nx.push(tmp);
        }
      }
    }

    if (flg) {
      cout << "NA\\n";
    }

    while (!nx.empty()) {
      nx.pop();
    }

    memset(went, 0, sizeof(went));
  }
  return 0;
}

bool ChekNow(ST ex) { return went[ex.tx][ex.ty][ex.kx][ex.ky]; }

ST GoMap(ST ex, int i) {
  if (map[ex.ty + gy[i]][ex.tx + gx[i]] == 0) {
    ex.ty += gy[i];
    ex.tx += gx[i];
  }
  if (map[ex.ky + gy[i + 4]][ex.kx + gx[i + 4]] == 0) {
    ex.ky += gy[i + 4];
    ex.kx += gx[i + 4];
  }

  return ex;
}