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

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 102;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};

int a[MAX_N][MAX_N];
P s, t;
int opt;
int n, m;

void dfs(P p, int cnt, map<P, int> &mp) {
  if (cnt == 10) {
    return;
  }
  cnt++;
  rep(i, 4) {
    P cor = p;
    if (cor.first + dx[i] < 0 || cor.first + dx[i] >= n ||
        cor.second + dy[i] < 0 || cor.second + dy[i] >= m) {
      continue;
    }
    if (mp[P(cor.first + dx[i], cor.second + dy[i])] == 1) {
      continue;
    }
    while (1) {
      cor.first += dx[i], cor.second += dy[i];
      if (cor == t) {
        if (opt > cnt) {
          opt = cnt;
        }
        break;
      }
      if (cor.first < 0 || cor.first >= n || cor.second < 0 ||
          cor.second >= m) {
        break;
      }
      if (mp[cor] == 1) {
        mp[cor] = 0;
        dfs(P(cor.first - dx[i], cor.second - dy[i]), cnt, mp);
        mp[cor] = 1;
        break;
      }
    }
  }
  return;
}

int main() {
  while (1) {
    scanf("%d%d", &m, &n);
    if (n == 0 && m == 0) {
      break;
    }
    map<P, int> mp;
    opt = INF;
    rep(i, n) {
      rep(j, m) {
        scanf("%d", &a[i][j]);
        if (a[i][j] == 1) {
          mp[P(i, j)] = 1;
        }
        if (a[i][j] == 2) {
          s = P(i, j);
        }
        if (a[i][j] == 3) {
          t = P(i, j);
        }
      }
    }
    dfs(s, 0, mp);
    if (opt == INF) {
      printf("-1\\n");
    } else {
      printf("%d\\n", opt);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 102;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};

int a[MAX_N][MAX_N];
P s, t;
int opt;
int n, m;

void dfs(P p, int cnt, map<P, int> mp) {
  if (cnt == 10) {
    return;
  }
  cnt++;
  rep(i, 4) {
    P cor = p;
    if (cor.first + dx[i] < 0 || cor.first + dx[i] >= n ||
        cor.second + dy[i] < 0 || cor.second + dy[i] >= m) {
      continue;
    }
    if (mp[P(cor.first + dx[i], cor.second + dy[i])] == 1) {
      continue;
    }
    while (1) {
      cor.first += dx[i], cor.second += dy[i];
      if (cor == t) {
        if (opt > cnt) {
          opt = cnt;
        }
        break;
      }
      if (cor.first < 0 || cor.first >= n || cor.second < 0 ||
          cor.second >= m) {
        break;
      }
      if (mp[cor] == 1) {
        mp[cor] = 0;
        dfs(P(cor.first - dx[i], cor.second - dy[i]), cnt, mp);
        mp[cor] = 1;
        break;
      }
    }
  }
  return;
}

int main() {
  while (1) {
    scanf("%d%d", &m, &n);
    if (n == 0 && m == 0) {
      break;
    }
    map<P, int> mp_pre;
    opt = INF;
    rep(i, n) {
      rep(j, m) {
        scanf("%d", &a[i][j]);
        if (a[i][j] == 1) {
          mp_pre[P(i, j)] = 1;
        }
        if (a[i][j] == 2) {
          s = P(i, j);
        }
        if (a[i][j] == 3) {
          t = P(i, j);
        }
      }
    }
    dfs(s, 0, mp_pre);
    if (opt == INF) {
      printf("-1\\n");
    } else {
      printf("%d\\n", opt);
    }
  }
  return 0;
}