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

#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
using namespace std;
char map[210][210];
int step[210][210];
int px, py, ex, ey;
int pstep = 500, sstep = 500;
struct node {
  int x, y, step;
} a[410];
void bfs(int h, int w) {
  queue<node> Q;
  while (!Q.empty())
    Q.pop();
  node tem;
  tem.x = ex, tem.y = ey;
  tem.step = 0;
  step[tem.x][tem.y] = 0;
  Q.push(tem);
  while (!Q.empty()) {
    tem = Q.front();
    Q.pop();
    if (map[tem.x][tem.y] == '@' && tem.step < pstep)
      pstep = tem.step;
    else if (map[tem.x][tem.y] == '$' && tem.step < sstep)
      sstep = tem.step;
    node t;
    if (tem.x - 1 >= 0 && map[tem.x - 1][tem.y] != '#' &&
        tem.step + 1 < step[tem.x - 1][tem.y]) {
      t.x = tem.x - 1;
      t.y = tem.y;
      t.step = tem.step + 1;
      step[t.x][t.y] = t.step;
      /*	if(map[tem.x-1][tem.y]=='@' && t.step<pstep)
                      pstep=t.step;
              else if(map[tem.x-1][tem.y]=='$' && t.step<sstep)
                      sstep=t.step;
                      */
      Q.push(t);
    }
    if (tem.x + 1 < h && map[tem.x + 1][tem.y] != '#' &&
        tem.step + 1 < step[tem.x + 1][tem.y]) {
      t.x = tem.x + 1;
      t.y = tem.y;
      t.step = tem.step + 1;
      step[t.x][t.y] = t.step;
      /*
 if(map[tem.x+1][tem.y]=='@' && t.step<pstep)
              pstep=t.step;
      else if(map[tem.x+1][tem.y]=='$' && t.step<sstep)
              sstep=t.step;
              */
      Q.push(t);
    }
    if (tem.y - 1 >= 0 && map[tem.x][tem.y - 1] != '#' &&
        tem.step + 1 < step[tem.x][tem.y - 1]) {
      t.x = tem.x;
      t.y = tem.y - 1;
      t.step = tem.step + 1;
      step[t.x][t.y] = t.step;
      /*
 if(map[tem.x][tem.y-1]=='@' && t.step<pstep)
              pstep=t.step;
      else if(map[tem.x][tem.y-1]=='$' && t.step<sstep)
              sstep=t.step;
              */
      Q.push(t);
    }
    if (tem.y + 1 < w && map[tem.x][tem.y + 1] != '#' &&
        tem.step + 1 < step[tem.x][tem.y + 1]) {
      t.x = tem.x;
      t.y = tem.y + 1;
      t.step = tem.step + 1;
      step[t.x][t.y] = t.step;
      /*
      if(map[tem.x][tem.y+1]=='@' && t.step<sstep)
              sstep=t.step;
      else if(map[tem.x][tem.y+1]=='$' && t.step<sstep)
              sstep=t.step;
              */
      Q.push(t);
    }
  }
}

int main() {
  int h, w;
  while (scanf("%d%d", &h, &w) != EOF) {
    pstep = 500, sstep = 500;
    memset(a, 0, sizeof(a));
    memset(step, 0x3f, sizeof(step));
    int i, j;
    for (i = 0; i < h; i++) {
      scanf("%s", &map[i]);
      for (j = 0; j < w; j++) {
        if (map[i][j] == '@') {
          px = i;
          py = j;
        } else if (map[i][j] == '%')
          ex = i, ey = j;
      }
    }
    bfs(h, w);
    if (pstep < sstep)
      printf("Yes\\n");
    else
      printf("No\\n");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> P;
int N, M;
char mz[105][105];
int mark[105][105];
int ans = 0;
int armyans = 0x3f3f3f3f;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
int BFS(int c, int l) {
  memset(mark, 0, sizeof(mark));
  queue<P> que;
  que.push(P(c, l));
  while (!que.empty()) {
    c = que.front().first, l = que.front().second; // Tmp=Queue.Top
    if (mz[c][l] == '%')
      return mark[c][l]; //?束条件:找到?点
    que.pop();
    for (int i = 0; i < 4; i++) // 状??展，列出下一?的所有可能状?
    {
      int x = c + dx[i];
      int y = l + dy[i];
      if (x < 0 || y < 0 || x >= N || y >= M)
        continue;
      if (!mark[x][y] && mz[x][y] != '#') // 状?合法，?生成新状?NEXT
      {
        mark[x][y] = mark[c][l] + 1; // NEXT.STEP=Tmp.STEP+1
        que.push(P(x, y));           // 加入?列
      }
    }
  }
  return -1;
}
int main() {
  while (~scanf("%d%d", &N, &M)) {
    armyans = 0x3f3f3f3f;
    for (int i = 0; i < N; i++) {
      scanf("%s", mz[i]);
    }
    for (int i = 0; i < N; i++)
      for (int j = 0; j < M; j++) {
        if (mz[i][j] == '@')
          ans = BFS(i, j);
        if (mz[i][j] == '$') {
          int temp = BFS(i, j);
          armyans = min(temp, armyans);
        }
      }
    if (ans < armyans)
      printf("Yes\\n");
    else
      printf("No\\n");
  }
  return 0;
}