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

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
using namespace std;

string masu[1010];
int intmasu[1010][1010];
int h, w, ch, cw, dh, dw;
int ans = -1;

struct st {
  int hh;
  int ww;
  int wnum;
  st(int a, int b, int c) {
    hh = a;
    ww = b;
    wnum = c;
  }
};

queue<st> solve;
int warpnum = -1;
int warpcount;

void move(int hh, int ww, int wnum) {
  if (hh <= 0 || ww <= 0 || hh > h || ww > w) {
    return;
  }
  if (masu[hh][ww] != '.') {
    return;
  }
  if (intmasu[hh][ww] == wnum) {
    return;
  }
  //	masu[hh][ww] = wnum + '0';
  intmasu[hh][ww] = wnum;
  solve.push(st(hh, ww, wnum));
  move(hh - 1, ww, wnum);
  move(hh + 1, ww, wnum);
  move(hh, ww - 1, wnum);
  move(hh, ww + 1, wnum);
  if (hh == dh && ww == dw) {
    if (ans != -1) {
      ans = min(ans, wnum);
    } else {
      ans = wnum;
    }
    return;
  }
  return;
}

void warp(int hh, int ww, int wnum) {
  int i, j;
  if (hh <= 0 || ww <= 0 || hh > h || ww > w) {
    return;
  }
  if (masu[hh][ww] != '.')
    return;
  if (wnum == warpnum) {
    for (i = hh - 2; i <= hh + 2; i++) {
      for (j = ww - 2; j <= ww + 2; j++) {
        warp(i, j, wnum + 1);
      }
    }
  } else if (wnum == warpnum + 1) {
    if (intmasu[hh][ww] == -1) {
      intmasu[hh][ww] = wnum - 1;
      solve.push(st(hh, ww, wnum));
      warpcount++;
      if (hh == dh && ww == dw) {
        if (ans != -1) {
          ans = min(ans, wnum);
        } else {
          ans = wnum;
        }
        return;
      }
    }
  }
  return;
}

void printmasu() {
  int i, j;
  cout << endl;
  cout << "warpcount = " << warpcount << endl;
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      cout << masu[i][j];
    }
    cout << endl;
  }
  cout << endl;
}

void initflg() {
  int i, j;
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      if (intmasu[i][j] == warpnum + 1) {
        intmasu[i][j] = -1;
      }
    }
  }
}

int main() {
  int i, j, k;

  cin >> h >> w;
  cin >> ch >> cw;
  cin >> dh >> dw;
  for (i = 1; i <= h; i++) {
    cin >> masu[i];
    masu[i] = '#' + masu[i];
  }

  for (i = 0; i < 1010; i++) {
    for (j = 0; j < 1010; j++) {
      intmasu[i][j] = -1;
    }
  }

  move(ch, cw, 0);
  if (ans != -1) {
    cout << ans << endl;
    return 0;
  }

  st tmp(0, 0, 0);
  while (1) {
    warpnum++;
    warpcount = 0;
    while (!solve.empty()) {
      tmp = solve.front();
      if (tmp.wnum != warpnum)
        break;
      solve.pop();
      warp(tmp.hh, tmp.ww, tmp.wnum);
    }
    // printmasu();
    // initflg();
    if (solve.empty())
      break;

    while (warpcount > 0) {
      tmp = solve.front();
      solve.pop();
      move(tmp.hh, tmp.ww, tmp.wnum);
      warpcount--;
    }
    // printmasu();

    if (ans != -1)
      break;
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
using namespace std;

string masu[1010];
int intmasu[1010][1010];
int h, w, ch, cw, dh, dw;
int ans = -1;

struct st {
  int hh;
  int ww;
  int wnum;
  st(int a, int b, int c) {
    hh = a;
    ww = b;
    wnum = c;
  }
};

queue<st> solve;
int warpnum = -1;
int warpcount;

void move(int hh, int ww, int wnum) {
  if (hh <= 0 || ww <= 0 || hh > h || ww > w) {
    return;
  }
  if (masu[hh][ww] != '.') {
    return;
  }
  if (intmasu[hh][ww] != -1) {
    return;
  }
  //	masu[hh][ww] = wnum + '0';
  intmasu[hh][ww] = wnum;
  solve.push(st(hh, ww, wnum));
  move(hh - 1, ww, wnum);
  move(hh + 1, ww, wnum);
  move(hh, ww - 1, wnum);
  move(hh, ww + 1, wnum);
  if (hh == dh && ww == dw) {
    if (ans != -1) {
      ans = min(ans, wnum);
    } else {
      ans = wnum;
    }
    return;
  }
  return;
}

void warp(int hh, int ww, int wnum) {
  int i, j;
  if (hh <= 0 || ww <= 0 || hh > h || ww > w) {
    return;
  }
  if (masu[hh][ww] != '.')
    return;
  if (wnum == warpnum) {
    for (i = hh - 2; i <= hh + 2; i++) {
      for (j = ww - 2; j <= ww + 2; j++) {
        warp(i, j, wnum + 1);
      }
    }
  } else if (wnum == warpnum + 1) {
    //		if (masu[hh][ww] == '.') {
    if (intmasu[hh][ww] == -1) {
      //			masu[hh][ww] = wnum + '0';
      intmasu[hh][ww] = wnum;
      solve.push(st(hh, ww, wnum));
      warpcount++;
      if (hh == dh && ww == dw) {
        if (ans != -1) {
          ans = min(ans, wnum);
        } else {
          ans = wnum;
        }
        return;
      }
    }
  }
  return;
}

void printmasu() {
  int i, j;
  cout << endl;
  cout << "warpcount = " << warpcount << endl;
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      cout << masu[i][j];
    }
    cout << endl;
  }
  cout << endl;
}

void initflg() {
  int i, j;
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      if (intmasu[i][j] == warpnum + 1) {
        intmasu[i][j] = -1;
      }
    }
  }
}

int main() {
  int i, j, k;

  cin >> h >> w;
  cin >> ch >> cw;
  cin >> dh >> dw;
  for (i = 1; i <= h; i++) {
    cin >> masu[i];
    masu[i] = '#' + masu[i];
  }

  for (i = 0; i < 1010; i++) {
    for (j = 0; j < 1010; j++) {
      intmasu[i][j] = -1;
    }
  }

  move(ch, cw, 0);
  if (ans != -1) {
    cout << ans << endl;
    return 0;
  }

  st tmp(0, 0, 0);
  while (1) {
    warpnum++;
    warpcount = 0;
    while (!solve.empty()) {
      tmp = solve.front();
      if (tmp.wnum != warpnum)
        break;
      solve.pop();
      warp(tmp.hh, tmp.ww, tmp.wnum);
    }
    // printmasu();
    initflg();
    if (solve.empty())
      break;

    while (warpcount > 0) {
      tmp = solve.front();
      solve.pop();
      move(tmp.hh, tmp.ww, tmp.wnum);
      warpcount--;
    }
    // printmasu();

    if (ans != -1)
      break;
  }

  cout << ans << endl;

  return 0;
}