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

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int H, W, N;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
char MAP[1000 + 1][1000 + 1];
int f[1000 + 1][1000 + 1];
int d[1000 + 1][1000 + 1];
int main() {
  cin >> H >> W >> N;
  vector<int> ans(N + 1);
  int startx, starty;
  for (int i = 1; i < H + 1; ++i) {
    for (int j = 1; j < W + 1; ++j) {
      cin >> MAP[i][j];
      f[i][j] = 0;
      if (MAP[i][j] == 'S') {
        startx = i, starty = j;
        // qux.push(i);
        // quy.push(j);
        f[i][j] = 1;
        d[i][j] = 0;
      }
    }
  }
  // cout << qux.size() << " " << quy.size() << "\\n";
  for (int i = 1; i < N + 1; ++i) {
    int x;
    int y;
    queue<int> qux;
    queue<int> quy;
    qux.push(startx);
    quy.push(starty);
    while (1) {
      if (!(qux.size() && quy.size())) {
        cout << x << " " << y << "\\n";
        break;
      }
      x = qux.front();
      y = quy.front();
      // cout << x << " " << y << " " << d[x][y] << "\\n";
      qux.pop();
      quy.pop();
      if (atoi(&MAP[x][y]) == i) {
        ans[i] = d[x][y];
        // cout << i << " " <<  d[x][y] << "\\n";
        for (int j = 1; j < H + 1; ++j) {
          for (int k = 1; k < W + 1; ++k) {
            f[j][k] = 0;
          }
        }
        startx = x;
        starty = y;
        d[x][y] = 0;
        break;
      }
      for (int j = 0; j < 4; ++j) {
        int nx = x + dx[j];
        int ny = y + dy[j];
        if (1 <= nx && nx <= H && 1 <= ny && ny <= W && MAP[nx][ny] != 'X' &&
            f[nx][ny] == 0) {
          // cout << "ok\\n";
          qux.push(nx);
          quy.push(ny);
          f[nx][ny] = 1;
          d[nx][ny] = d[x][y] + 1;
        }
      }
    }
  }
  int ANS = 0;
  for (int i = 1; i < N + 1; ++i) {
    ANS += ans[i];
  }
  cout << ANS << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int H, W, N;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
char MAP[1000 + 1][1000 + 1];
int f[1000 + 1][1000 + 1];
int d[1000 + 1][1000 + 1];
int main() {
  cin >> H >> W >> N;
  queue<char> qux;
  queue<char> quy;
  vector<int> ans(N + 1);
  for (int i = 1; i < H + 1; ++i) {
    for (int j = 1; j < W + 1; ++j) {
      cin >> MAP[i][j];
      f[i][j] = 0;
      if (MAP[i][j] == 'S') {
        qux.push(i);
        quy.push(j);
        f[i][j] = 1;
        d[i][j] = 0;
      }
    }
  }
  int x;
  int y;
  for (int i = 1; i < N + 1; ++i) {
    while (1 /*qux.size()&&quy.size()*/) {
      x = qux.front();
      y = quy.front();
      // cout << x << " " << y << " " << d[x][y] << "\\n";
      qux.pop();
      quy.pop();
      // if('0'<=MAP[x][y]&&MAP[x][y]<='9'){
      // if(((int)MAP[x][y]-'0')==i) {
      if (atoi(&MAP[x][y]) == i) {
        ans[i] = d[x][y];
        for (int j = 1; j < H + 1; ++j) {
          for (int k = 1; k < W + 1; ++k) {
            f[i][j] = 0;
          }
        }
        d[x][y] = 0;
        break;
      }
      //}
      for (int j = 0; j < 4; ++j) {
        int nx = x + dx[j];
        int ny = y + dy[j];
        if (1 <= nx && nx <= H && 1 <= ny && ny <= W && MAP[nx][ny] != 'X' &&
            f[nx][ny] == 0) {
          qux.push(nx);
          quy.push(ny);
          f[nx][ny] = 1;
          d[nx][ny] = d[x][y] + 1;
        }
      }
    }
  }
  int ANS = 0;
  for (int i = 1; i < N + 1; ++i) {
    ANS += ans[i];
  }
  cout << ANS << "\\n";
  return 0;
}