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

#include <deque>
#include <iostream>
#include <utility>
using namespace std;

int main() {
  int h, w;
  cin >> h >> w;
  int startx, starty;
  cin >> startx >> starty;
  int endx, endy;
  cin >> endx >> endy;
  int board[h + 3][w + 3];
  int visit[h + 3][w + 3];
  for (int i = 1; i <= h; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= w; j++) {

      board[i][j] = (s[j - 1] == '.');
      // cout<<board[i][j]<<" ";
      visit[i][j] = 0;
    }
    // cout<<"\\n";
  }
  int dx[4] = {0, 0, -1, 1};
  int dy[4] = {-1, 1, 0, 0};
  deque<pair<pair<int, int>, int>> q;
  bool p = false;
  q.push_back(make_pair(make_pair(startx, starty), 0));
  while (!q.empty()) {
    pair<pair<int, int>, int> top = q.front();
    pair<int, int> first = top.first;
    int magic = top.second;
    int x = first.first;
    int y = first.second;
    q.pop_front();

    if (visit[x][y] == 1)
      continue;
    if (x == endx && y == endy) {
      cout << magic << endl;
      p = true;
      break;
    }
    visit[x][y] = 1;
    for (int i = 0; i < 4; i++) {
      int x1 = x + dx[i];
      int y1 = y + dy[i];

      if (x1 >= 1 && y1 >= 1 && x1 <= h && y1 <= w && visit[x1][y1] == 0 &&
          board[x1][y1]) {
        q.push_front(make_pair(make_pair(x1, y1), magic));
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {

        int x1 = x + i;
        int y1 = y + j;

        if (x1 >= 1 && y1 >= 1 && x1 <= h && y1 <= w && visit[x1][y1] == 0 &&
            board[x1][y1]) {
          q.push_back(make_pair(make_pair(x1, y1), magic + 1));
          // cout<<x1<<" "<<y1<<endl;
        }
      }
    }
  }
  if (p == false)
    cout << "-1" << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <deque>
#include <iostream>
#include <utility>
using namespace std;

int main() {
  int h, w;
  cin >> h >> w;
  int startx, starty;
  cin >> startx >> starty;
  int endx, endy;
  cin >> endx >> endy;
  int board[h + 3][w + 3];
  int visit[h + 3][w + 3];
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      char c;
      cin >> c;
      board[i][j] = c - '.';
      visit[i][j] = 0;
    }
  }
  int dx[4] = {0, 0, -1, 1};
  int dy[4] = {-1, 1, 0, 0};
  deque<pair<pair<int, int>, int>> q;
  q.push_back(make_pair(make_pair(startx, starty), 0));
  while (!q.empty()) {
    pair<pair<int, int>, int> top = q.front();
    pair<int, int> first = top.first;
    int magic = top.second;
    int x = first.first;
    int y = first.second;
    q.pop_front();
    if (visit[x][y] == 1)
      continue;
    if (x == endx && y == endy) {
      cout << magic << endl;
      return 0;
    }
    visit[x][y] = 1;
    for (int i = 0; i < 4; i++) {
      int x1 = x + dx[i];
      int y1 = y + dy[i];

      if (visit[x1][y1] == 0 && board[x1][y1] == 0) {
        q.push_front(make_pair(make_pair(x1, y1), magic));
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {

        int x1 = x + i;
        int y1 = y + j;

        if (visit[x1][y1] == 0 && board[x1][y1] == 0) {
          q.push_back(make_pair(make_pair(x1, y1), magic + 1));
        }
      }
    }
  }
  cout << "-1" << endl;

  return 0;
}