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

// TDteam01
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int H, W;
char t[200][200];
int si, sj, ti, tj;
int d[200][200];
int di[] = {-1, 0, 1, 0};
int dj[] = {0, 1, 0, -1};

int main() {
  while (cin >> H >> W) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> t[i][j];
        if (t[i][j] == '@') {
          si = i;
          sj = j;
        }
        if (t[i][j] == '%') {
          ti = i;
          tj = j;
        }
      }
    }
    memset(d, 1, sizeof(d));
    d[ti][tj] = 0;
    queue<int> qi, qj;
    qi.push(ti);
    qj.push(tj);
    while (!qi.empty()) {
      int pi = qi.front();
      qi.pop();
      int pj = qj.front();
      qj.pop();
      for (int dir = 0; dir < 4; dir++) {
        int ni = pi + di[dir];
        int nj = pj + dj[dir];
        if (ni < 0 || nj < 0 || ni >= H || nj >= W)
          continue;
        if (t[ni][nj] == '#')
          continue;
        if (d[ni][nj] > d[pi][pj] + 1) {
          d[ni][nj] = d[pi][pj] + 1;
          qi.push(ni);
          qj.push(nj);
        }
      }
    }
    int dist = d[si][sj];
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++)
        if (t[i][j] == '$')
          if (d[i][j] <= dist)
            dist = 0;
    if (dist)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int n, H, W;
const int maxn = 1e4;
const int INF = 1e9;
int labyrinth[maxn][maxn], scnt;
int shortest;
int pshort, sshort, eshort, goal;
bool tf;

struct pt {
  int x, y;
} soldier[maxn], princess, escape;
char ch;
bool vis[maxn][maxn];

int dfs(int x, int y, int cur_length) {
  int temp;
  // printf("%d %d, H:%d W:%d\\n",x,y,H,W);
  // labyrinth[y][x] = 3;
  vis[y][x] = true;
  if (x > 0 && labyrinth[y][x - 1] && !vis[y][x - 1]) {
    if (labyrinth[y][x - 1] == goal) {
      return cur_length + 1;
    }
    if ((temp = dfs(x - 1, y, cur_length + 1)) < shortest) {
      shortest = temp;
    }
  }
  if (x < W - 1 && labyrinth[y][x + 1] && !vis[y][x + 1]) {
    if (labyrinth[y][x + 1] == 2) {
      return cur_length + 1;
    }
    if ((temp = dfs(x + 1, y, cur_length + 1)) < shortest) {
      shortest = temp;
    }
  }
  if (y > 0 && labyrinth[y - 1][x] && !vis[y - 1][x]) {
    if (labyrinth[y - 1][x] == 2) {
      return cur_length + 1;
    }
    if ((temp = dfs(x, y - 1, cur_length + 1)) < shortest) {
      shortest = temp;
    }
  }
  if (y < H - 1 && labyrinth[y + 1][x] && !vis[y + 1][x]) {
    if (labyrinth[y + 1][x] == 2) {
      return cur_length + 1;
    }
    if ((temp = dfs(x, y + 1, cur_length + 1)) < shortest) {
      shortest = temp;
    }
  }
  return shortest;
}

int main() {
  ios::sync_with_stdio(false);
  // freopen("1002.txt","r",stdin);
  // freopen("ans.txt","w+",stdout);
  while (~scanf("%d%d", &H, &W)) {
    tf = true;
    scnt = 0;
    shortest = INF;
    memset(labyrinth, -1, sizeof(labyrinth));
    getchar();
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        while ((ch = getchar()) == '\\n')
          ;
        switch (ch) {
        case '@':
          princess.x = j;
          princess.y = i, labyrinth[i][j] = 5;
          break;
        case '.':
          labyrinth[i][j] = 1;
          break;
        case '$':
          soldier[scnt].x = j;
          soldier[scnt++].y = i;
          labyrinth[i][j] = 1;
          break;
        case '%':
          labyrinth[i][j] = 2;
          escape.x = j;
          escape.y = i;
          break;
        case '#':
          labyrinth[i][j] = 0;
          break;
        }
      }
    }
    memset(vis, false, sizeof(vis));
    goal = 2;
    pshort = dfs(princess.x, princess.y, 0);
    // printf("pshort:%d\\n",pshort);

    goal = 5;
    shortest = INF;
    memset(vis, false, sizeof(vis));
    eshort = dfs(escape.x, escape.y, 0);
    // printf("eshort:%d\\n",eshort);
    if (eshort < pshort)
      pshort = eshort;
    /*for(int i = 0; i<H; ++i)
    {
            for(int j = 0; j < W; ++ j)
            {
                    if(i == princess.y&&j == princess.x)
                            printf("X");
                    else
                    printf("%d",labyrinth[i][j]);
            }
            printf("\\n");
    }*/
    if (!scnt && pshort != INF) {
      tf = true;
    } else {
      if (pshort != INF) {
        goal = 2;
        for (int i = 0; i < scnt; ++i) {
          memset(vis, false, sizeof(vis));
          shortest = INF;
          sshort = dfs(soldier[i].x, soldier[i].y, 0);
          // printf("sshort:%d\\n",sshort);
          if (sshort <= pshort) {
            tf = false;
            break;
          }
        }

      } else
        tf = false;
    }
    if (tf) {
      printf("Yes\\n");
    } else {
      printf("No\\n");
    }
  }
}