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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#define MAXN 210
#define INF 0x3f3f3f3f
using namespace std;

int R, C;
char a[MAXN][MAXN];

struct Cell {
  int r, c;
  Cell(int r, int c) : r(r), c(c) {}
};

const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, -1, 1};
int d[MAXN][MAXN][2], vis[MAXN][MAXN][2];

queue<Cell> Q;
void bfs(int kind) {
  while (!Q.empty()) {
    Cell cell = Q.front();
    Q.pop();
    int r = cell.r, c = cell.c;
    for (int dir = 0; dir < 4; dir++) {
      int nr = r + dr[dir], nc = c + dc[dir];
      if (nr >= 0 && nr < R && nc >= 0 && nc < C && a[nr][nc] == '.' &&
          !vis[nr][nc][kind]) {
        Q.push(Cell(nr, nc));
        vis[nr][nc][kind] = 1;
        d[nr][nc][kind] = d[r][c][kind] + 1;
      }
    }
  }
}

int ans;
void check(int r, int c) {
  if (a[r][c] != '.' || !vis[r][c][0])
    return;
  if (!vis[r][c][1] || d[r][c][0] < d[r][c][1])
    ans = min(ans, d[r][c][0] + 1);
}

int main() {

  scanf("%d%d", &R, &C);
  int jr, jc;
  int tr, tc;
  vector<Cell> fires;
  for (int i = 0; i < R; i++) {
    scanf("%s", a[i]);
    for (int j = 0; j < C; j++)

      if (a[i][j] == '%') {
        tr = i;
        tc = j;
        a[i][j] = '.';
      } else if (a[i][j] == '@') {
        jr = i;
        jc = j;
        a[i][j] = '.';
      } else if (a[i][j] == '$') {
        fires.push_back(Cell(i, j));
        a[i][j] = '.';
      }
  }
  memset(vis, 0, sizeof(vis));
  vis[jr][jc][0] = 1;
  d[jr][jc][0] = 0;
  Q.push(Cell(jr, jc));
  bfs(0);
  for (int i = 0; i < fires.size(); i++) {
    vis[fires[i].r][fires[i].c][1] = 1;
    d[fires[i].r][fires[i].c][1] = 0;
    Q.push(fires[i]);
  }
  bfs(1);
  ans = INF;
  check(tr, tc);
  if (ans == INF)
    printf("No\\n");
  else
    printf("Yes\\n");

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int H, W;
int exx = -1, exy = -1;
struct graph {
  int E[230][230];
};
graph ori;
void graph_clr(graph &k) {
  for (int i = 0; i < H + 2; i++)
    for (int j = 0; j < W + 2; j++)
      k.E[i][j] = 0;
}
struct S {
  int x, y;
  graph dfs;
};
vector<S> SV;
S princess;
void read() {
  char t;
  cin >> H >> W;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) {
      cin >> t;
      ori.E[i][j] = t;
      if (t == '$') {
        S st;
        graph_clr(st.dfs);
        st.x = i;
        st.y = j;
        SV.push_back(st);
      } else if (t == '@') {
        graph_clr(princess.dfs);
        princess.x = i;
        princess.y = j;
      } else if (t == '%') {
        exx = i;
        exy = j;
      }
    }
}
void Sdfs(S &t, int step, int x, int y) {
  if (ori.E[x][y] == '#')
    return;
  if (x < 0 || y < 0 || x > H - 1 || y > W - 1)
    return;
  // if(t.dfs.E[x][y]!=0)return;
  if (x == t.x && y == t.y && step != 0)
    return;

  // if(t.dfs.E[exx][exy])return;

  if (t.dfs.E[x][y] == 0 || t.dfs.E[x][y] > step)
    t.dfs.E[x][y] = step;
  else
    return;
  Sdfs(t, step + 1, x + 1, y);
  Sdfs(t, step + 1, x - 1, y);
  Sdfs(t, step + 1, x, y + 1);
  Sdfs(t, step + 1, x, y - 1);
}
/*void Pdfs(S &t,int step,int x,int y)
{
    if(ori.E[x][y]=='#')return;
    if(x<0||y<0||x>H-1||y>W-1)return;
    //if(t.dfs.E[x][y]!=0)return;
    if(x==t.x&&y==t.y&&step!=0)return;
    if(t.dfs.E[x][y]==0||t.dfs.E[x][y]>step)
    t.dfs.E[x][y]=step;
    else return;
    Sdfs(t,step+1,x+1,y);
    Sdfs(t,step+1,x-1,y);
    Sdfs(t,step+1,x,y+1);
    Sdfs(t,step+1,x,y-1);
}*/
void showG(graph &k) {
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cout << k.E[i][j] << ' ';
    }
    cout << endl;
  }
  cout << endl;
}
bool pre1() {
  Sdfs(princess, 0, princess.x, princess.y);
  for (int t = 0; t < SV.size(); t++) {
    Sdfs(SV[t], 0, SV[t].x, SV[t].y);
    if (SV[t].dfs.E[exx][exy] <= princess.dfs.E[exx][exy]) {
      // showG(SV[t].dfs);
      // showG(princess.dfs);
      return 0;
    }
    // showG(SV[t].dfs);
  }
  return 1;
  // showG(princess.dfs);
}
int main() {
  ios::sync_with_stdio(false);
  read();
  cout << (pre1() ? "Yes" : "No") << endl;
}