Problem p01881 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int dx[] = {0, 0, -1, 1};
const int dy[] = {-1, 1, 0, 0};
const int maxn = 205;
char maze[maxn][maxn];
bool vis[maxn][maxn];
int n, m, dp, ds, cur;

const pair<int, int> step = make_pair(-1, -1);
queue<pair<int, int>> que;
void bfs(int x, int y) {
  while (!que.empty())
    que.pop();
  cur = 0, dp = INF, ds = INF;
  memset(vis, false, sizeof vis);

  vis[y][x] = true;
  que.push(make_pair(x, y));
  que.push(step);

  while (cur <= n + m && (dp == INF || ds == INF)) {
    pair<int, int> u = que.front();
    que.pop();

    if (maze[u.second][u.first] == '@')
      dp = min(dp, cur);
    if (maze[u.second][u.first] == '$')
      ds = min(ds, cur);

    if (u == step) {
      ++cur;
      que.push(step);
      continue;
    }
    for (int i = 0; i < 4; ++i) {
      int nx = u.first + dx[i], ny = u.second + dy[i];
      if (!vis[ny][nx] && 0 <= nx && 0 <= ny && nx < m && ny < n &&
          maze[ny][nx] != '#') {
        vis[ny][nx] = true;
        que.push(make_pair(nx, ny));
      }
    }
  }
}

int main() {
  while (~scanf("%d%d", &n, &m)) {
    for (int i = 0; i < n; ++i) {
      scanf("%s", maze[i]);
    }
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        if (maze[i][j] == '%') {
          bfs(j, i);
        }
      }
    }
    if (dp < ds)
      puts("Yes");
    else
      puts("No");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <vector>
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef vector<int> V;
typedef pair<int, int> P;
const int inf = 0x3f3f3f3f;
const ll INF = 2e18;
const double PI = acos(-1.0);
const double eps = 1e-9;
const int N = 5 + 1e2;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int dp[N][N];
char s[N][N];
bool vis[N][N];
int ans1, ans2, n, m;

void bfs(int i, int j) {
  dp[i][j] = 0;
  ans1 = ans2 = 0;
  queue<P> q;
  while (!q.empty())
    q.pop();
  q.push(P(i, j));
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    vis[p.x][p.y] = 1;
    if (ans1 && ans2)
      break;
    if (s[p.x][p.y] == '@')
      ans1 = dp[p.x][p.y];
    if (s[p.x][p.y] == '$')
      ans2 = dp[p.x][p.y];
    for (int k = 0; k < 4; k++) {
      int xx = dx[k] + p.x, yy = dy[k] + p.y;
      if (xx == 0 || yy == 0 || xx > n || yy > m)
        continue;
      if (vis[xx][yy])
        continue;
      if (s[xx][yy] == '#')
        continue;
      dp[xx][yy] = dp[p.x][p.y] + 1;
      q.push(P(xx, yy));
    }
  }
}

int main() {
  while (~scanf("%d%d", &n, &m)) {
    memset(dp, 0x3f, sizeof dp);
    memset(vis, 0, sizeof vis);
    for (int i = 1; i <= n; i++)
      scanf("%s", s[i] + 1);
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) {
        if (s[i][j] == '%')
          bfs(i, j);
      }
    if (ans1 < ans2 || ans2 == 0)
      puts("Yes");
    else
      puts("No");
  }
  return 0;
}