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

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

const int MAXN = 1e3 + 5, MAXM = 1e6 + 5;
const int dr[10] = {0, -1, 1, 0, 0}, dc[10] = {0, 0, 0, -1, 1};
int H, W;
int R1, C1, R2, C2;
char C[MAXN][MAXN];
struct edge {
  int V, W;
  edge() {}
  edge(int v, int w) { V = v, W = w; }
  friend bool operator<(edge a, edge b) { return a.W > b.W; }
};
vector<edge> G[MAXM];
int dis[MAXM];
bool vis[MAXM];

void addEdge(int u, int v, int w) {
  G[u].push_back(edge(v, w));
  G[v].push_back(edge(u, w));
}

void Dijkstra(int s) {
  memset(dis, 0x3f, sizeof(dis));
  priority_queue<edge> q;
  q.push(edge(s, 0));
  dis[s] = 0;
  while (!q.empty()) {
    int u = q.top().V;
    q.pop();
    if (vis[u])
      continue;
    vis[u] = 1;
    int siz = G[u].size();
    for (int i = 0; i < siz; i++) {
      int v = G[u][i].V, w = G[u][i].W;
      if (dis[u] + w < dis[v]) {
        dis[v] = dis[u] + w;
        q.push(edge(v, dis[v]));
      }
    }
  }
}

int main() {
  scanf("%d %d", &H, &W);
  scanf("%d %d %d %d", &R1, &C1, &R2, &C2);
  for (int r1 = 1; r1 <= H; r1++) {
    scanf("\\n");
    for (int c1 = 1; c1 <= W; c1++)
      scanf("%c", &C[r1][c1]);
  }
  for (int r1 = 1; r1 <= H; r1++) {
    for (int c1 = 1; c1 <= W; c1++) {
      if (C[r1][c1] == '#')
        continue;
      int indexs = (r1 - 1) * W + c1;
      for (int r2 = r1 - 2; r2 <= r1 + 2; r2++) {
        for (int c2 = c1 - 2; c2 <= c1 + 2; c2++) {
          int indext = (r2 - 1) * W + c2;
          if (r2 < 1 || c2 < 1 || r2 > H || c2 > W || C[r2][c2] == '#' ||
              indext == indexs)
            continue;
          addEdge(indexs, indext, 1);
        }
      }
      for (int i = 1; i <= 4; i++) {
        int nr = r1 + dr[i], nc = c1 + dc[i];
        int indext = (nr - 1) * W + nc;
        if (nr < 1 || nc < 1 || nr > H || nc > W || C[nr][nc] == '#')
          continue;
        addEdge(indexs, indext, 0);
      }
    }
  }
  int indexS = (R1 - 1) * W + C1, indexT = (R2 - 1) * W + C2;
  Dijkstra(indexS);
  if (dis[indexT] == 0x3f3f3f3f)
    printf("-1");
  else
    printf("%d", dis[indexT]);
  return 0;
}


Error: WA

Fixed submission:

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

const int MAXN = 1e3 + 5, MAXM = 1e6 + 5;
const int dr[10] = {0, -1, 1, 0, 0}, dc[10] = {0, 0, 0, -1, 1};
int H, W;
int R1, C1, R2, C2;
char C[MAXN][MAXN];
struct edge {
  int V, W;
  edge() {}
  edge(int v, int w) { V = v, W = w; }
  friend bool operator<(edge a, edge b) { return a.W > b.W; }
};
vector<edge> G[MAXN];
int dis[MAXM];
bool vis[MAXM];

void addEdge(int u, int v, int w) {
  G[u].push_back(edge(v, w));
  G[v].push_back(edge(u, w));
}

void Dijkstra(int s) {
  memset(dis, 0x3f, sizeof(dis));
  priority_queue<edge> q;
  q.push(edge(s, 0));
  dis[s] = 0;
  while (!q.empty()) {
    int u = q.top().V;
    q.pop();
    if (vis[u])
      continue;
    vis[u] = 1;
    int siz = G[u].size();
    for (int i = 0; i < siz; i++) {
      int v = G[u][i].V, w = G[u][i].W;
      if (dis[u] + w < dis[v]) {
        dis[v] = dis[u] + w;
        q.push(edge(v, dis[v]));
      }
    }
  }
}

void print() {
  printf("\\n");
  printf("%d %d\\n", H, W);
  printf("%d %d\\n", R1, C1);
  printf("%d %d\\n", R2, C2);
  for (int r = 1; r <= H; r++) {
    for (int c = 1; c <= W; c++) {
      printf("%c", C[r][c]);
    }
    printf("\\n");
  }
  for (int i = 1; i <= H * W; i++) {
    printf("%d ", dis[i]);
  }
  printf("\\n");
  for (int i = 1; i <= H * W; i++) {
    printf("%d:\\n", i);
    int siz = G[i].size();
    for (int j = 0; j < siz; j++) {
      printf("%d %d\\n", G[i][j].V, G[i][j].W);
    }
  }
}

int main() {
  scanf("%d %d", &H, &W);
  scanf("%d %d %d %d", &R1, &C1, &R2, &C2);
  for (int r1 = 1; r1 <= H; r1++) {
    scanf("\\n");
    for (int c1 = 1; c1 <= W; c1++)
      scanf("%c", &C[r1][c1]);
  }
  for (int r1 = 1; r1 <= H; r1++) {
    for (int c1 = 1; c1 <= W; c1++) {
      if (C[r1][c1] == '#')
        continue;
      int indexs = (r1 - 1) * W + c1;
      for (int r2 = r1 - 2; r2 <= r1 + 2; r2++) {
        for (int c2 = c1 - 2; c2 <= c1 + 2; c2++) {
          int indext = (r2 - 1) * W + c2;
          if (r2 < 1 || c2 < 1 || r2 > H || c2 > W || C[r2][c2] == '#' ||
              indext == indexs)
            continue;
          addEdge(indexs, indext, 1);
        }
      }
      for (int i = 1; i <= 4; i++) {
        int nr = r1 + dr[i], nc = c1 + dc[i];
        int indext = (nr - 1) * W + nc;
        if (nr < 1 || nc < 1 || nr > H || nc > W || C[nr][nc] == '#')
          continue;
        addEdge(indexs, indext, 0);
      }
    }
  }
  int indexS = (R1 - 1) * W + C1, indexT = (R2 - 1) * W + C2;
  Dijkstra(indexS);
  if (dis[indexT] == 0x3f3f3f3f)
    printf("-1");
  else
    printf("%d", dis[indexT]);
  //	print();
  return 0;
}