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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define maxn 2555
#define oo 1000000000
#define clearAll(a) memset(a, 0, sizeof(a))
#define sq(a) ((a) * (a))

using namespace std;

typedef long long ll;

bool g[maxn][maxn];
bool h[maxn][maxn];
int d[maxn];
int pre[maxn];
bool vis[maxn];
vector<int> v[maxn];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int W, H;

inline int getID(int x, int y) { return (x - 1) + (y - 1) * W; }

void deal(int sx, int sy, int tx, int ty) {
  if (sx == tx) {
    if (sx == 0 || sx == W)
      return;
    if (sy > ty)
      swap(sy, ty);
    for (int i = sy + 1; i <= ty; i++) {
      int id1 = getID(sx + 1, i);
      int id2 = getID(sx, i);
      g[id1][id2] = g[id2][id1] = true;
    }
  } else {
    if (sy == 0 || sy == H)
      return;
    if (sx > tx)
      swap(sx, tx);
    for (int i = sx + 1; i <= tx; i++) {
      int id1 = getID(i, sy);
      int id2 = getID(i, sy + 1);
      g[id1][id2] = g[id2][id1] = true;
    }
  }
}

void build() {
  int n;
  scanf("%d%d%d", &W, &H, &n);

  int sx, sy, tx, ty;

  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d%d", &sx, &sy, &tx, &ty);
    deal(sx, sy, tx, ty);
  }

  for (int i = 1; i <= W; i++)
    for (int j = 1; j <= H; j++)
      for (int k = 0; k < 4; k++) {
        int xx = i + dx[k];
        int yy = j + dy[k];
        if (xx < 1 || xx > W)
          continue;
        if (yy < 1 || yy > H)
          continue;
        int x = getID(i, j);
        int y = getID(xx, yy);

        if (g[x][y])
          continue;

        v[x].push_back(y);
      }
}

int init(int s, int t) {
  queue<int> q;
  q.push(s);
  int n = W * H;

  d[s] = 0;
  pre[s] = -1;
  for (int i = 0; i < n; i++)
    vis[i] = false;
  vis[s] = true;
  int x, y;

  while (!q.empty()) {
    x = q.front();
    q.pop();
    for (int i = v[x].size() - 1; i >= 0; i--)
      if (!vis[v[x][i]]) {
        y = v[x][i];
        pre[y] = x;
        d[y] = d[x] + 1;
        q.push(y);
        vis[y] = true;
      }
  }

  x = t;
  while (pre[x] != -1) {
    y = pre[x];
    h[y][x] = h[x][y] = true;
    x = y;
  }
  return d[t];
}

int bfs(int s, int t) {
  queue<int> q;
  q.push(s);
  int n = W * H;

  d[s] = 0;
  for (int i = 0; i < n; i++)
    vis[i] = false;
  vis[s] = true;
  int x, y;

  while (!q.empty()) {
    x = q.front();
    q.pop();
    for (int i = v[x].size() - 1; i >= 0; i--)
      if (!vis[v[x][i]]) {
        y = v[x][i];
        if (g[x][y])
          continue;

        d[y] = d[x] + 1;
        if (y == t)
          return d[y];
        q.push(y);
        vis[y] = true;
      }
  }

  return -1;
}

int main() {
  // freopen("C:\\\\Users\\\\py\\\\Desktop\\\\input.txt","r",stdin);
  // freopen("C:\\\\Users\\\\py\\\\Desktop\\\\output.txt","w",stdout);

  build();
  int sx, sy, tx, ty;
  scanf("%d%d%d%d", &sx, &sy, &tx, &ty);
  int now;
  int id1 = getID(sx + 1, sy + 1);
  int id2 = getID(tx + 1, ty + 1);
  now = init(id1, id2);
  int n = W * H;

  int next;
  int best = 0;

  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++)
      if (h[i][j]) {
        g[i][j] = g[j][i] = true;
        next = bfs(id1, id2);
        g[i][j] = g[j][i] = false;
        if (next == -1)
          continue;
        best = max(best, next - now);
      }
  printf("%d\\n", best);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
const int fa1 = 51;
const int fa = 1001;
int n, m, w, h, x, y, tot, xx, yy;
int a1[fa1][fa1];
int a2[fa1][fa1];
int dd[][2] = {1, 0, 0, 1, -1, 0, 0, -1};
bool pan(int x, int y) {
  if (x < 0 || y < 0)
    return false;
  if (x >= w || y >= h)
    return false;
  return true;
}
struct wgh {
  int x, y;
} s, t;
int d[fa1][fa1];
wgh pre[fa1][fa1];
bool work(int x1, int y1, int x2, int y2) {
  if (x1 == x2) {
    if (y1 > y2)
      swap(y1, y2);
    if (a2[x2][y2])
      return false;
    return true;
  }
  if (y1 == y2) {
    if (x1 > x2)
      swap(x1, x2);
    if (a1[x2][y2])
      return false;
    return true;
  }
}
void bfs() {
  memset(d, 0, sizeof(d));
  wgh o = s;
  queue<wgh> q;
  q.push(s);
  d[s.x][s.y] = 1;
  while (!q.empty()) {
    wgh o = q.front();
    q.pop();
    for (int k = 0; k < 4; k++) {
      x = o.x + dd[k][0];
      y = o.y + dd[k][1];
      if (pan(x, y)) {
        if (work(o.x, o.y, x, y)) {
          if (d[x][y] == 0) {
            wgh o1;
            o1.x = x;
            o1.y = y;
            d[x][y] = d[o.x][o.y] + 1;
            pre[x][y] = o;
            q.push(o1);
          }
        }
      }
    }
  }
  //        for(int i=0;i<=w;i++)
  //    {
  //        for(int j=0;j<=h;j++)
  //        {
  //            printf("%d ",d[i][j]);
  //        }
  //        puts("");
  //    }
}
int dist[fa1][fa1];
wgh aa[fa];
bool work1(int x1, int y1, int x2, int y2, int u) {
  if (x1 == aa[u].x && y1 == aa[u].y && x2 == aa[u + 1].x && y2 == aa[u + 1].y)
    return false;
  if (x2 == aa[u].x && y2 == aa[u].y && x1 == aa[u + 1].x && y1 == aa[u + 1].y)
    return false;
  return true;
}
void bfs1(int u) {
  memset(dist, 0, sizeof(dist));
  wgh o = s;
  queue<wgh> q;
  q.push(s);
  dist[s.x][s.y] = 1;
  while (!q.empty()) {
    wgh o = q.front();
    q.pop();
    for (int k = 0; k < 4; k++) {
      x = o.x + dd[k][0];
      y = o.y + dd[k][1];
      if (pan(x, y)) {
        if (work(o.x, o.y, x, y)) {
          if (work1(o.x, o.y, x, y, u))
            if (dist[x][y] == 0) {
              wgh o1;
              o1.x = x;
              o1.y = y;
              dist[x][y] = dist[o.x][o.y] + 1;
              pre[x][y] = o;
              q.push(o1);
            }
        }
      }
    }
  }
}

int main() {
  // freopen("out.txt","w",stdout);
  // freopen("in.txt","r",stdin);
  int i, j, k, ans;
  while (~scanf("%d%d%d", &w, &h, &n)) {
    tot = 0;
    ans = 0;
    memset(a1, 0, sizeof(a1));
    memset(a2, 0, sizeof(a2));
    for (i = 1; i <= n; i++) {
      scanf("%d%d%d%d", &x, &y, &xx, &yy);
      if (x > xx)
        swap(x, xx);
      if (y > yy)
        swap(y, yy);
      if (x == xx) {
        for (int jj = y; jj < yy; jj++) {
          a1[xx][jj] = 1;
        }
      }
      if (y == yy) {
        for (int ii = x; ii < xx; ii++) {
          a2[ii][yy] = 1;
        }
      }
    }
    scanf("%d%d%d%d", &s.x, &s.y, &t.x, &t.y);
    bfs();
    // cout<<d[t.x][t.y]<<endl;
    wgh o = t;
    int oo = 0;
    while (o.x != s.x || o.y != s.y) {
      oo++;
      aa[oo] = o;
      o = pre[o.x][o.y];
    }
    for (i = 1; i < oo; i++) {
      bfs1(i);
      // printf("%d\\n",dist[t.x][t.y]);
      ans = max(ans, dist[t.x][t.y] - d[t.x][t.y]);
    }
    printf("%d\\n", ans);
  }
  return 0;
}