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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i, n) for (i = 0; i < (n); i++)
#define loop(i, a, n) for (i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;

int v[20][20];
int ans;
int h, w;
int gx, gy;

void dfs(int x, int y, int t) {
  // cout << "! " << x << " " << y << " " << t << endl;
  if (t > 10)
    return;
  if (x == gx && y == gy) {
    ans = min(ans, t);
    return;
  }
  if (x == gx) {
    int i = y;
    int tmp = 1;
    if (y < gy && v[x][y + 1] != 1) {
      for (i = y; i < gy; i++) {
        if (v[x][i] == 1)
          tmp++;
      }
    } else if (y > gy && v[x][y - 1] != 1) {
      for (i = y; i > gy; i--) {
        if (v[x][i] == 1)
          tmp++;
      }
    }
    if (i == gy) {
      ans = min(ans, t + tmp);
      return;
    }
  }
  if (y == gy) {
    int i = x;
    int tmp = 1;
    if (x < gx && v[x + 1][y] != 1) {
      for (i = x; i < gx; i++) {
        if (v[i][y] == 1)
          tmp++;
      }
    } else if (x > gx && v[x - 1][y] != 1) {
      for (i = x; i > gx; i--) {
        if (v[i][y] == 1)
          tmp++;
      }
    }
    if (i == gx) {
      ans = min(ans, t + tmp);
      return;
    }
  }

  for (int i = x; i < h; i++) {
    if (v[i][y] == 1) {
      if (x + 1 == i)
        break;
      v[i][y] = 0;
      // cout << i << " " << y << 0 <<" " << endl;
      dfs(i - 1, y, t + 1);
      // cout << i << " " << y << 1 <<" " << endl;
      v[i][y] = 1;
      break;
    }
  }

  for (int i = x; i >= 0; i--) {
    if (v[i][y] == 1) {
      if (x - 1 == i)
        break;
      v[i][y] = 0;
      // cout << i << " " << y << 0 <<" " << endl;
      dfs(i + 1, y, t + 1);
      // cout << i << " " << y << 1 <<" " << endl;
      v[i][y] = 1;
      break;
    }
  }

  for (int i = y; i < w; i++) {
    if (v[x][i] == 1) {
      if (y + 1 == i)
        break;
      v[x][i] = 0;
      // cout << x << " " << i << 0 <<" " << endl;
      dfs(x, i - 1, t + 1);
      v[x][i] = 1;
      // cout << x << " " << i << 1 <<" " << endl;
      break;
    }
  }

  for (int i = y; i >= 0; i--) {
    if (v[x][i] == 1) {
      if (y - 1 == i)
        break;
      v[x][i] = 0;
      // cout << x << " " << i << 0 <<" " << endl;
      dfs(x, i + 1, t + 1);
      v[x][i] = 1;
      // cout << x << " " << i << 1 <<" " << endl;
      break;
    }
  }
}

int main(void) {
  int i, j;
  while (cin >> w >> h, h) {
    int sx, sy;
    rep(i, h) rep(j, w) {
      cin >> v[i][j];
      if (v[i][j] == 2) {
        sx = i;
        sy = j;
      }
      if (v[i][j] == 3) {
        gx = i;
        gy = j;
      }
    }
    ans = INF;
    dfs(sx, sy, 0);
    if (ans <= 10) {
      cout << ans << endl;
    } else {
      cout << -1 << endl;
    }
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i, n) for (i = 0; i < (n); i++)
#define loop(i, a, n) for (i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;

int v[20][20];
int ans;
int h, w;
int gx, gy;

void dfs(int x, int y, int t) {

  if (x == gx && y == gy) {
    ans = min(ans, t);
    return;
  }
  if (x == gx) {
    int i = y;
    int tmp = 1;
    if (y < gy && v[x][y + 1] != 1) {
      for (i = y; i < gy; i++) {
        if (v[x][i] == 1)
          tmp++;
      }
    } else if (y > gy && v[x][y - 1] != 1) {
      for (i = y; i > gy; i--) {
        if (v[x][i] == 1)
          tmp++;
      }
    }
    if (i == gy) {
      ans = min(ans, t + tmp);
      return;
    }
  }
  if (y == gy) {
    int i = x;
    int tmp = 1;
    if (x < gx && v[x + 1][y] != 1) {
      for (i = x; i < gx; i++) {
        if (v[i][y] == 1)
          tmp++;
      }
    } else if (x > gx && v[x - 1][y] != 1) {
      for (i = x; i > gx; i--) {
        if (v[i][y] == 1)
          tmp++;
      }
    }
    if (i == gx) {
      ans = min(ans, t + tmp);
      return;
    }
  }

  for (int i = x; i < h; i++) {
    if (v[i][y] == 1) {
      if (x + 1 == i)
        break;
      v[i][y] = 0;
      dfs(i - 1, y, t + 1);
      v[i][y] = 1;
      break;
    }
  }

  for (int i = x; i >= 0; i--) {
    if (v[i][y] == 1) {
      if (x - 1 == i)
        break;
      v[i][y] = 0;
      dfs(i + 1, y, t + 1);
      v[i][y] = 1;
      break;
    }
  }

  for (int i = y; i < w; i++) {
    if (v[x][i] == 1) {
      if (y + 1 == i)
        break;
      v[x][i] = 0;
      dfs(x, i - 1, t + 1);
      v[x][i] = 1;
      break;
    }
  }

  for (int i = y; i >= 0; i--) {
    if (v[x][i] == 1) {
      if (y - 1 == i)
        break;
      v[x][i] = 0;
      dfs(x, i + 1, t + 1);
      v[x][i] = 1;
      break;
    }
  }
}

int main(void) {
  int i, j;
  while (cin >> h >> w, h) {
    int sx, sy;
    rep(i, h) rep(j, w) {
      cin >> v[i][j];
      if (v[i][j] == 2) {
        sx = i;
        sy = j;
      }
      if (v[i][j] == 3) {
        gx = i;
        gy = j;
      }
    }
    ans = INF;
    dfs(sx, sy, 0);
    if (ans <= 10) {
      cout << ans << endl;
    } else {
      cout << -1 << endl;
    }
  }
}