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

#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;

bool flag[1005][1005];
int temp[1005][1005];
char map[1005][1005], ch[1005];
int n, m, xy[10][2], sum;
char tt[10] =
    {
        '1', '2', '3', '4', '5', '6', '7', '8', '9', '10',
},
     t;
void initflag() {
  for (int i = 0; i < 1005; i++) {
    memset(flag[i], true, sizeof(flag[i]));
  }
}
void initmap() {
  for (int i = 0; i < 1005; i++) {
    memset(map[i], 'X', sizeof(map[i]));
  }
}
int main() {
  int i, j, k, x, y;
  while (~scanf("%d%d%d", &n, &m, &k)) {
    initmap();
    for (i = 1; i <= n; i++) {
      scanf("%s", ch);
      for (j = 1; j <= m; j++) {
        map[i][j] = ch[j - 1];
        if (map[i][j] == 'S') {
          xy[0][0] = i;
          xy[0][1] = j;
        }
        if (map[i][j] >= '1' && map[i][j] <= '9') {
          xy[map[i][j] - '0'][0] = i;
          xy[map[i][j] - '0'][1] = j;
        }
      }
    }
    /*
    for(i=0;i<=n+1;i++){
            for(j=0;j<=m+1;j++)
            printf("%c",map[i][j]);
            printf("\\n");
    }
    for(i=0;i<=n+1;i++){
            for(j=0;j<=m+1;j++)
            printf("%d",flag[i][j]);
            printf("\\n");
    }
    for(i=0;i<10;i++){
            printf("%c %d %d\\n",tt[i],xy[i][0],xy[i][1]);
    }
    */

    sum = 0;
    for (i = 0; i < k; i++) {
      initflag(); // 初始 flag
      x = xy[i][0];
      y = xy[i][1]; // 起点坐?
      queue<int> q; // ??
      q.push(x * 10000 + y);
      flag[x][y] = false; //??
      temp[x][y] = 0;     //?数
      t = tt[i];          // 目?

      // printf("i=%d x=%d y=%d q=%d t=%c\\n" ,i,x,y,q.front(),t);
      while (!q.empty()) {
        x = q.front() / 10000;
        y = q.front() % 10000;
        if (flag[x + 1][y] && map[x + 1][y] != 'X') {
          q.push((x + 1) * 10000 + y);
          flag[x + 1][y] = false;
          temp[x + 1][y] = temp[x][y] + 1;
        }
        if (map[x + 1][y] == t) {
          sum += temp[x + 1][y];
          break;
        }

        if (flag[x - 1][y] && map[x - 1][y] != 'X') {
          q.push((x - 1) * 10000 + y);
          flag[x - 1][y] = false;
          temp[x - 1][y] = temp[x][y] + 1;
        }
        if (map[x - 1][y] == t) {
          sum += temp[x - 1][y];
          break;
        }
        if (flag[x][y + 1] && map[x][y + 1] != 'X') {
          q.push(x * 10000 + y + 1);
          flag[x][y + 1] = false;
          temp[x][y + 1] = temp[x][y] + 1;
        }
        if (map[x][y + 1] == t) {
          sum += temp[x][y + 1];
          break;
        }
        if (flag[x][y - 1] && map[x][y - 1] != 'X') {
          q.push(x * 10000 + y - 1);
          flag[x][y - 1] = false;
          temp[x][y - 1] = temp[x][y] + 1;
        }
        if (map[x][y - 1] == t) {
          sum += temp[x][y - 1];
          break;
        }
        q.pop();
      }
    }
    printf("%d\\n", sum);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string.h>

using namespace std;

int H, W;
int N;
char a[1005][1005];
bool b[1005][1005];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

struct Node {
  int x;
  int y;
  int deep;
};

struct Node Temp;

void bfs(int n) {
  queue<struct Node> t;
  t.push(Temp);
  while (t.size()) {
    Temp = t.front();
    t.pop();
    int x = Temp.x;
    int y = Temp.y;
    int d = Temp.deep;
    if (a[x][y] == n + 48) {
      break;
    }
    if (b[x][y])
      b[x][y] = false;

    for (int i = 0; i < 4; i++) {
      int x1 = x + dx[i];
      int y1 = y + dy[i];
      if (x1 >= 0 && x1 < W && y1 >= 0 && y1 < H && a[x1][y1] != 'X' &&
          b[x1][y1]) {
        Temp.x = x1;
        Temp.y = y1;
        Temp.deep = d + 1;
        t.push(Temp);
      }
    }
  }
}

int main() {
  while (cin >> H >> W >> N) {
    int i, j;
    int sx = 0, sy = 0;
    for (j = 0; j < H; j++)
      for (i = 0; i < W; i++) {
        cin >> a[i][j];
        if (a[i][j] == 'S') {
          sx = i;
          sy = j;
        }
      }
    Temp.x = sx;
    Temp.y = sy;
    Temp.deep = 0;

    for (int k = 1; k <= N; k++) {
      memset(b, true, sizeof(b));
      bfs(k);
    }
    cout << Temp.deep << endl;
  }
  return 0;
}