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


#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define pb push_back
typedef long long ll;
typedef complex<int> P;

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

bool inRange(int a, int x, int b) { return (a <= x && x <= b); }

int Check(vector<vector<vector<int>>> &cube, int &n, int &m) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        int p = cube[i][j][k];
        if (p == 0)
          continue;
        //				cout<< i<< " "<< j<< " "<< k<< " "<< p<<
        //endl;
        for (int a = 0; a < 13; a++) {
          int i2 = i + (m - 1) * dx[a];
          int j2 = j + (m - 1) * dy[a];
          int k2 = k + (m - 1) * dz[a];
          if (inRange(0, i2, n - 1) && inRange(0, j2, n - 1) &&
              inRange(0, k2, n - 1)) {
            bool flag = true;
            for (int b = 0; b < m; b++) {
              int q = cube[i + b * dx[a]][j + b * dy[a]][k + b * dz[a]];
              if (q != p) {
                flag = false;
                break;
              }
            }
            if (flag)
              return p;
          }
        }
      }
    }
  }
  return 0;
}

bool solve() {
  int n, m, p;
  cin >> n >> m >> p;
  if (n == 0 && m == 0 && p == 0)
    return false;
  //	cout<< endl; cout<< n<< " "<< m<< " "<< p<< endl;

  vector<vector<vector<int>>> cube(n,
                                   vector<vector<int>>(n, vector<int>(n, 0)));
  int turn = -1;
  int ans1 = 0, ans2 = -1;
  for (int i = 0; i < p; i++) {
    int x, y;
    cin >> x >> y;
    x = x - 1;
    y = y - 1;
    for (int j = 0; j < n; j++) {
      if (cube[j][y][x] == 0) {
        cube[j][y][x] = turn;
        break;
      }
    }

    if (ans1 == 0) {
      ans1 = Check(cube, n, m);
      if (ans1 != 0)
        ans2 = i + 1;
    }
    turn *= -1;
  }

  /*	for(int i=n-1;i>=0;i--){
                  for(int j=0;j<n;j++){
                          for(int k=0;k<n;k++){
                                  cout<< " "<< cube[i][j][k];
                          }
                          cout<< endl;
                  }
                  cout<< endl;
          }
  */
  if (ans1) {
    cout << ((ans1 == 1) ? "White" : "Black") << " " << ans2 << endl;
  } else {
    cout << "Draw" << endl;
  }
  return true;
}

int main() {
  cout.setf(ios::fixed);
  cout.precision(10);

  while (solve())
    ;
  return 0;
}


Error: TLE

Fixed submission:


#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;

#define pb push_back
typedef long long ll;
typedef complex<int> P;

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

bool inRange(int a, int x, int b) { return (a <= x && x <= b); }

int Check(vector<vector<vector<int>>> &cube, int &n, int &m) {
  for (int i = 0; i + m - 1 < n; i++) {
    for (int j = 0; j + m - 1; j++) {
      for (int k = 0; k + m - 1 < n; k++) {
        for (int a = 0; a < 13; a++) {
          int i2 = i + (m - 1) * dx[a];
          int j2 = j + (m - 1) * dy[a];
          int k2 = k + (m - 1) * dz[a];
          if (inRange(0, i2, n - 1) && inRange(0, j2, n - 1) &&
              inRange(0, k2, n - 1)) {
            int p = cube[i][j][k];
            bool flag = true;
            for (int b = 0; b < m; b++) {
              int q = cube[i + b * dx[a]][j + b * dy[a]][k + b * dz[a]];
              if (q != p) {
                flag = false;
                break;
              }
            }
            if (flag)
              return p;
          }
        }
      }
    }
  }
  return 0;
}

bool solve() {
  int n, m, p;
  cin >> n >> m >> p;
  if (n == 0 && m == 0 && p == 0)
    return false;
  //	cout<< endl; cout<< n<< " "<< m<< " "<< p<< endl;

  vector<vector<vector<int>>> cube(n,
                                   vector<vector<int>>(n, vector<int>(n, 0)));
  int turn = -1;
  int ans1 = 0, ans2 = -1;
  for (int i = 0; i < p; i++) {
    int x, y;
    cin >> x >> y;
    x = x - 1;
    y = y - 1;
    for (int j = 0; j < n; j++) {
      if (cube[j][y][x] == 0) {
        cube[j][y][x] = turn;
        break;
      }
    }

    if (ans1 == 0) {
      ans1 = Check(cube, n, m);
      if (ans1 != 0)
        ans2 = i + 1;
    }
    turn *= -1;
  }

  /*	for(int i=n-1;i>=0;i--){
                  for(int j=0;j<n;j++){
                          for(int k=0;k<n;k++){
                                  cout<< " "<< cube[i][j][k];
                          }
                          cout<< endl;
                  }
                  cout<< endl;
          }
  */
  if (ans1) {
    cout << ((ans1 == 1) ? "White" : "Black") << " " << ans2 << endl;
  } else {
    cout << "Draw" << endl;
  }
  return true;
}

int main() {
  cout.setf(ios::fixed);
  cout.precision(10);

  while (solve())
    ;
  return 0;
}