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

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;

char s[10][10];
int x[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int y[8] = {1, 1, 1, 0, 0, -1, -1, -1};

void witch() {
  int imax = 0, jmax = 0, kmax, maxcount = 0, count1, count2;
  int ax, ay;
  vector<int> vec, maxvec;
  for (int i = 1; i <= 8; i++) {
    for (int j = 1; j <= 8; j++) {
      count2 = 0;
      if (s[i][j] == '.') {
        for (int k = 0; k < 8; k++) {
          ay = i + y[k];
          ax = j + x[k];
          if (s[ay][ax] == 'o') {
            count1 = 1;
            while (1) {
              ay += y[k];
              ax += x[k];
              if (s[ay][ax] == 'o')
                count1++;
              else if (s[ay][ax] == 'x') {
                count2 += count1;
                vec.push_back(k);
                break;
              } else
                break;
            }
          }
        }
        if (count2 >= maxcount) {
          maxcount = count2;
          imax = i;
          jmax = j;
          maxvec.clear();
          copy(vec.begin(), vec.end(), back_inserter(maxvec));
        }
        vec.clear();
      }
    }
  }
  if (maxcount > 0) {
    s[imax][jmax] = 'x';
    for (int i = 0; i < maxvec.size(); i++) {
      ay = imax;
      ax = jmax;
      while (1) {
        ay += y[maxvec[i]];
        ax += x[maxvec[i]];
        if (s[ay][ax] == 'x')
          break;
        else
          s[ay][ax] = 'x';
      }
    }
  }
}

void mami() {
  int imax = 0, jmax = 0, kmax, maxcount = 0, count1, count2;
  int ax, ay;
  vector<int> vec, maxvec;
  for (int i = 8; i >= 1; i--) {
    for (int j = 8; j >= 1; j--) {
      count2 = 0;
      if (s[i][j] == '.') {
        for (int k = 0; k < 8; k++) {
          ay = i + y[k];
          ax = j + x[k];
          if (s[ay][ax] == 'x') {
            count1 = 1;
            while (1) {
              ay += y[k];
              ax += x[k];
              if (s[ay][ax] == 'x')
                count1++;
              else if (s[ay][ax] == 'o') {
                count2 += count1;
                vec.push_back(k);
                break;
              } else
                break;
            }
          }
        }
        if (count2 >= maxcount) {
          maxcount = count2;
          imax = i;
          jmax = j;
          maxvec.clear();
          copy(vec.begin(), vec.end(), back_inserter(maxvec));
        }
        vec.clear();
      }
    }
  }
  if (maxcount > 0) {
    s[imax][jmax] = 'o';
    for (int i = 0; i < maxvec.size(); i++) {
      ay = imax;
      ax = jmax;
      while (1) {
        ay += y[maxvec[i]];
        ax += x[maxvec[i]];
        if (s[ay][ax] == 'o')
          break;
        else
          s[ay][ax] = 'o';
      }
    }
  }
}

void game(int d) {
  int d1 = d, d2;
  while (1) {
    mami();
    witch();
    d2 = 0;
    for (int i = 1; i <= 8; i++) {
      for (int j = 1; j <= 8; j++) {
        if (s[i][j] == '.')
          d2++;
      }
    }
    if (d1 == d2)
      break;
    d1 = d2;
  }
}

int main() {
  int d = 0;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      s[i][j] = ',';
    }
  }
  for (int i = 1; i <= 8; i++) {
    for (int j = 1; j <= 8; j++) {
      cin >> s[i][j];
      if (s[i][j] == '.') {
        d++;
      }
    }
  }

  game(d);

  for (int i = 1; i <= 8; i++) {
    for (int j = 1; j <= 8; j++) {
      cout << s[i][j];
    }
    cout << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;

char s[10][10];
int x[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int y[8] = {1, 1, 1, 0, 0, -1, -1, -1};

bool check() {
  int dcount = 0;
  int ocount = 0;
  int xcount = 0;

  for (int i = 1; i <= 8; i++) {
    for (int j = 1; j <= 8; j++) {
      if (s[i][j] == 'o') {
        ocount++;
      } else if (s[i][j] == 'x') {
        xcount++;
      } else if (s[i][j] == '.') {
        dcount++;
      }
    }
  }

  if (dcount == 0 || ocount == 0 || xcount == 0) {
    return true;
  }
  return false;
}

void witch() {
  int imax = 0, jmax = 0, kmax, maxcount = 0, count1, count2;
  int ax, ay;
  vector<int> vec, maxvec;
  for (int i = 1; i <= 8; i++) {
    for (int j = 1; j <= 8; j++) {
      count2 = 0;
      if (s[i][j] == '.') {
        for (int k = 0; k < 8; k++) {
          ay = i + y[k];
          ax = j + x[k];
          if (s[ay][ax] == 'o') {
            count1 = 1;
            while (1) {
              ay += y[k];
              ax += x[k];
              if (s[ay][ax] == 'o')
                count1++;
              else if (s[ay][ax] == 'x') {
                count2 += count1;
                vec.push_back(k);
                break;
              } else
                break;
            }
          }
        }
        if (count2 >= maxcount) {
          maxcount = count2;
          imax = i;
          jmax = j;
          maxvec.clear();
          copy(vec.begin(), vec.end(), back_inserter(maxvec));
        }
        vec.clear();
      }
    }
  }
  if (maxcount > 0) {
    s[imax][jmax] = 'x';
    for (int i = 0; i < maxvec.size(); i++) {
      ay = imax;
      ax = jmax;
      while (1) {
        ay += y[maxvec[i]];
        ax += x[maxvec[i]];
        if (s[ay][ax] == 'x')
          break;
        else
          s[ay][ax] = 'x';
      }
    }
  }
}

void mami() {
  int imax = 0, jmax = 0, kmax, maxcount = 0, count1, count2;
  int ax, ay;
  vector<int> vec, maxvec;
  for (int i = 8; i >= 1; i--) {
    for (int j = 8; j >= 1; j--) {
      count2 = 0;
      if (s[i][j] == '.') {
        for (int k = 0; k < 8; k++) {
          ay = i + y[k];
          ax = j + x[k];
          if (s[ay][ax] == 'x') {
            count1 = 1;
            while (1) {
              ay += y[k];
              ax += x[k];
              if (s[ay][ax] == 'x')
                count1++;
              else if (s[ay][ax] == 'o') {
                count2 += count1;
                vec.push_back(k);
                break;
              } else
                break;
            }
          }
        }
        if (count2 >= maxcount) {
          maxcount = count2;
          imax = i;
          jmax = j;
          maxvec.clear();
          copy(vec.begin(), vec.end(), back_inserter(maxvec));
        }
        vec.clear();
      }
    }
  }
  if (maxcount > 0) {
    s[imax][jmax] = 'o';
    for (int i = 0; i < maxvec.size(); i++) {
      ay = imax;
      ax = jmax;
      while (1) {
        ay += y[maxvec[i]];
        ax += x[maxvec[i]];
        if (s[ay][ax] == 'o')
          break;
        else
          s[ay][ax] = 'o';
      }
    }
  }
}

void game() {
  while (1) {
    if (check()) {
      break;
    }
    mami();
    if (check()) {
      break;
    }
    witch();
  }
}

int main() {
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      s[i][j] = ',';
    }
  }
  for (int i = 1; i <= 8; i++) {
    for (int j = 1; j <= 8; j++) {
      cin >> s[i][j];
    }
  }

  game();

  for (int i = 1; i <= 8; i++) {
    for (int j = 1; j <= 8; j++) {
      cout << s[i][j];
    }
    cout << endl;
  }

  return 0;
}