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

#include <bits/stdc++.h>
using namespace std;

const int MOD = 1000000007;
const int INF = 1e9;

int main() {
  int h, w, m;
  const int MAX = 3 * 1e5;
  cin >> h >> w >> m;
  int line_cnt[MAX + 1] = {};
  int column_cnt[MAX + 1] = {};
  int line_max = 0;
  int column_max = 0;
  int bx[MAX + 1];
  int by[MAX + 1];
  map<pair<int, int>, int> bomb;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    bx[i] = x;
    by[i] = y;
    bomb[make_pair(x, y)] = 1;
    column_cnt[x]++;
    line_cnt[y]++;
    if (column_max < column_cnt[x])
      column_max = column_cnt[x];
    if (line_max < line_cnt[y])
      line_max = line_cnt[y];
  }
  vector<int> candidate_line;
  vector<int> candidate_column;
  vector<int> line_map(w, 0);
  vector<int> column_map(h, 0);
  for (int i = 0; i < h; i++) {
    if (column_cnt[i] == column_max) {
      candidate_column.push_back(i);
      column_map[i] = 1;
    }
  }
  for (int i = 0; i < w; i++) {
    if (line_cnt[i] == line_max) {
      candidate_line.push_back(i);
      line_map[i] = 1;
    }
  }

  long long ans = line_max + column_max - 1;
  long long cross_cnt = candidate_line.size() * candidate_column.size();
  long long cnt = 0;
  bool flag = 0;
  for (int i = 0; i < m; i++) {
    int x = bx[i];
    int y = by[i];
    if (column_map[x] == 1 && line_map[y] == 1)
      cnt++;
  }
  // if(flag) ans ++;
  if (cross_cnt > cnt)
    ans++;

  cout << ans << endl;

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int MOD = 1000000007;
const int INF = 1e9;

int main() {
  int h, w;
  int sx, sy;
  int ex, ey;
  char s[1001][1001];

  cin >> h >> w;
  cin >> sx >> sy;
  cin >> ex >> ey;
  sx--;
  sy--;
  ex--;
  ey--;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> s[i][j];
    }
  }

  queue<pair<int, int>> todo;
  vector<vector<int>> seen(1001, vector<int>(1001, -1));
  todo.push(make_pair(sx, sy));
  seen[sx][sy] = 0;

  int dx[] = {0, 0, 1, -1};
  int dy[] = {1, -1, 0, 0};
  bool cant_flag = 0;
  int cnt = 0;

  vector<int> dwx;
  vector<int> dwy;
  vector<vector<int>> warp(1001, vector<int>(1001, INF));
  warp[sx][sy] = 0;
  for (int i = -2; i <= 2; i++) {
    for (int j = -2; j <= 2; j++) {
      if (i == 0 && j == 0)
        continue;
      dwx.push_back(i);
      dwy.push_back(j);
    }
  }
  // for(int i = 0; i < 24; i++){
  //   cout << dx[i] << ", " << dy[i] << endl;
  // }

  while (!todo.empty()) {
    cant_flag = 0;
    int x = todo.front().first;
    int y = todo.front().second;
    todo.pop();

    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx >= h || ny >= w || nx < 0 || ny < 0)
        continue;
      // if(seen[nx][ny] >= 0 && warp[nx][ny] <= warp[x][y]) continue;
      if (warp[nx][ny] <= warp[x][y])
        continue;
      if (s[nx][ny] == '#')
        continue;
      cant_flag = 1;
    }

    if (cant_flag == 1) {
      // can walk to next map
      for (int i = 0; i < 4; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx >= h || ny >= w || nx < 0 || ny < 0)
          continue;
        // if(seen[nx][ny] >= 0 && warp[nx][ny] <= warp[x][y]) continue;
        if (warp[nx][ny] <= warp[x][y])
          continue;
        if (s[nx][ny] == '#')
          continue;
        // seen[nx][ny] = seen[x][y] + 1;
        warp[nx][ny] = warp[x][y];
        todo.push(make_pair(nx, ny));
      }
    } else {
      // need to use warp;
      for (int i = 0; i < 24; i++) {
        int nx = x + dwx[i];
        int ny = y + dwy[i];
        if (nx >= h || ny >= w || nx < 0 || ny < 0)
          continue;
        // if(seen[nx][ny] >= 0 && warp[nx][ny] <= warp[x][y] + 1) continue;
        if (warp[nx][ny] <= warp[x][y] + 1)
          continue;
        if (s[nx][ny] == '#')
          continue;
        todo.push(make_pair(nx, ny));
        warp[nx][ny] = warp[x][y] + 1;
        // seen[nx][ny] = seen[x][y] + 1;
      }
    }
  }

  // for(int i = 0; i < h; i++){
  //   for(int j = 0; j < w; j++){
  //     cout << warp[i][j];
  //   }
  //   cout << endl;
  // }

  if (warp[ex][ey] != INF)
    cout << warp[ex][ey] << endl;
  else
    cout << -1 << endl;

  return 0;
}