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

#include <algorithm>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>

#define FOR(k, m, n) for (int(k) = (m); (k) < (n); (k)++)
#define REP(i, n) FOR((i), 0, (n))
#define LL long long
#define CLR(a) memset((a), 0, sizeof(a))
#define SZ(x) (int((x).size()))
#define WAITING(str)                                                           \\
  int str;                                                                     \\
  std::cin >> str;
#define DEBUGING(str) cout << str << endl
using namespace std;

const LL MOD = 1000000007; // 10^9+7
const int INF = (1 << 30);

// 変数
const int siz = 8;
vector<string> s(siz);

// サブ関数
// 入力
void input() {
  REP(i, siz) { cin >> s[i]; }
}

bool isOut(int h, int w) {
  if (h < 0 || w < 0)
    return true;
  if (7 < h || 7 < w)
    return true;
  return false;
}

int evaluate(bool mami, int h, int w) {
  char me = (mami ? 'o' : 'x');
  if (s[h][w] != '.')
    return 0;

  int sumValue = 0;
  int dy[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
  int dx[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
  REP(dir, 8) {
    int y = h;
    int x = w;
    int value = 0;
    while (true) {
      y += dy[dir];
      x += dx[dir];
      if (isOut(y, x))
        break;
      if (s[y][x] == '.')
        break;

      if (s[y][x] != me)
        value++;
      else {
        sumValue += value;
        break;
      }
    }
  }
  return sumValue;
}

void put_cookie(bool mami, int h, int w) {
  char me = (mami ? 'o' : 'x');

  int dy[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
  int dx[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
  REP(dir, 8) {
    int y = h;
    int x = w;
    bool change = false;

    while (true) {
      y += dy[dir];
      x += dx[dir];
      if (isOut(y, x))
        break;
      if (s[y][x] == '.')
        break;
      if (s[y][x] == me) {
        change = true;
        break;
      }
    }
    if (change) {
      y = h;
      x = w;
      do {
        s[y][x] = me;
        y += dy[dir];
        x += dx[dir];
      } while (s[y][x] != me);
    }
  }
}

bool search(bool mami) {
  int maxH = 0;
  int maxW = 0;
  int maxV = 0;
  REP(i, siz) REP(j, siz) {
    int tmpV = evaluate(mami, i, j);
    if (mami) {
      if (maxV < tmpV) {
        maxH = i;
        maxW = j;
        maxV = tmpV;
      }
    } else {
      if (maxV <= tmpV) {
        maxH = i;
        maxW = j;
        maxV = tmpV;
      }
    }
  }
  if (maxV) {
    put_cookie(mami, maxH, maxW);
    return true;
  } else {
    return false;
  }
}

// 計算
void calc() {
  bool mami = true;
  int cnt = 0;
  while (cnt < 2) {
    if (search(mami))
      cnt = 0;
    else
      cnt++;

    mami = !mami;
  }
}

// 出力
void output() {
  // cerr << endl << endl;
  for (auto row : s) {
    cout << row << endl;
  }
}

// デバッグ
void debug() {
  int N;
  cin >> N;
}

// メイン関数
int main() {
  input();
  calc();
  output();
  // debug();

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>

#define FOR(k, m, n) for (int(k) = (m); (k) < (n); (k)++)
#define REP(i, n) FOR((i), 0, (n))
#define LL long long
#define CLR(a) memset((a), 0, sizeof(a))
#define SZ(x) (int((x).size()))
#define WAITING(str)                                                           \\
  int str;                                                                     \\
  std::cin >> str;
#define DEBUGING(str) cout << str << endl
using namespace std;

const LL MOD = 1000000007; // 10^9+7
const int INF = (1 << 30);

// 変数
const int siz = 8;
vector<string> s(siz);

// サブ関数
// 入力
void input() {
  REP(i, siz) { cin >> s[i]; }
}

bool isOut(int h, int w) {
  if (h < 0 || w < 0)
    return true;
  if (7 < h || 7 < w)
    return true;
  return false;
}

int evaluate(bool mami, int h, int w) {
  char me = (mami ? 'o' : 'x');
  if (s[h][w] != '.')
    return 0;

  int sumValue = 0;
  int dy[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
  int dx[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
  REP(dir, 8) {
    int y = h;
    int x = w;
    int value = 0;
    while (true) {
      y += dy[dir];
      x += dx[dir];
      if (isOut(y, x))
        break;
      if (s[y][x] == '.')
        break;

      if (s[y][x] != me)
        value++;
      else {
        sumValue += value;
        break;
      }
    }
  }
  return sumValue;
}

void put_cookie(bool mami, int h, int w) {
  char me = (mami ? 'o' : 'x');

  int dy[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
  int dx[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
  REP(dir, 8) {
    int y = h;
    int x = w;
    bool change = false;

    while (true) {
      y += dy[dir];
      x += dx[dir];
      if (isOut(y, x))
        break;
      if (s[y][x] == '.')
        break;
      if (s[y][x] == me) {
        change = true;
        break;
      }
    }
    if (change) {
      y = h;
      x = w;
      do {
        s[y][x] = me;
        y += dy[dir];
        x += dx[dir];
      } while (s[y][x] != me);
    }
  }
}

bool search(bool mami) {
  int maxH = 0;
  int maxW = 0;
  int maxV = 0;
  REP(i, siz) REP(j, siz) {
    int tmpV = evaluate(mami, i, j);
    if (mami) {
      if (maxV < tmpV) {
        maxH = i;
        maxW = j;
        maxV = tmpV;
      }
    } else {
      if (maxV <= tmpV) {
        maxH = i;
        maxW = j;
        maxV = tmpV;
      }
    }
  }
  if (maxV) {
    put_cookie(mami, maxH, maxW);
    return true;
  } else {
    return false;
  }
}

// 計算
void calc() {
  bool mami = true;
  int cnt = 0;
  while (cnt < 2) {
    if (search(mami))
      cnt = 0;
    else
      cnt++;

    mami = !mami;
  }
}

// 出力
void output() {
  cerr << endl << endl;
  for (auto row : s) {
    cout << row << endl;
  }
}

// デバッグ
void debug() {
  int N;
  cin >> N;
}

// メイン関数
int main() {
  input();
  calc();
  output();
  debug();

  return 0;
}