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

#include <bits/stdc++.h>
#include <regex>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 1145141919;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

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

vector<P> search(char c) {
  vector<P> v;
  int mx = 1;
  REP(i, 8) REP(j, 8) {
    if (fld[i][j] != '.')
      continue;
    int cnt = 0;
    REP(k, 8) {
      int tmp = 0;
      int nx = i;
      int ny = j;
      while (1) {
        nx = nx + dx[k];
        ny = ny + dy[k];
        if (nx < 0 || ny < 0 || nx > 7 || ny > 7 || fld[nx][ny] == '.')
          break;
        else if (fld[nx][ny] != c) {
          tmp++;
          continue;
        } else {
          cnt += tmp;
          break;
        }
      }
    }
    if (cnt > mx) {
      v.clear();
      v.push_back(P(i, j));
      mx = cnt;
    } else if (cnt == mx) {
      v.push_back(P(i, j));
    }
  }
  return v;
}

bool endf() {
  vector<P> a, b;
  a = search('x');
  b = search('o');
  if (a.size() == 0 && b.size() == 0)
    return false;
  else
    return true;
}

void majo() {
  vector<P> v = search('x');
  int x, y;
  if (v.size() > 0) {
    x = v.back().first;
    y = v.back().second;
    fld[x][y] = 'x';
    REP(k, 8) {
      vector<P> w;
      int nx = x;
      int ny = y;
      while (1) {
        nx = nx + dx[k];
        ny = ny + dy[k];
        if (nx < 0 || ny < 0 || nx > 7 || ny > 7 || fld[nx][ny] == '.')
          break;
        else if (fld[nx][ny] == 'o') {
          w.push_back(P(nx, ny));
          continue;
        } else {
          REP(i, w.size()) { fld[w[i].first][w[i].second] = 'x'; }
          break;
        }
      }
    }
  }
}

void mami() {
  vector<P> v = search('o');
  int x, y;
  if (v.size() > 0) {
    x = v[0].first;
    y = v[0].second;
    fld[x][y] = 'o';
    REP(k, 8) {
      vector<P> w;
      int nx = x;
      int ny = y;
      while (1) {
        nx = nx + dx[k];
        ny = ny + dy[k];
        if (nx < 0 || ny < 0 || nx > 7 || ny > 7 || fld[nx][ny] == '.')
          break;
        else if (fld[nx][ny] == 'x') {
          w.push_back(P(nx, ny));
          continue;
        } else {
          REP(i, w.size()) { fld[w[i].first][w[i].second] = 'o'; }
          break;
        }
      }
    }
  }
}

int main() {
  REP(i, 8) cin >> fld[i];
  for (int i = 0; endf(); i++) {
    if (i % 2)
      majo();
    else
      mami();
  }
  REP(i, 8) cout << fld[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <regex>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 1145141919;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

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

bool endf() {
  int a = 0;
  int b = 0;
  int c = 0;
  REP(i, 8) REP(j, 8) {
    if (fld[i][j] == 'o')
      a++;
    else if (fld[i][j] == 'x')
      b++;
    else
      c++;
  }
  if (a == 0 || b == 0 || c == 0)
    return false;
  else
    return true;
}

vector<P> search(char c) {
  vector<P> v;
  int mx = 1;
  REP(i, 8) REP(j, 8) {
    if (fld[i][j] != '.')
      continue;
    int cnt = 0;
    REP(k, 8) {
      int tmp = 0;
      int nx = i;
      int ny = j;
      while (1) {
        nx = nx + dx[k];
        ny = ny + dy[k];
        if (nx < 0 || ny < 0 || nx > 7 || ny > 7 || fld[nx][ny] == '.')
          break;
        else if (fld[nx][ny] != c) {
          tmp++;
          continue;
        } else {
          cnt += tmp;
          break;
        }
      }
    }
    if (cnt > mx) {
      v.clear();
      v.push_back(P(i, j));
      mx = cnt;
    } else if (cnt == mx) {
      v.push_back(P(i, j));
    }
  }
  return v;
}

void majo() {
  vector<P> v = search('x');
  int x, y;
  if (v.size() > 0) {
    x = v.back().first;
    y = v.back().second;
    fld[x][y] = 'x';
    REP(k, 8) {
      vector<P> w;
      int nx = x;
      int ny = y;
      while (1) {
        nx = nx + dx[k];
        ny = ny + dy[k];
        if (nx < 0 || ny < 0 || nx > 7 || ny > 7 || fld[nx][ny] == '.')
          break;
        else if (fld[nx][ny] == 'o') {
          w.push_back(P(nx, ny));
          continue;
        } else {
          REP(i, w.size()) { fld[w[i].first][w[i].second] = 'x'; }
          break;
        }
      }
    }
  }
}

void mami() {
  vector<P> v = search('o');
  int x, y;
  if (v.size() > 0) {
    x = v[0].first;
    y = v[0].second;
    fld[x][y] = 'o';
    REP(k, 8) {
      vector<P> w;
      int nx = x;
      int ny = y;
      while (1) {
        nx = nx + dx[k];
        ny = ny + dy[k];
        if (nx < 0 || ny < 0 || nx > 7 || ny > 7 || fld[nx][ny] == '.')
          break;
        else if (fld[nx][ny] == 'x') {
          w.push_back(P(nx, ny));
          continue;
        } else {
          REP(i, w.size()) { fld[w[i].first][w[i].second] = 'o'; }
          break;
        }
      }
    }
  }
}

int main() {
  REP(i, 8) cin >> fld[i];
  for (int i = 0; endf(); i++) {
    if (i % 2)
      majo();
    else
      mami();
  }
  REP(i, 8) cout << fld[i] << endl;
  return 0;
}