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


#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;

int board[12][12];
int used[12];
int mask[12];
int all = (1 << 10) - 1;

int makeCurrMask(int curr, int m) {
  for (int i = 0; i < 10; i++) {
    if ((m >> i) & 1) {
      int t = (i == 0) ? (7 >> 1) : (7 << (i - 1));
      curr ^= t;
    }
  }
  return curr & all;
}

bool dfs(int prev, int curr, int index) {
  if (index >= 10) {
    if (prev)
      return false;
    return true;
  }
  for (int m = 0; m < (1 << 10); m++) {
    if (index > 0 && prev ^ m)
      continue;
    int ncurr = makeCurrMask(curr, m);
    int next = mask[index + 1] ^ m;

    used[index] = m;
    if (dfs(ncurr, next, index + 1)) {
      return true;
    }
  }
  return false;
}

void solve() {
  memset(mask, 0, sizeof(mask));
  memset(used, 0, sizeof(used));
  for (int y = 0; y < 10; y++) {
    int m = 0;
    for (int x = 0; x < 10; x++) {
      m = ((m << 1) + board[y][x]);
    }
    mask[y] = m;
  }
  dfs(-1, mask[0], 0);
}

void output() {
  for (int y = 0; y < 10; y++) {
    int m = used[y];
    for (int x = 9; x >= 0; x--) {
      if (x != 9)
        cout << " ";
      cout << (((m >> x) & 1) ? 1 : 0);
    }
    cout << endl;
  }
}

int main() {
  int n;
  cin >> n;

  for (int t = 0; t < n; t++) {
    for (int y = 0; y < 10; y++) {
      for (int x = 0; x < 10; x++)
        cin >> board[y][x];
    }
    solve();
    output();
  }
  return 0;
}

Error: TLE

Fixed submission:


#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;

int board[12][12];
int used[12][12];

bool lastCheck() {
  for (int y = 0; y < 10; y++) {
    for (int x = 0; x < 10; x++) {
      if (board[y][x])
        return false;
    }
  }
  return true;
}
/*
bool allow(int n){
        for(int i=0;i<n;i++){
                int y = i / 10;
                int x = i % 10;

                if(board[y][x] == 0)continue;

                bool ok = false;
                for(int j=n;j<100;j++){
                        int ny = j / 10;
                        int nx = j % 10;

                        int dy = abs(ny - y);
                        int dx = abs(nx - x);

                        if(dy >= 2)break;
                        if(dy + dx <= 1){
                                ok = true;
                                break;
                        }
                }

                if(!ok)return false;
        }
        return true;
}
*/

int pattern[] = {0, 1, 0, 1, 1, 1, 0, 1, 0};

void flip(int y, int x) {
  int sy = y - 1;
  int sx = x - 1;
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++) {
      int ny = sy + i;
      int nx = sx + j;
      if (ny < 0 || ny >= 10 || nx < 0 || nx >= 10)
        continue;

      if (pattern[i * 3 + j]) {
        board[ny][nx] ^= 1;
      }
    }
}

bool dfs(int n) {
  if (n >= 10 * 10) {
    return lastCheck();
  }
  /*if(!allow(n)){
          return false;
  }
  */
  int y = n / 10;
  int x = n % 10;

  if (dfs(n + 1)) {
    return true;
  }

  flip(y, x);
  used[y][x] = 1;

  if (dfs(n + 1))
    return true;

  used[y][x] = 0;
  flip(y, x);

  return false;
}

void solve() {
  memset(used, 0, sizeof(used));
  if (!dfs(0)) {
    while (1)
      ;
  }
}
int main() {
  int n;
  cin >> n;

  for (int t = 0; t < n; t++) {
    for (int y = 0; y < 10; y++) {
      for (int x = 0; x < 10; x++)
        cin >> board[y][x];
    }
    solve();
    for (int y = 0; y < 10; y++) {
      for (int x = 0; x < 10; x++) {
        if (x > 0)
          cout << " ";
        cout << used[y][x];
      }
      cout << endl;
    }
  }
  return 0;
}