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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, int> P;
const int H = 10;
const int W = 10;

void disp(vector<vector<int>> &v) {
  cout << "----------" << endl;
  REP(i, H) {
    REP(j, W) cout << v[i][j];
    cout << endl;
  }
}

void shoot(int y, int x, vector<vector<int>> &v) {
  v[y][x] = !v[y][x];
  if (y + 1 < H)
    v[y + 1][x] = !v[y + 1][x];
  if (y - 1 > -1)
    v[y - 1][x] = !v[y - 1][x];
  if (x + 1 < W)
    v[y][x + 1] = !v[y][x + 1];
  if (x - 1 > -1)
    v[y][x - 1] = !v[y][x - 1];
}

vector<P> change(vector<vector<int>> &v) {
  vector<P> res;
  FOR(y, 1, v.size()) {
    REP(x, v[y].size()) {
      if (v[y - 1][x] == 1) {
        shoot(y, x, v);
        res.push_back(P(y, x));
      }
    }
  }
  return res;
}

bool check(vector<vector<int>> &v) {
  REP(i, v.size()) REP(j, v[i].size()) if (v[i][j] == 1) return false;
  return true;
}

vector<P> solve() {
  vector<vector<int>> v(H, vector<int>(W));
  REP(i, H) REP(j, W) cin >> v[i][j];
  vector<P> res;
  REP(b, (1 << W)) {
    vector<vector<int>> vv = v;
    vector<P> tmp;
    REP(i, W) {
      if (!(1 & (b >> i)))
        continue;
      shoot(0, i, vv);
      tmp.push_back(P(0, i));
    }
    vector<P> ans = change(vv);
    REP(i, tmp.size()) ans.push_back(tmp[i]);
    if (check(vv)) {
      res = ans;
      break;
    }
  }
  return res;
}

int main() {
  int N;
  cin >> N;
  while (N--) {
    vector<P> r = solve();
    vector<vector<int>> ans(H, vector<int>(W, 0));
    REP(i, r.size()) ans[r[i].first][r[i].second] = 1;
    REP(i, H) {
      REP(j, W) cout << (j != 0 ? " " : "") << ans[i][j];
      cout << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < j; ++i)
#define FOR(i, j, k) for (int i = j; i < k; ++i)
#define P pair<int, int>
const int H = 10;
const int W = 10;
typedef vector<vector<int>> ivv;
typedef vector<int> iv;

int my[5] = {1, -1, 0, 0, 0};
int mx[5] = {0, 0, 1, -1, 0};

class C {
public:
  ivv u;
  int t, y, x;
  C() {}
  C(ivv _u, int _t, int _y, int _x) {
    u = _u;
    t = _t;
    y = _y;
    x = _x;
  }
};

queue<C> open;

void rev(ivv &v, int y, int x) {
  REP(i, 5) {
    int ny = y + my[i], nx = x + mx[i];
    if (ny >= 0 && nx >= 0 && ny < H && nx < W)
      v[ny][nx] = (v[ny][nx] + 1) % 2;
  }
}

bool next(ivv v, ivv &u, int y, int x) {
  REP(i, H) REP(j, W) if (u[i][j]) rev(v, i, j);
  int a = 0, b = 0;
  REP(i, 5) {
    int ny = y + my[i], nx = x + mx[i];
    if (ny >= 0 && nx >= 0 && ny < H && nx < W) {
      if (v[ny][nx])
        ++a;
      else
        ++b;
    }
  }
  return a > b;
}

bool end(ivv v, ivv &u) {
  REP(i, H) REP(j, W) if (u[i][j]) rev(v, i, j);
  REP(i, H) REP(j, W) if (v[i][j] == 1) return false;
  return true;
}

ivv bfs(ivv &v) {
  open = queue<C>();
  ivv _u(H, iv(W, 0));
  open.push(C(_u, 0, 0, -1));
  while (!open.empty()) {
    C tmp = open.front();
    open.pop();
    ivv u = tmp.u;
    int t = tmp.t;
    if (end(v, u))
      return u;
    FOR(y, tmp.y, H) {
      REP(x, W) {
        if (y == tmp.y && x <= tmp.x)
          continue;
        if (!u[y][x] && next(v, u, y, x)) {
          rev(v, y, x);
          u[y][x] = true;
          open.push(C(u, t + 1, y, x));
          u[y][x] = false;
          rev(v, y, x);
        }
      }
    }
  }
  return ivv();
}

int main() {
  int T;
  cin >> T;
  REP(t, T) {
    ivv v(H, iv(W));
    REP(i, H) REP(j, W) cin >> v[i][j];
    ivv ans = bfs(v);
    REP(i, H) {
      REP(j, W) cout << ans[i][j] << (j + 1 == W ? "" : " ");
      cout << endl;
    }
  }
  return 0;
}