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

#include <bits/stdc++.h>
#define int long long
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
using namespace std;
static const int INF = 1ll << 60;
typedef pair<int, int> pii;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};

int N, M;
int feld[11][11];
int cnt;
pii last;
int ans;

void dfs(int x, int y, int sum) {
  for (int i = 0; i < 4; ++i) {
    int nx = x;
    int ny = y;
    while (1) {
      nx += dx[i];
      ny += dy[i];
      if (nx < 0 || nx >= N || ny < 0 || ny >= M)
        break;
      if (feld[ny][nx] == 1) {
        feld[ny][nx] = -1;
        dfs(nx, ny, sum + 1);
        feld[ny][nx] = 1;
        break;
      } else if (feld[ny][nx] == 2) {
        if (sum == cnt) {
          ans++;
          return;
        }
      }
    }
  }
  return;
}

signed main() {
  while (1) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    memset(feld, -1, sizeof(feld));
    for (int i = 0; i < M; ++i) {
      for (int j = 0; j < N; ++j) {
        cin >> feld[i][j];
        if (feld[i][j] == 2) {
          last.FI = i;
          last.SE = j;
        } else if (feld[i][j] == 1)
          cnt++;
      }
    }
    dfs(last.SE, last.FI, 0);
    cout << ans << endl;
    ans = 0;
    cnt = 0;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
using namespace std;
static const int INF = 1ll << 60;
typedef pair<int, int> pii;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};

int N, M;
int temp[110][110];
int feld[110][110];
int cnt;
pii last;
int ans;
int t;
vector<pii> vect[110][110];

void dfs(int x, int y, int sum, int pdx, int pdy, bool down) {
  if (x < 0 || x >= N || y < 0 || y >= M)
    return;
  // cout<<x<<" "<<y<<" "<<sum<<endl;
  if (feld[y][x] == 1 && down)
    feld[y][x] = -1;
  if (y == last.FI && x == last.SE && sum == cnt) {
    ans++;
    // cout<<"hoge"<<endl;
    // cout<<ans<<endl;
    return;
  }
  if (down) {
    bool b = false;
    int ptx = -1;
    int pty = -1;
    for (int i = 0; i < vect[y][x].size(); ++i) {
      int nx = vect[y][x][i].FI;
      int ny = vect[y][x][i].SE;
      int tx = vect[y][x][i].FI - x;
      int ty = vect[y][x][i].SE - y;
      if (tx != 0)
        tx /= abs(tx);
      if (ty != 0)
        ty /= abs(ty);
      if (b && ptx == tx && pty == ty)
        continue;
      b = false;
      ptx = tx;
      pty = ty;
      if (feld[ny][nx] == -1) {
        b = true;
        continue;
      }
      if (feld[ny][nx] == 1) {
        dfs(nx, ny, sum + 1, tx, ty, true);
      } else
        dfs(nx, ny, sum, tx, ty, false);
    }
  } else {
    for (int i = 0; i < vect[y][x].size(); ++i) {
      int nx = vect[y][x][i].FI;
      int ny = vect[y][x][i].SE;
      int tx = vect[y][x][i].FI - x;
      int ty = vect[y][x][i].SE - y;
      if (tx != 0)
        tx /= abs(tx);
      if (ty != 0)
        ty /= abs(ty);
      if (pdx == tx && pdy == ty) {
        if (feld[ny][nx] == -1) {
          if (feld[y][x] == -1)
            feld[y][x] = 1;
          return;
        } else if (feld[ny][nx] == 1) {
          dfs(nx, ny, sum + 1, tx, ty, true);
        } else
          dfs(nx, ny, sum, tx, ty, false);
      }
    }
  }
  if (feld[y][x] == -1)
    feld[y][x] = 1;
  return;
}

signed main() {
  while (1) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    memset(feld, 0, sizeof(feld));
    for (int i = 0; i < M; ++i) {
      for (int j = 0; j < N; ++j) {
        cin >> feld[i][j];
        if (feld[i][j] == 2) {
          last.FI = i;
          last.SE = j;
        } else if (feld[i][j] != 0)
          cnt++;
      }
    }

    for (int i = 0; i < M; ++i) {
      for (int j = 0; j < N; ++j) {
        for (int k = 0; k < 4; ++k) {
          for (int l = 1; l < 100; ++l) {
            int nx = dx[k] * l;
            int ny = dy[k] * l;
            if (j + nx < 0 || j + nx >= N || i + ny < 0 || i + ny >= M)
              break;
            if (feld[i + ny][j + nx] != 0) {
              vect[i][j].push_back(pii(j + nx, i + ny));
              // break;
            }
          }
        }
      }
    }
    dfs(last.SE, last.FI, 0, 0, 0, true);
    /*for(int i=0;i<M;++i)for(int j=0;j<N;++j){
        cout<<"["<<i<<" "<<j<<"]"<<endl;
        for(int k=0;k<vect[i][j].size();++k)cout<<vect[i][j][k].FI<<"
    "<<vect[i][j][k].SE<<endl;
    }*/
    cout << ans << endl;
    ans = 0;
    cnt = 0;
    for (int i = 0; i < M; ++i)
      for (int j = 0; j < N; ++j)
        vect[i][j].clear();
  }
}