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

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  vector<string> S(8);
  for (int i = 0; i < 8; ++i) {
    cin >> S[i];
  }
  bool f = true, g = true, h = true;
  int player = -1, M, m, x, y, s, t, u, v, pass = 0;
  char mark, opponent;
  vector<int> dx, dy;
  dx = {0, 1, -1};
  dy = {0, 1, -1};
  while (f && g && h && pass < 2) {
    f = false;
    g = false;
    h = false;
    M = 0;
    s = -1;
    t = -1;
    if (player > 0) {
      mark = 'x';
      opponent = 'o';
    } else {
      mark = 'o';
      opponent = 'x';
    }
    for (int i = 0; i < 8; ++i) {
      for (int j = 0; j < 8; ++j) {
        if (S[i][j] == 'x')
          f = true;
        else if (S[i][j] == 'o')
          g = true;
        else { // S[i][j] == '.'
          h = true;
          m = 0;
          for (int k = 0; k < 3; ++k) { // 8??????????????????
            for (int l = 0; l < 3; ++l) {
              if (k || l) {
                x = i;
                y = j;
                while (0 <= x + dx[k] && x + dx[k] < 8 && 0 <= y + dy[l] &&
                       y + dy[l] < 8 && S[x + dx[k]][y + dy[l]] == opponent) {
                  x += dx[k];
                  y += dy[l];
                }
                if (0 <= x + dx[k] && x + dx[k] < 8 && 0 <= y + dy[l] &&
                    y + dy[l] < 8 && S[x + dx[k]][y + dy[l]] == mark) {
                  m += max(abs(x - i), abs(y - j));
                }
              }
            }
          }
          if ((m >= M && player > 0) || (m > M && player < 0)) {
            s = i;
            t = j;
            M = m;
          }
        }
      }
    }
    if (M > 0) { //??????????????????????????´?????¶????????´??°
      S[s][t] = mark;
      pass = 0;
      for (int k = 0; k < 3; ++k) { // 8??????????????????
        for (int l = 0; l < 3; ++l) {
          if (k || l) {
            x = s;
            y = t;
            while (0 <= x + dx[k] && x + dx[k] < 8 && 0 <= y + dy[l] &&
                   y + dy[l] < 8 && S[x + dx[k]][y + dy[l]] == opponent) {
              x += dx[k];
              y += dy[l];
            }
            if (0 <= x + dx[k] && x + dx[k] < 8 && 0 <= y + dy[l] &&
                y + dy[l] < 8 && S[x + dx[k]][y + dy[l]] == mark) {
              while (x != s || y != t) {
                S[x][y] = mark;
                x -= dx[k];
                y -= dy[l];
              }
            }
          }
        }
      }
    } else
      ++pass;
    player *= -1;
    //    for (int i = 0; i < 8; ++i) cout << S[i] << endl;
  }
  for (int i = 0; i < 8; ++i)
    cout << S[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  vector<string> S(8);
  for (int i = 0; i < 8; ++i) {
    cin >> S[i];
  }
  bool f = true, g = true, h = true;
  int player = -1, M, m, x, y, s, t, u, v;
  char mark, opponent;
  vector<int> dx, dy;
  dx = {0, 1, -1};
  dy = {0, 1, -1};
  while (f && g && h) {
    f = false;
    g = false;
    h = false;
    M = 0;
    s = -1;
    t = -1;
    if (player > 0) {
      mark = 'x';
      opponent = 'o';
    } else {
      mark = 'o';
      opponent = 'x';
    }
    for (int i = 0; i < 8; ++i) {
      for (int j = 0; j < 8; ++j) {
        if (S[i][j] == 'x')
          f = true;
        else if (S[i][j] == 'o')
          g = true;
        else { // S[i][j] == '.'
          h = true;
          m = 0;
          for (int k = 0; k < 3; ++k) { // 8??????????????????
            for (int l = 0; l < 3; ++l) {
              if (k || l) {
                x = i;
                y = j;
                while (0 <= x + dx[k] && x + dx[k] < 8 && 0 <= y + dy[l] &&
                       y + dy[l] < 8 && S[x + dx[k]][y + dy[l]] == opponent) {
                  x += dx[k];
                  y += dy[l];
                }
                if (0 <= x + dx[k] && x + dx[k] < 8 && 0 <= y + dy[l] &&
                    y + dy[l] < 8 && S[x + dx[k]][y + dy[l]] == mark) {
                  m += max(abs(x - i), abs(y - j));
                }
              }
            }
          }
          if ((m >= M && player > 0) || (m > M && player < 0)) {
            s = i;
            t = j;
            M = m;
          }
        }
      }
    }
    if (M > 0) { //??????????????????????????´?????¶????????´??°
      S[s][t] = mark;
      for (int k = 0; k < 3; ++k) { // 8??????????????????
        for (int l = 0; l < 3; ++l) {
          if (k || l) {
            x = s;
            y = t;
            while (0 <= x + dx[k] && x + dx[k] < 8 && 0 <= y + dy[l] &&
                   y + dy[l] < 8 && S[x + dx[k]][y + dy[l]] == opponent) {
              x += dx[k];
              y += dy[l];
            }
            if (0 <= x + dx[k] && x + dx[k] < 8 && 0 <= y + dy[l] &&
                y + dy[l] < 8 && S[x + dx[k]][y + dy[l]] == mark) {
              while (x != s || y != t) {
                S[x][y] = mark;
                x -= dx[k];
                y -= dy[l];
              }
            }
          }
        }
      }
    }
    player *= -1;
    //    for (int i = 0; i < 8; ++i) cout << S[i] << endl;
  }
  for (int i = 0; i < 8; ++i)
    cout << S[i] << endl;
  return 0;
}