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

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <string.h>
#include <vector>

using namespace std;

int h, w, sx, sy, gx, gy;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};

char ma[21][21];
int co[21][21];
bool visited[21][21];

int main() {
  while (cin >> h >> w, h) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        co[i][j] = INT_MAX;
        cin >> ma[i][j];
        if (ma[i][j] == 'A') {
          sx = j, sy = i;
        } else if (ma[i][j] == 'B') {
          gx = j, gy = i;
        }
      }
    }
    queue<pair<pair<int, int>, int>> q;
    q.push(make_pair(make_pair(sx, sy), 0));
    while (q.size()) {
      // cout<<q.size()<<endl;
      pair<pair<int, int>, int> p = q.front();
      q.pop();
      if (co[p.first.second][p.first.first] != INT_MAX) {
        continue;
      }
      // cout<<p.first.second<<" "<<p.first.first<<endl;
      co[p.first.second][p.first.first] = p.second;
      for (int i = 0; i < 4; i++) {
        int nx = p.first.first + dx[i];
        int ny = p.first.second + dy[i];
        if (nx >= 0 && nx < w && ny >= 0 & ny < h && ma[ny][nx] != '#' &&
            co[ny][nx] == INT_MAX) {
          q.push(make_pair(make_pair(nx, ny), p.second + 1));
        }
      }
      // cout<<q.size()<<endl;
    }
    string pat;
    cin >> pat;
    int go = 0;
    for (int j = 0;; j++) {
      int i = j % pat.length();
      //   cout<<gy<<" "<<gx<<" "<<co[gy][gx]<<" "<<INT_MAX<<endl;
      if (co[gy][gx] <= go) {
        cout << go << " " << gy << " " << gx << endl;
        break;
      }
      if (go > 100000) {
        cout << "impossible" << endl;
        break;
      }
      if (pat[i] == '5') {

      } else if (pat[i] == '8') {
        gy--;
      } else if (pat[i] == '6') {
        gx++;
      } else if (pat[i] == '4') {
        gx--;
      } else if (pat[i] == '2') {
        gy++;
      }
      gy = max(0, gy);
      gy = min(h - 1, gy);
      gx = max(0, gx);
      gx = min(w - 1, gx);
      go++;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <string.h>
#include <vector>

using namespace std;

int h, w, sx, sy, gx, gy;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};

char ma[21][21];
int co[21][21];
bool visited[21][21];

int main() {
  while (cin >> h >> w, h) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        co[i][j] = INT_MAX;
        cin >> ma[i][j];
        if (ma[i][j] == 'A') {
          sx = j, sy = i;
        } else if (ma[i][j] == 'B') {
          gx = j, gy = i;
        }
      }
    }
    queue<pair<pair<int, int>, int>> q;
    q.push(make_pair(make_pair(sx, sy), 0));
    while (q.size()) {
      pair<pair<int, int>, int> p = q.front();
      if (co[p.first.second][p.first.first] != INT_MAX) {
        continue;
      }
      co[p.first.second][p.first.first] = p.second;
      q.pop();
      for (int i = 0; i < 4; i++) {
        int nx = p.first.first + dx[i];
        int ny = p.first.second + dy[i];
        if (nx >= 0 && nx < w && ny >= 0 & ny < h && ma[ny][nx] != '#' &&
            co[ny][nx] == INT_MAX) {
          q.push(make_pair(make_pair(nx, ny), p.second + 1));
        }
      }
    }
    string pat;
    cin >> pat;
    //        int go=0;
    //        for(int j=0;;j++){
    //            int i=j%pat.length();
    //         //   cout<<gy<<" "<<gx<<" "<<co[gy][gx]<<" "<<INT_MAX<<endl;
    //            if(co[gy][gx]<=go){
    //                cout<<go<<" "<<gy<<" "<<gx<<endl;
    //                break;
    //            }
    //            if(go>1000){
    //                cout<<"impossible"<<endl;
    //                break;
    //            }
    //            if(pat[i]=='5'){
    //
    //            } else if(pat[i]=='8'){
    //                gy--;
    //            } else if(pat[i]=='6'){
    //                gx++;
    //            } else if(pat[i]=='4'){
    //                gx--;
    //            } else if(pat[i]=='2'){
    //                gy++;
    //            }
    //            gy=max(0,gy);
    //            gy=min(h-1,gy);
    //            gx=max(0,gx);
    //            gx=min(w-1,gx);
    //            go++;
    //        }
  }
}