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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<ll, ll> pll;

const int INF = 1000000;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const double INF_D = 1e18;

int dy[2] = {0, 1};
int dx[2] = {1, 0};

int dp[110][110];
bool ng[2][110][110];
bool used[110][110];

int main() {
  int n, p, t;
  int gx, gy;
  pii p1, p2;

  int x, y, nx, ny;
  cin >> n;
  while (n--) {

    rep(i, 110) rep(j, 110) {
      rep(k, 2) ng[k][i][j] = false;
      dp[i][j] = 0;
      used[i][j] = false;
    }
    cin >> gx >> gy >> p;

    // cout<<" "<<n<<" "<<p<<endl;

    rep(i, p) {
      cin >> p1.fi >> p1.se >> p2.fi >> p2.se;
      if (p1 > p2)
        swap(p1, p2);
      if (p1.fi != p2.fi) {
        ng[0][p1.fi][p1.se] = true;
      } else {
        ng[1][p1.fi][p1.se] = true;
      }
    }

    queue<int> q;
    dp[0][0] = 1;
    q.push(0);
    used[0][0] = true;
    while (!q.empty()) {
      t = q.front();
      q.pop();
      x = t / 100;
      y = t % 100;
      // assert(x < 100 && y < 100);
      if (x > 15 || y > 15)
        continue;
      if (x == gx && y == gy)
        break;
      rep(i, 2) {
        if (ng[i][x][y])
          continue;
        nx = x + dx[i];
        ny = y + dy[i];
        dp[nx][ny] += dp[x][y];
        if (used[nx][ny])
          continue;
        q.push(nx * 100 + ny);
        used[nx][ny] = true;
      }
    }
    if (dp[gx][gy] == 0) {
      cout << "Miserable Hokusai!" << endl;
      continue;
    }
    cout << dp[gx][gy] << endl;
    // cout<<dp[0][0]<<" "<<dp[0][1]<<" "<<dp[1][1]<<endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<ll, ll> pll;

const int INF = 1000000;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const double INF_D = 1e18;

int dy[2] = {0, 1};
int dx[2] = {1, 0};

int dp[110][110];
bool ng[2][110][110];
bool used[110][110];

int main() {
  int n, p, t;
  int gx, gy;
  pii p1, p2;

  int x, y, nx, ny;
  cin >> n;
  while (n--) {

    rep(i, 110) rep(j, 110) {
      rep(k, 2) ng[k][i][j] = false;
      dp[i][j] = 0;
      used[i][j] = false;
    }
    cin >> gx >> gy >> p;

    // cout<<" "<<n<<" "<<p<<endl;

    rep(i, p) {
      cin >> p1.fi >> p1.se >> p2.fi >> p2.se;
      if (p1 > p2)
        swap(p1, p2);
      if (p1.fi != p2.fi) {
        ng[0][p1.fi][p1.se] = true;
      } else {
        ng[1][p1.fi][p1.se] = true;
      }
    }

    queue<int> q;
    dp[0][0] = 1;
    q.push(0);
    used[0][0] = true;
    while (!q.empty()) {
      t = q.front();
      q.pop();
      x = t / 100;
      y = t % 100;
      if (x == gx && y == gy)
        break;
      rep(i, 2) {
        if (ng[i][x][y])
          continue;
        nx = x + dx[i];
        ny = y + dy[i];
        dp[nx][ny] += dp[x][y];
        if (used[nx][ny])
          continue;
        q.push(nx * 100 + ny);
        used[nx][ny] = true;
      }
    }
    if (dp[gx][gy] == 0) {
      cout << "Miserable Hokusai!" << endl;
      continue;
    }
    cout << dp[gx][gy] << endl;
    // cout<<dp[0][0]<<" "<<dp[0][1]<<" "<<dp[1][1]<<endl;
  }
}