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

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

int field[50][50];
bool visit[50][50][50][50];

const int dx[] = {+1, 0, -1, 0};
const int dy[] = {0, +1, 0, -1};

int main() {
  int w, h, ax, ay, bx, by;

  while (cin >> w >> h, w || h) {
    cin >> ax >> ay;
    cin >> bx >> by;

    for (int y = 0; y < h; y++)
      for (int x = 0; x < w; x++) {
        cin >> field[y][x];
      }
    memset(visit, 0, sizeof(visit));

    int cnt = 0;
    bool flag = false;
    queue<int> q;
    q.push(ax - 1);
    q.push(ay - 1);
    q.push(bx - 1);
    q.push(by - 1);
    while (!q.empty()) {
      int len = q.size() / 4;
      for (int l = 0; l < len; l++) {
        ax = q.front();
        q.pop();
        ay = q.front();
        q.pop();
        bx = q.front();
        q.pop();
        by = q.front();
        q.pop();

        if (ax == bx && ay == by) {
          flag = true;
          break;
        }

        if (visit[ax][ay][bx][by]) {
          continue;
        }
        visit[ax][ay][bx][by] = 1;

        for (int d = 0; d < 4; d++) {
          int px = ax + dx[d];
          int py = ay + dy[d];
          int qx = bx - dx[d];
          int qy = by - dy[d];

          if (px < 0 || w <= px || py < 0 || h <= py) {
            px = ax;
            py = ay;
          }
          if (qx < 0 || w <= qx || qy < 0 || h <= qy) {
            qx = bx;
            qy = by;
          }
          if (field[py][px]) {
            px = ax;
            py = ay;
          }
          if (field[qy][qx]) {
            qx = bx;
            qy = by;
          }

          if (!visit[px][py][qx][qy]) {
            q.push(px);
            q.push(py);
            q.push(qx);
            q.push(qy);
          }
        }
      }

      if (flag)
        break;
      if (++cnt >= 100)
        break;
    }

    if (flag) {
      cout << cnt << endl;
    } else {
      cout << "NA" << endl;
    }
  }
}

Error: TLE

Fixed submission:

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

int field[50][50];
bool visit[50][50][50][50];

const int dx[] = {+1, 0, -1, 0};
const int dy[] = {0, +1, 0, -1};

int main() {
  int w, h, ax, ay, bx, by;

  while (cin >> w >> h, w || h) {
    cin >> ax >> ay;
    cin >> bx >> by;

    for (int y = 0; y < h; y++)
      for (int x = 0; x < w; x++) {
        cin >> field[y][x];
      }
    memset(visit, 0, sizeof(visit));

    int cnt = 0;
    bool flag = false;
    queue<int> q;
    q.push(ax - 1);
    q.push(ay - 1);
    q.push(bx - 1);
    q.push(by - 1);
    visit[ax][ay][bx][by] = 1;

    while (!q.empty()) {
      int len = q.size() / 4;
      for (int l = 0; l < len; l++) {
        ax = q.front();
        q.pop();
        ay = q.front();
        q.pop();
        bx = q.front();
        q.pop();
        by = q.front();
        q.pop();

        if (ax == bx && ay == by) {
          flag = true;
          break;
        }

        for (int d = 0; d < 4; d++) {
          int px = ax + dx[d];
          int py = ay + dy[d];
          int qx = bx - dx[d];
          int qy = by - dy[d];

          if (px < 0 || w <= px || py < 0 || h <= py) {
            px = ax;
            py = ay;
          }
          if (qx < 0 || w <= qx || qy < 0 || h <= qy) {
            qx = bx;
            qy = by;
          }
          if (field[py][px]) {
            px = ax;
            py = ay;
          }
          if (field[qy][qx]) {
            qx = bx;
            qy = by;
          }

          if (!visit[px][py][qx][qy]) {
            q.push(px);
            q.push(py);
            q.push(qx);
            q.push(qy);
            visit[ax][ay][bx][by] = 1;
          }
        }
      }

      if (flag)
        break;
      if (++cnt >= 100)
        break;
    }

    if (flag) {
      cout << cnt << endl;
    } else {
      cout << "NA" << endl;
    }
  }
}