Problem p01428 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

string s[10];

int cnt[10][10];

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

bool check() {
  bool f[3] = {};
  REP(i, 8) {
    if (s[i].find('o') != -1)
      f[0] = 1;
    if (s[i].find('x') != -1)
      f[1] = 1;
    if (s[i].find('.') != -1)
      f[2] = 1;
  }
  return f[0] && f[1] && f[2];
}

int count(int y, int x, int d, bool t) {
  int res = 0;
  bool f = 0;
  x += dx[d];
  y += dy[d];
  while (x >= 0 && x < 8 && y >= 0 && y < 8) {
    if (s[y][x] == (t ? 'o' : 'x')) {
      f = 1;
      break;
    } else {
      if (t) {
        if (s[y][x] == 'x')
          res++;
        else
          break;
      } else {
        if (s[y][x] == 'o')
          res++;
        else
          break;
      }
    }
    x += dx[d];
    y += dy[d];
  }
  if (f)
    return res;
  return 0;
}

void print() {
  REP(i, 8) {
    REP(j, 8) { cout << s[i][j]; }
    cout << endl;
  }
}

int main() {
  REP(i, 8) cin >> s[i];
  bool f = 1;
  int err = 0;
  while (1) {
    REP(i, 10) REP(j, 10) cnt[i][j] = 0;
    if (!check())
      break;
    int ma = 0;
    P pos = {-1, -1};
    REP(i, 8) {
      REP(j, 8) {
        int ny, nx;
        if (f)
          ny = i, nx = j;
        else
          ny = 7 - i, nx = 7 - j;
        if (s[ny][nx] == '.') {
          int w = 0;
          REP(k, 8) w += count(ny, nx, k, f);
          if (ma < w) {
            ma = w;
            pos = {ny, nx};
          }
        }
      }
    }
    if (ma != 0) {
      err = 0;
      REP(i, 8) {
        int y = pos.first, x = pos.second;
        int w = count(y, x, i, f);
        REP(j, w + 1) {
          int ny = pos.first + dy[i] * j, nx = pos.second + dx[i] * j;
          // cout << "!" << ny << " " << nx << endl;
          if (f)
            s[ny][nx] = 'o';
          else
            s[ny][nx] = 'x';
        }
      }
    } else
      err++;
    if (err >= 2)
      break;
    // print();
    // cout << endl;
    f = !f;
  }
  print();
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

string s[10];

int cnt[10][10];

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

bool check() {
  bool f[3] = {};
  REP(i, 8) {
    if (s[i].find('o') != -1)
      f[0] = 1;
    if (s[i].find('x') != -1)
      f[1] = 1;
    if (s[i].find('.') != -1)
      f[2] = 1;
  }
  return f[0] && f[1] && f[2];
}

int count(int y, int x, int d, bool t) {
  int res = 0;
  bool f = 0;
  x += dx[d];
  y += dy[d];
  while (x >= 0 && x < 8 && y >= 0 && y < 8) {
    if (s[y][x] == (t ? 'o' : 'x')) {
      f = 1;
      break;
    } else {
      if (t) {
        if (s[y][x] == 'x')
          res++;
        else
          break;
      } else {
        if (s[y][x] == 'o')
          res++;
        else
          break;
      }
    }
    x += dx[d];
    y += dy[d];
  }
  if (f)
    return res;
  return 0;
}

void print() {
  REP(i, 8) {
    REP(j, 8) { cout << s[i][j]; }
    cout << endl;
  }
}

int main() {
  REP(i, 8) cin >> s[i];
  bool f = 1;
  while (1) {
    REP(i, 10) REP(j, 10) cnt[i][j] = 0;
    if (!check())
      break;
    int ma = 0;
    P pos = {-1, -1};
    REP(i, 8) {
      REP(j, 8) {
        int ny, nx;
        if (f)
          ny = i, nx = j;
        else
          ny = 7 - i, nx = 7 - j;
        if (s[ny][nx] == '.') {
          int w = 0;
          REP(k, 8) w += count(ny, nx, k, f);
          if (ma < w) {
            ma = w;
            pos = {ny, nx};
          }
        }
      }
    }
    if (ma != 0) {
      REP(i, 8) {
        int y = pos.first, x = pos.second;
        int w = count(y, x, i, f);
        REP(j, w + 1) {
          int ny = pos.first + dy[i] * j, nx = pos.second + dx[i] * j;
          // cout << "!" << ny << " " << nx << endl;
          if (f)
            s[ny][nx] = 'o';
          else
            s[ny][nx] = 'x';
        }
      }
    }
    // print();
    // cout << endl;
    f = !f;
  }
  print();
}