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

#include <bits/stdc++.h>
#define N 101
using namespace std;
int h, w, A;
string mp[101];
int dx[] = {0, 0, 1, -1}; // V^><
int dy[] = {1, -1, 0, 0};

void pr(int x, int y, int dir) {
  char ch[] = {'V', '^', '>', '<'};
  char tmp = mp[y][x];
  mp[y][x] = ch[dir];
  for (int i = 0; i < h; i++)
    cout << mp[i] << endl;
  mp[y][x] = tmp;
  cout << "------" << endl;
}

int ans = 1e9, used[101][101][10][10][4];
void dfs(int x, int y, int p, int q, int dir) {
  if (2 * A - (p + q) >= ans)
    return;
  int &u = used[x][y][p][q][dir];
  if (u != -1 && u <= 2 * A - (p + q))
    return;
  u = 2 * A - (p + q);

  vector<int> X, Y, ch;
  while (1) {
    x += dx[dir];
    y += dy[dir];
    if (x < 0 || y < 0 || x >= w || y >= h || mp[y][x] == '#')
      break;
    if (mp[y][x] == 'S')
      continue;
    if (mp[y][x] == 'G')
      ans = min(ans, 2 * A - (p + q));
    X.push_back(x);
    Y.push_back(y);
    ch.push_back(mp[y][x]);
    mp[y][x] = '#';
    if (p) {
      if (dir == 0)
        dfs(x, y, p - 1, q, 2); //>
      if (dir == 1)
        dfs(x, y, p - 1, q, 3); //<
      if (dir == 2)
        dfs(x, y, p - 1, q, 0); // V
      if (dir == 3)
        dfs(x, y, p - 1, q, 1); //^
    }
    if (q) {
      if (dir == 0)
        dfs(x, y, p, q - 1, 3); //>
      if (dir == 1)
        dfs(x, y, p, q - 1, 2); //<
      if (dir == 2)
        dfs(x, y, p, q - 1, 1); //^
      if (dir == 3)
        dfs(x, y, p, q - 1, 0); // V
    }
  }
  for (int i = 0; i < X.size(); i++)
    mp[Y[i]][X[i]] = ch[i];
}

int main() {
  cin >> h >> w >> A;
  for (int i = 0; i < h; i++)
    cin >> mp[i];

  memset(used, -1, sizeof(used));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (mp[i][j] == 'S')
        dfs(j, i, A, A, 0);

  cout << (ans == 1e9 ? -1 : ans) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define N 101
using namespace std;
int h, w, A;
string mp[101];
int dx[] = {0, 0, 1, -1}; // V^><
int dy[] = {1, -1, 0, 0};

void pr(int x, int y, int dir) {
  char ch[] = {'V', '^', '>', '<'};
  char tmp = mp[y][x];
  mp[y][x] = ch[dir];
  for (int i = 0; i < h; i++)
    cout << mp[i] << endl;
  mp[y][x] = tmp;
  cout << "------" << endl;
}

int ans = 1e9; //,used[101][101][10][10][4];
int used[101][101][2];
void dfs(int x, int y, int p, int q, int dir) {
  if (2 * A - (p + q) >= ans)
    return;
  // if(used[x][y][p][q][dir]++)return;

  vector<int> X, Y, ch;
  while (1) {
    x += dx[dir];
    y += dy[dir];
    if (x < 0 || y < 0 || x >= w || y >= h || mp[y][x] == '#')
      break;
    if (mp[y][x] == 'S')
      continue;
    if (mp[y][x] == 'G')
      ans = min(ans, 2 * A - (p + q));
    X.push_back(x);
    Y.push_back(y);
    ch.push_back(mp[y][x]);
    mp[y][x] = '#';
    if (p) {
      if (dir == 0)
        dfs(x, y, p - 1, q, 2); //>
      if (dir == 1)
        dfs(x, y, p - 1, q, 3); //<
      if (dir == 2)
        dfs(x, y, p - 1, q, 0); // V
      if (dir == 3)
        dfs(x, y, p - 1, q, 1); //^
    }
    if (q) {
      if (dir == 0)
        dfs(x, y, p, q - 1, 3); //>
      if (dir == 1)
        dfs(x, y, p, q - 1, 2); //<
      if (dir == 2)
        dfs(x, y, p, q - 1, 1); //^
      if (dir == 3)
        dfs(x, y, p, q - 1, 0); // V
    }
  }
  for (int i = 0; i < X.size(); i++)
    mp[Y[i]][X[i]] = ch[i];
}

int main() {
  cin >> h >> w >> A;
  for (int i = 0; i < h; i++)
    cin >> mp[i];

  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (mp[i][j] == 'S')
        dfs(j, i, A, A, 0);

  cout << (ans == 1e9 ? -1 : ans) << endl;
  return 0;
}