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

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

const int DIRECTSIZE = 4;
struct _direct {
  int drow;
  int dcol;
} direct[DIRECTSIZE] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};

const int N = 1000;
const int N2 = 10;
char grid[N][N], visited[N][N];
int h, w, n;
struct _factory {
  int row;
  int col;
} factory[N2];

struct _node {
  int row;
  int col;
  int level;
};

int bfs(int n, char goal) {
  memset(visited, 0, sizeof(visited));
  _node start = {factory[n - 1].row, factory[n - 1].col, 0}, temp;
  visited[start.row][start.col] = 1;
  queue<_node> q;
  q.push(start);
  while (!q.empty()) {
    _node front = q.front();
    q.pop();
    for (int i = 0; i < DIRECTSIZE; i++) {
      int nextrow = front.row + direct[i].drow;
      int nextcol = front.col + direct[i].dcol;
      if (0 <= nextrow && nextrow < h && 0 <= nextcol && nextcol < w) {
        if (grid[nextrow][nextcol] == goal) {
          return front.level + 1;
        } else if (visited[nextrow][nextcol] == 0 &&
                   grid[nextrow][nextcol] != 'X') {
          visited[nextrow][nextcol] = 1;
          temp.row = nextrow;
          temp.col = nextcol;
          temp.level = front.level + 1;
          q.push(temp);
        }
      }
    }
  }
  return 0;
}

int main() {
  while (~scanf("%d %d %d", &h, &w, &n)) {
    for (int i = 0; i < h; i++) {
      scanf("%s", grid[i]);
      for (int j = 0; j < w; j++) {
        if (grid[i][j] == 'S') {
          factory[0].row = i;
          factory[0].col = j;
        } else if (isdigit(grid[i][j])) {
          factory[grid[i][j] - '0'].row = i;
          factory[grid[i][j] - '0'].col = j;
        }
      }
    }
    int minstep = 0;
    char goal = '1';
    for (int i = 1; i <= n; i++) {
      minstep += bfs(i, goal);
      goal++;
    }
    printf("%d\\n", minstep);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
char a[1100][1100];
int vis[1100][1100];
int d[4][2] = {0, 1, 0, -1, -1, 0, 1, 0};
int n, m, k, sum;
struct node {
  int x;
  int y;
  int t;
} nod;
int make(int x, int y) {
  if (!vis[x][y] && x >= 0 && x < n && y >= 0 && y < m && a[x][y] != 'X')
    return 1;
  else
    return 0;
}
void bfs(node no, char kk) {
  queue<node> q;
  vis[no.x][no.y] = 1;
  int i, j;
  q.push(no);
  while (!q.empty()) {
    node head = q.front();
    q.pop();
    for (i = 0; i < 4; i++) {
      node tail;
      tail.x = head.x + d[i][0];
      tail.y = head.y + d[i][1];
      tail.t = head.t + 1;
      if (make(tail.x, tail.y)) {
        if (a[tail.x][tail.y] == kk) {
          sum += tail.t;
          tail.t = 0;
          nod = tail;
          return;
        } else {
          vis[tail.x][tail.y] = 1;
          q.push(tail);
        }
      }
    }
  }
}
int main(void) {
  int i, j, x, y;
  while (scanf("%d%d%d", &n, &m, &k) != EOF) {
    for (i = 0; i < n; i++) {
      scanf("%s", a[i]);
      for (j = 0; j < m; j++) {
        if (a[i][j] == 'S') {
          nod.x = i;
          nod.y = j;
          nod.t = 0;
        }
      }
    }
    sum = 0;
    for (i = 1; i <= k; i++) {
      memset(vis, 0, sizeof(vis));
      bfs(nod, '0' + i);
    }
    printf("%d\\n", sum);
  }
  return 0;
}