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


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
const double EPS = 1e-9;
const double PI = acos(-1);
const int knightDir[8][2] = {{-2, -1}, {-2, 1}, {-1, 2},  {1, 2},
                             {2, -1},  {2, 1},  {-1, -2}, {1, -2}};
// const int dx[]={0,0,0,1,1,1,-1,-1,-1};
// const int dy[]={-1,0,1,-1,0,1,-1,0,1};
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
// #define MOD         1000000007LL
#define MAX 1005
const ll MOD = 1000000000 + 7;

bool vis[MAX][MAX];
int cost[MAX][MAX];
string maze[MAX];
int w, h, cx, cy, fx, fy;
bool isSafe(int x, int y) {
  if (x < 0 || x >= w || y < 0 || y >= h)
    return false;
  return true;
}
int main() {
  FAST int t;
  t = 1;

  while (t--) {

    cin >> w >> h;
    cin >> cx >> cy;
    cx--, cy--;
    cin >> fx >> fy;
    fx--, fy--;

    for (int i = 0; i < w; i++) {
      cin >> maze[i];
    }

    memset(cost, -1, sizeof cost);

    deque<pair<int, int>> q;
    q.push_front({cx, cy});
    cost[cx][cy] = 0;

    while (!q.empty()) {

      pair<int, int> t = q.front();
      q.pop_front();

      if (vis[t.first][t.second]) {
        continue;
      }
      vis[t.first][t.second] = true;

      for (int i = 0; i < 4; i++) {
        int nx = t.first + dx[i];
        int ny = t.second + dy[i];

        if (!isSafe(nx, ny))
          continue;
        if (maze[nx][ny] == '#')
          continue;

        if (cost[nx][ny] == -1 || cost[t.first][t.second] < cost[nx][ny]) {
          cost[nx][ny] = cost[t.first][t.second];
          q.push_front({nx, ny});
        }
      }

      for (int i = -2; i < 3; i++) {
        for (int j = -2; j < 3; j++) {
          int nx = t.first + i;
          int ny = t.second + j;

          if (!isSafe(nx, ny))
            continue;
          if (maze[nx][ny] == '#')
            continue;

          if (cost[nx][ny] == -1 ||
              cost[t.first][t.second] + 1 < cost[nx][ny]) {
            cost[nx][ny] = cost[t.first][t.second] + 1;
            q.push_back({nx, ny});
          }
        }
      }
    }

    cout << cost[fx][fy] << endl;
  }
}


Error: 0

Fixed submission:


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
const double EPS = 1e-9;
const double PI = acos(-1);
const int knightDir[8][2] = {{-2, -1}, {-2, 1}, {-1, 2},  {1, 2},
                             {2, -1},  {2, 1},  {-1, -2}, {1, -2}};
// const int dx[]={0,0,0,1,1,1,-1,-1,-1};
// const int dy[]={-1,0,1,-1,0,1,-1,0,1};
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
// #define MOD         1000000007LL
#define MAX 1005
const ll MOD = 1000000000 + 7;

bool vis[MAX][MAX];
int cost[MAX][MAX];
int w, h, cx, cy, fx, fy;
bool isSafe(int x, int y) {
  if (x < 0 || x >= w || y < 0 || y >= h)
    return false;
  else
    return true;
}
int main() {
  FAST int t;
  t = 1;

  while (t--) {

    cin >> w >> h;
    cin >> cx >> cy;
    cx--, cy--;
    cin >> fx >> fy;
    fx--, fy--;

    string maze[h + 1];
    for (int i = 0; i < h; i++) {
      cin >> maze[i];
    }

    memset(cost, -1, sizeof cost);

    queue<pair<int, int>> q;
    q.push({cx, cy});

    while (!q.empty()) {

      pair<int, int> t = q.front();
      q.pop();

      if (vis[t.first][t.second]) {
        continue;
      }
      vis[t.first][t.second] = 1;

      for (int i = 0; i < 4; i++) {
        int nx = t.first + dx[i];
        int ny = t.second + dy[i];

        if (!isSafe(nx, ny))
          continue;
        if (maze[nx][ny] == '#')
          continue;

        if (cost[nx][ny] == -1 || cost[t.first][t.second] < cost[nx][ny]) {
          cost[nx][ny] = cost[t.first][t.second];
          q.push({nx, ny});
        }
      }

      for (int i = -2; i < 3; i++) {
        for (int j = -2; j < 3; j++) {
          int nx = t.first + i;
          int ny = t.second + j;

          if (!isSafe(nx, ny))
            continue;
          if (maze[nx][ny] == '#')
            continue;

          if (cost[nx][ny] == -1 ||
              cost[t.first][t.second] + 1 < cost[nx][ny]) {
            cost[nx][ny] = cost[t.first][t.second] + 1;
            q.push({nx, ny});
          }
        }
      }
    }

    cout << cost[fx][fy] << endl;
  }
}