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

#include <algorithm>
#include <queue>
#include <set>
#include <stdio.h>
#include <time.h>
using namespace std;
int c[11][11];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int v[11][11];
struct wolf {
  int t[10];
  int ar, ac, ad;
  int br, bc, bd;
  wolf() {
    for (int i = 0; i < 10; i++)
      t[i] = 0;
  }
};
int n, m;
inline bool operator<(const wolf &a, const wolf &b) {
  for (int i = 0; i < n; i++)
    if (a.t[i] != b.t[i])
      return a.t[i] < b.t[i];
  if (a.ar != b.ar)
    return a.ar < b.ar;
  if (a.ac != b.ac)
    return a.ac < b.ac;
  if (a.br != b.br)
    return a.br < b.br;
  if (a.bc != b.bc)
    return a.bc < b.bc;
  if (a.ad != b.ad)
    return a.ad < b.ad;
  if (a.bd != b.bd)
    return a.bd < b.bd;
  return false;
}
int sz = 0;
set<wolf> S;
bool dame = false;
clock_t start;
int solve(wolf a) {
  if (S.count(a))
    return 0;
  S.insert(a);
  if ((clock() - start) * 2 > CLOCKS_PER_SEC)
    return 0;
  bool ok = true;

  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (c[i][j] && !(a.t[i] & (3 << (j * 2))))
        ok = false;
  if (ok)
    return 1;
  /*for(int i=0;i<n;i++)for(int j=0;j<m;j++)v[i][j]=0;
  queue<pair<int,int> >Q;
  v[a.ar][a.ac]=1;
  Q.push(make_pair(a.ar,a.ac));
  while(Q.size()){
          int row=Q.front().first;int col=Q.front().second;Q.pop();
          for(int i=0;i<4;i++){
                  if(0<=row+dx[i]&&row+dx[i]<n&&0<=col+dy[i]&&col+dy[i]<m&&
                  c[row+dx[i]][col+dy[i]]&&!(a.t[row+dx[i]]&(2<<((col+dy[i])*2)))&&
                  !v[row+dx[i]][col+dy[i]]){
                          v[row+dx[i]][col+dy[i]]=1;
                          Q.push(make_pair(row+dx[i],col+dy[i]));
                  }
          }
  }
  int tmp=0;
  for(int i=0;i<n;i++)for(int j=0;j<m;j++)if(v[i][j])tmp++;
  if(tmp<sz/2)return 0;
  for(int i=0;i<n;i++)for(int j=0;j<m;j++)v[i][j]=0;
  v[a.br][a.bc]=1;
  Q.push(make_pair(a.br,a.bc));
  while(Q.size()){
          int row=Q.front().first;int col=Q.front().second;Q.pop();
          for(int i=0;i<4;i++){
                  if(0<=row+dx[i]&&row+dx[i]<n&&0<=col+dy[i]&&col+dy[i]<m&&
                  c[row+dx[i]][col+dy[i]]&&!(a.t[row+dx[i]]&(1<<((col+dy[i])*2)))&&
                  !v[row+dx[i]][col+dy[i]]){
                          v[row+dx[i]][col+dy[i]]=1;
                          Q.push(make_pair(row+dx[i],col+dy[i]));
                  }
          }
  }
  tmp=0;
  for(int i=0;i<n;i++)for(int j=0;j<m;j++)if(v[i][j])tmp++;
  if(tmp<sz/2)return 0;
  */
  // move
  ok = true;
  wolf to = a;
  if (0 <= to.ar + dx[to.ad / 2] && to.ar + dx[to.ad / 2] < n &&
      0 <= to.ac + dy[to.ad / 2] && to.ac + dy[to.ad / 2] < m &&
      0 <= to.br + dx[to.bd / 2] && to.br + dx[to.bd / 2] < n &&
      0 <= to.bc + dy[to.bd / 2] && to.bc + dy[to.bd / 2] < m &&
      !(to.t[to.ar + dx[to.ad / 2]] & (2 << ((to.ac + dy[to.ad / 2]) * 2))) &&
      !(to.t[to.br + dx[to.bd / 2]] & (1 << ((to.bc + dy[to.bd / 2]) * 2))) &&
      c[to.ar + dx[to.ad / 2]][to.ac + dy[to.ad / 2]] &&
      c[to.br + dx[to.bd / 2]][to.bc + dy[to.bd / 2]] &&
      (to.ar + dx[to.ad / 2] != to.br + dx[to.bd / 2] ||
       to.ac + dy[to.ad / 2] != to.bc + dy[to.bd / 2])) {
    to.t[to.ar + dx[to.ad / 2]] |= (1 << ((to.ac + dy[to.ad / 2]) * 2));
    to.t[to.br + dx[to.bd / 2]] |= (2 << ((to.bc + dy[to.bd / 2]) * 2));
    to.ar += dx[to.ad / 2];
    to.ac += dy[to.ad / 2];
    to.br += dx[to.bd / 2];
    to.bc += dy[to.bd / 2];
    if (solve(to))
      return 1;
  }
  // turn
  to = a;
  to.ad = (to.ad + 2) % 8;
  if (to.bd % 2)
    to.bd = (to.bd + 6) % 8;
  else
    to.bd = (to.bd + 2) % 8;
  if (solve(to))
    return 1;
  return 0;
}
int main() {
  int a, b;
  while (scanf("%d%d", &b, &a), a) {
    start = clock();
    n = a;
    m = b;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++)
        scanf("%d", &c[i][j]);
    int sr = -1;
    int sc = -1;
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; j++) {
        if (c[i][j] == 1) {
          sr = i;
          sc = j;
          break;
        }
      }
      if (~sr)
        break;
    }
    sz = 0;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++)
        if (c[i][j] == 1)
          sz++;
    if (sz % 2) {
      printf("NO\\n");
      continue;
    }
    int ret = 0;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++) {
        if (ret)
          break;
        if (c[i][j] == 1) {
          if (sr == i && sc == j)
            continue;
          for (int k = 0; k < 8; k++) {
            if (ret)
              break;
            S.clear();
            wolf at;
            dame = false;
            at.ar = sr;
            at.ac = sc;
            at.br = i;
            at.bc = j;
            at.ad = 0;
            at.bd = k;
            at.t[sr] = 1 << (sc * 2);
            at.t[i] = 2 << (j * 2);
            for (int x = 0; x < a; x++)
              for (int y = 0; y < b; y++) {
                if (c[x][y]) {
                  int r1, r2, c1, c2;
                  int dr = x - sr;
                  int dc = y - sc;
                  if (k % 2)
                    dc = -dc;
                  for (int w = 0; w < k / 2; w++) {
                    int td = dr;
                    dr = -dc;
                    dc = td;
                  }
                  r1 = i + dr;
                  c1 = j + dc;
                  dr = x - i;
                  dc = y - j;
                  if (k % 2) {
                    if (k / 2 % 2 == 0)
                      dc = -dc;
                    else
                      dr = -dr;
                  }
                  for (int w = 0; w < k / 2; w++) {
                    int td = dr;
                    dr = dc;
                    dc = -td;
                  }
                  r2 = sr + dr;
                  c2 = sc + dc;
                  if (!c[r1][c1] && !c[r2][c2])
                    dame = true;
                }
              }
            if (dame)
              continue;
            int res = solve(at);
            if (res)
              ret = 1;
          }
        }
      }
    if (ret)
      printf("YES\\n");
    else
      printf("NO\\n");
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <queue>
#include <set>
#include <stdio.h>
#include <time.h>
using namespace std;
int c[11][11];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int v[11][11];
struct wolf {
  int t[10];
  int ar, ac, ad;
  int br, bc, bd;
  wolf() {
    for (int i = 0; i < 10; i++)
      t[i] = 0;
  }
};
int n, m;
inline bool operator<(const wolf &a, const wolf &b) {
  for (int i = 0; i < n; i++)
    if (a.t[i] != b.t[i])
      return a.t[i] < b.t[i];
  if (a.ar != b.ar)
    return a.ar < b.ar;
  if (a.ac != b.ac)
    return a.ac < b.ac;
  if (a.br != b.br)
    return a.br < b.br;
  if (a.bc != b.bc)
    return a.bc < b.bc;
  if (a.ad != b.ad)
    return a.ad < b.ad;
  if (a.bd != b.bd)
    return a.bd < b.bd;
  return false;
}
int sz = 0;
set<wolf> S;
clock_t start;
int solve(wolf a) {
  if (S.count(a))
    return 0;
  S.insert(a);
  if ((clock() - start) * 3 > CLOCKS_PER_SEC)
    return 0;
  bool ok = true;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (c[i][j] && !(a.t[i] & (3 << (j * 2))))
        ok = false;
  if (ok)
    return 1;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      v[i][j] = 0;
  queue<pair<int, int>> Q;
  v[a.ar][a.ac] = 1;
  Q.push(make_pair(a.ar, a.ac));
  while (Q.size()) {
    int row = Q.front().first;
    int col = Q.front().second;
    Q.pop();
    for (int i = 0; i < 4; i++) {
      if (0 <= row + dx[i] && row + dx[i] < n && 0 <= col + dy[i] &&
          col + dy[i] < m && c[row + dx[i]][col + dy[i]] &&
          !(a.t[row + dx[i]] & (2 << ((col + dy[i]) * 2))) &&
          !v[row + dx[i]][col + dy[i]]) {
        v[row + dx[i]][col + dy[i]] = 1;
        Q.push(make_pair(row + dx[i], col + dy[i]));
      }
    }
  }
  int tmp = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (v[i][j])
        tmp++;
  if (tmp < sz / 2)
    return 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      v[i][j] = 0;
  v[a.br][a.bc] = 1;
  Q.push(make_pair(a.br, a.bc));
  while (Q.size()) {
    int row = Q.front().first;
    int col = Q.front().second;
    Q.pop();
    for (int i = 0; i < 4; i++) {
      if (0 <= row + dx[i] && row + dx[i] < n && 0 <= col + dy[i] &&
          col + dy[i] < m && c[row + dx[i]][col + dy[i]] &&
          !(a.t[row + dx[i]] & (1 << ((col + dy[i]) * 2))) &&
          !v[row + dx[i]][col + dy[i]]) {
        v[row + dx[i]][col + dy[i]] = 1;
        Q.push(make_pair(row + dx[i], col + dy[i]));
      }
    }
  }
  tmp = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (v[i][j])
        tmp++;
  if (tmp < sz / 2)
    return 0;

  // move
  ok = true;
  wolf to = a;
  if (0 <= to.ar + dx[to.ad / 2] && to.ar + dx[to.ad / 2] < n &&
      0 <= to.ac + dy[to.ad / 2] && to.ac + dy[to.ad / 2] < m &&
      0 <= to.br + dx[to.bd / 2] && to.br + dx[to.bd / 2] < n &&
      0 <= to.bc + dy[to.bd / 2] && to.bc + dy[to.bd / 2] < m &&
      !(to.t[to.ar + dx[to.ad / 2]] & (2 << ((to.ac + dy[to.ad / 2]) * 2))) &&
      !(to.t[to.br + dx[to.bd / 2]] & (1 << ((to.bc + dy[to.bd / 2]) * 2))) &&
      c[to.ar + dx[to.ad / 2]][to.ac + dy[to.ad / 2]] &&
      c[to.br + dx[to.bd / 2]][to.bc + dy[to.bd / 2]] &&
      (to.ar + dx[to.ad / 2] != to.br + dx[to.bd / 2] ||
       to.ac + dy[to.ad / 2] != to.bc + dy[to.bd / 2])) {
    to.t[to.ar + dx[to.ad / 2]] |= (1 << ((to.ac + dy[to.ad / 2]) * 2));
    to.t[to.br + dx[to.bd / 2]] |= (2 << ((to.bc + dy[to.bd / 2]) * 2));
    to.ar += dx[to.ad / 2];
    to.ac += dy[to.ad / 2];
    to.br += dx[to.bd / 2];
    to.bc += dy[to.bd / 2];
    if (solve(to))
      return 1;
  }
  // turn
  to = a;
  to.ad = (to.ad + 2) % 8;
  if (to.bd % 2)
    to.bd = (to.bd + 6) % 8;
  else
    to.bd = (to.bd + 2) % 8;
  if (solve(to))
    return 1;
  return 0;
}
int main() {
  int a, b;
  while (scanf("%d%d", &b, &a), a) {
    start = clock();
    n = a;
    m = b;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++)
        scanf("%d", &c[i][j]);
    int sr = -1;
    int sc = -1;
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; j++) {
        if (c[i][j] == 1) {
          sr = i;
          sc = j;
          break;
        }
      }
      if (~sr)
        break;
    }
    sz = 0;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++)
        if (c[i][j] == 1)
          sz++;
    if (sz % 2) {
      printf("NO\\n");
      continue;
    }
    int ret = 0;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++) {
        if (ret)
          break;
        if (c[i][j] == 1) {
          if (sr == i && sc == j)
            continue;
          for (int k = 0; k < 8; k++) {
            if (ret)
              break;
            S.clear();
            wolf at;
            at.ar = sr;
            at.ac = sc;
            at.br = i;
            at.bc = j;
            at.ad = 0;
            at.bd = k;
            at.t[sr] = 1 << (sc * 2);
            at.t[i] = 2 << (j * 2);
            int res = solve(at);
            if (res)
              ret = 1;
          }
        }
      }
    if (ret)
      printf("YES\\n");
    else
      printf("NO\\n");
  }
}