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

#include <algorithm>
#include <iostream>
using namespace std;
int map[8][8];

int val(int y, int x) {
  int dx[] = {1, 1, 1, 0, 0, -1, -1, -1};
  int dy[] = {1, 0, -1, 1, -1, 1, 0, -1};
  int ans = 0;
  for (int i = 0; i < 8; i++) {
    int cnt = 0, a = y, b = x;
    while (true) {
      a += dy[i], b += dx[i];
      if (a >= 0 && a < 8 && b >= 0 && b < 8) {
        if (map[a][b] == 1)
          cnt++;
        if (map[a][b] == 0) {
          ans += cnt;
          break;
        }
        if (map[a][b] == 2)
          break;
      } else
        break;
    }
  }
  return ans;
}

void ch(int y, int x) {
  int dx[] = {1, 1, 1, 0, 0, -1, -1, -1};
  int dy[] = {1, 0, -1, 1, -1, 1, 0, -1};
  for (int i = 0; i < 8; i++) {
    int a = y, b = x;
    bool ok = false;
    while (true) {
      a += dy[i], b += dx[i];
      if (a >= 0 && a < 8 && b >= 0 && b < 8) {
        if (map[a][b] == 0) {
          ok = true;
          break;
        }
        if (map[a][b] == 2)
          break;
      } else
        break;
    }
    if (ok) {
      while (true) {
        if (a == y && b == x)
          break;
        a -= dy[i], b -= dx[i];
        map[a][b] = 0;
      }
    }
  }
}

int main() {
  for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; j++) {
      char z;
      cin >> z;
      if (z == 'o')
        map[i][j] = 0;
      if (z == 'x')
        map[i][j] = 1;
      if (z == '.')
        map[i][j] = 2;
    }
  }
  int t = 0;
  for (int u = 0; u < 130; u++) {
    if (t == 0) {
      int x = -1, y = -1, z = 0;
      for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
          if (map[i][j] == 2) {
            int k = val(i, j);
            if (k > z)
              x = j, y = i, z = k;
          }
        }
      }
      if (z)
        ch(y, x);
    }
    if (t == 1) {
      int x = -1, y = -1, z = 0;
      for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
          if (map[i][j] == 2) {
            int k = val(i, j);
            if (k >= z)
              x = j, y = i, z = k;
          }
        }
      }
      if (z)
        ch(y, x);
    }
    for (int i = 0; i < 8; i++) {
      for (int j = 0; j < 8; j++) {
        if (map[i][j] == 0)
          map[i][j] = 3;
        if (map[i][j] == 1)
          map[i][j] = 0;
        if (map[i][j] == 3)
          map[i][j] = 1;
      }
    }
    t = 1 - t;
  }

  if (t) {
    for (int i = 0; i < 8; i++) {
      for (int j = 0; j < 8; j++) {
        if (map[i][j] == 0)
          map[i][j] = 3;
        if (map[i][j] == 1)
          map[i][j] = 0;
        if (map[i][j] == 3)
          map[i][j] = 1;
      }
    }
  }
  for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; j++) {
      if (map[i][j] == 0)
        cout << "o";
      if (map[i][j] == 1)
        cout << "x";
      if (map[i][j] == 2)
        cout << ".";
    }
    cout << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int map[8][8];

int val(int y, int x) {
  int dx[] = {1, 1, 1, 0, 0, -1, -1, -1};
  int dy[] = {1, 0, -1, 1, -1, 1, 0, -1};
  int ans = 0;
  for (int i = 0; i < 8; i++) {
    int cnt = 0, a = y, b = x;
    while (true) {
      a += dy[i], b += dx[i];
      if (a >= 0 && a < 8 && b >= 0 && b < 8) {
        if (map[a][b] == 1)
          cnt++;
        if (map[a][b] == 0) {
          ans += cnt;
          break;
        }
        if (map[a][b] == 2)
          break;
      } else
        break;
    }
  }
  return ans;
}

void ch(int y, int x) {
  int dx[] = {1, 1, 1, 0, 0, -1, -1, -1};
  int dy[] = {1, 0, -1, 1, -1, 1, 0, -1};
  for (int i = 0; i < 8; i++) {
    int a = y, b = x;
    bool ok = false;
    while (true) {
      a += dy[i], b += dx[i];
      if (a >= 0 && a < 8 && b >= 0 && b < 8) {
        if (map[a][b] == 0) {
          ok = true;
          break;
        }
        if (map[a][b] == 2)
          break;
      } else
        break;
    }
    if (ok) {
      while (true) {
        if (a == y && b == x)
          break;
        a -= dy[i], b -= dx[i];
        map[a][b] = 0;
      }
    }
  }
}

int main() {
  for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; j++) {
      char z;
      cin >> z;
      if (z == 'o')
        map[i][j] = 0;
      if (z == 'x')
        map[i][j] = 1;
      if (z == '.')
        map[i][j] = 2;
    }
  }
  int t = 0, d = 130;
  while (d--) {
    if (t == 0) {
      int x = -1, y = -1, z = 0;
      for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
          if (map[i][j] == 2) {
            int k = val(i, j);
            if (k > z)
              x = j, y = i, z = k;
          }
        }
      }
      ch(y, x);
    }
    if (t == 1) {
      int x = -1, y = -1, z = 0;
      for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
          if (map[i][j] == 2) {
            int k = val(i, j);
            if (k >= z)
              x = j, y = i, z = k;
          }
        }
      }
      ch(y, x);
    }
    for (int i = 0; i < 8; i++) {
      for (int j = 0; j < 8; j++) {
        if (map[i][j] == 0)
          map[i][j] = 3;
        if (map[i][j] == 1)
          map[i][j] = 0;
        if (map[i][j] == 3)
          map[i][j] = 1;
      }
    }
    t = 1 - t;
  }

  if (t) {
    for (int i = 0; i < 8; i++) {
      for (int j = 0; j < 8; j++) {
        if (map[i][j] == 0)
          map[i][j] = 3;
        if (map[i][j] == 1)
          map[i][j] = 0;
        if (map[i][j] == 3)
          map[i][j] = 1;
      }
    }
  }
  for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; j++) {
      if (map[i][j] == 0)
        cout << "o";
      if (map[i][j] == 1)
        cout << "x";
      if (map[i][j] == 2)
        cout << ".";
    }
    cout << endl;
  }
  return 0;
}