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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>

using namespace std;

const int MAX = 50;
const int INF = (1 << 25);

bool come[MAX][MAX][MAX][MAX];
int M[MAX][MAX];
int w, h, stx, sty, skx, sky;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
struct data {
  int t, tx, ty, kx, ky;
  data(int t, int tx, int ty, int kx, int ky)
      : t(t), tx(tx), ty(ty), kx(kx), ky(ky) {}
};
queue<data> Q;

void init() {
  // fill(come[0][0][0],come[0][0][0]+MAX*MAX*MAX*MAX,INF);
  while (!Q.empty())
    Q.pop();
  memset(come, false, sizeof(come));
}

void input() {
  cin >> stx >> sty >> skx >> sky;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> M[i][j];
}

bool isMovable(int x, int y) {
  return 0 <= x && x < w && 0 <= y && y < h && M[y][x] == 0;
}

int bfs() {

  Q.push(data(0, stx - 1, sty - 1, skx - 1, sky - 1));

  while (!Q.empty()) {
    data now = Q.front();
    Q.pop();

    if (now.t > 100)
      return -1;
    if (now.tx == now.kx && now.ty == now.ky)
      return now.t;

    if (come[now.tx][now.ty][now.kx][now.ky])
      continue;
    come[now.tx][now.ty][now.kx][now.ky] = true;

    for (int i = 0; i < 4; i++) {
      data nex = now;
      nex.tx += dx[i];
      nex.ty += dy[i];
      nex.kx -= dx[i];
      nex.ky -= dy[i];
      nex.t++;

      if (!isMovable(nex.tx, nex.ty)) {
        nex.tx = now.tx;
        nex.ty = now.ty;
      }
      if (!isMovable(nex.kx, nex.ky)) {
        nex.kx = now.kx;
        nex.ky = now.ky;
      }
      Q.push(nex);
    }
  }
  return -1;
}

void solve() {
  // dfs(0,stx-1,sty-1,skx-1,sky-1);
  int ans = bfs();
  if (ans == -1)
    cout << "NA" << endl;
  else
    cout << ans << endl;
}

int main() {
  while (cin >> w >> h && w + h) {
    init();
    input();
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;

const int MAX = 50;
const int INF = (1 << 25);

int come[MAX][MAX][MAX][MAX];
int M[MAX][MAX];
int w, h, stx, sty, skx, sky;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
void init() {
  // fill(come[0][0][0],come[0][0][0]+MAX*MAX*MAX*MAX,INF);
  memset(come, -1, sizeof(come));
}

void input() {
  cin >> stx >> sty >> skx >> sky;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> M[i][j];
}

bool isMovable(int x, int y) {
  return 0 <= x && x < w && 0 <= y && y < h && M[y][x] == 0;
}
void dfs(int t, int tx, int ty, int kx, int ky) {

  if (t > 100 || come[tx][ty][kx][ky] <= t && come[tx][ty][kx][ky] != -1)
    return;
  come[tx][ty][kx][ky] = t;

  for (int i = 0; i < 4; i++) {
    int ntx = tx + dx[i];
    int nty = ty + dy[i];
    int nkx = kx - dx[i];
    int nky = ky - dy[i];

    int move = 0;
    if (!isMovable(ntx, nty)) {
      ntx = tx;
      nty = ty;
      move++;
    }
    if (!isMovable(nkx, nky)) {
      nkx = kx;
      nky = ky;
      move++;
    }
    if (move == 2)
      return;

    dfs(t + 1, ntx, nty, nkx, nky);
  }
}

void solve() {
  dfs(0, stx - 1, sty - 1, skx - 1, sky - 1);

  int ans = INF;
  for (int i = 0; i < w; i++)
    for (int j = 0; j < h; j++)
      if (come[i][j][i][j] != -1)
        ans = min(ans, come[i][j][i][j]);

  if (ans == INF)
    cout << "NA" << endl;
  else
    cout << ans << endl;
}

int main() {
  while (cin >> w >> h && w + h) {
    init();
    input();
    solve();
  }
  return 0;
}