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

#include <algorithm>
#include <cmath>
#include <cstdio>
#pragma warning(disable : 4996)
using namespace std;
const int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
char c[8][9];
int place(int x, int y, char t, bool m) {
  int ret = 0;
  for (int i = 0; i < 8; i++) {
    int px = x + dx[i], py = y + dy[i];
    bool flag = false;
    while (0 <= px && px < 8 && 0 <= py && py < 8) {
      if (c[py][px] == '.')
        break;
      if (c[py][px] == t) {
        flag = true;
        break;
      }
      px += dx[i];
      py += dy[i];
    }
    if (flag) {
      ret += max(abs(x - px) - 1, abs(y - py) - 1);
      if (m) {
        int qx = x, qy = y;
        while (qx != px || qy != py) {
          c[qy][qx] = t;
          qx += dx[i];
          qy += dy[i];
        }
      }
    }
  }
  return ret;
}
int main() {
  for (int i = 0; i < 8; i++)
    scanf("%s", &c[i]);
  int t = 0, v = 0;
  while (true) {
    int x = -1, y = -1, r = 0;
    for (int i = 0; i < 8; i++) {
      for (int j = 0; j < 8; j++) {
        if (c[i][j] == '.') {
          int p = place(j, i, t % 2 == 0 ? 'o' : 'x', false);
          if (t % 2 == 1) {
            if (p >= r && p >= 1) {
              x = j;
              y = i;
              r = p;
            }
          } else {
            if (p > r && p >= 1) {
              x = j;
              y = i;
              r = p;
            }
          }
        }
      }
    }
    if (r == 0) {
      if (v == 0) {
        v = 1;
        t++;
        continue;
      } else {
        break;
      }
    } else {
      place(x, y, t++ % 2 == 0 ? 'o' : 'x', true);
      v = 0;
    }
  }
  for (int i = 0; i < 8; i++)
    printf("%s\\n", c[i]);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#pragma warning(disable : 4996)
using namespace std;
const int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
char c[8][9];
int place(int x, int y, char t, bool m) {
  int ret = 0;
  for (int i = 0; i < 8; i++) {
    int px = x + dx[i], py = y + dy[i];
    bool flag = false;
    while (0 <= px && px < 8 && 0 <= py && py < 8) {
      if (c[py][px] == '.')
        break;
      if (c[py][px] == t) {
        flag = true;
        break;
      }
      px += dx[i];
      py += dy[i];
    }
    if (flag) {
      ret += max(abs(x - px), abs(y - py));
      if (m) {
        int qx = x, qy = y;
        while (qx != px && qy != py) {
          c[qx][qy] = t;
          qx += dx[i];
          qy += dy[i];
        }
      }
    }
  }
  return ret;
}
int main() {
  for (int i = 0; i < 8; i++)
    scanf("%s", &c[i]);
  int t = 0;
  while (true) {
    int x = -1, y = -1, r = 0;
    for (int i = 0; i < 8; i++) {
      for (int j = 0; j < 8; j++) {
        if (c[i][j] == '.') {
          int p = place(j, i, t % 2 == 0 ? 'o' : 'x', false);
          if (p >= r && p >= 1) {
            x = j;
            y = i;
            r = p;
          }
        }
      }
    }
    if (r == 0)
      break;
    place(y, x, t++ % 2 == 0 ? 'o' : 'x', true);
  }
  for (int i = 0; i < 8; i++)
    printf("%s\\n", c[i]);
  return 0;
}