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

#include <bits/stdc++.h>
#define inf 1 << 30
using namespace std;

class State {
public:
  int h, w, leg, cost;
  State(int a, int b, int c, int d) : h(a), w(b), leg(c), cost(d) {}
  bool operator<(State s) const { return s.cost < cost; }
};

int w, h;
char s[100][100];
int dp[100][100][2] = {};
//            a                    b
int x[2][9] = {{1, 1, 1, 1, 1, 2, 2, 2, 3},
               {-1, -1, -1, -1, -1, -2, -2, -2, -3}};
int y[2][9] = {{-2, -1, 0, 1, 2, -1, 0, 1, 0}, {-2, -1, 0, 1, 2, -1, 0, 1, 0}};

bool in(int a, int b) {
  if (a < 0 || b < 0 || h <= a || w <= b)
    return false;
  return true;
}

int main() {
  while (1) {
    cin >> w >> h;
    if (w + h == 0)
      break;
    priority_queue<State> pq;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> s[i][j];
        dp[i][j][0] = inf;
        dp[i][j][1] = inf;
        if (s[i][j] == 'S') {
          pq.push(State(i, j, 0, 0));
          pq.push(State(i, j, 1, 0));
          dp[i][j][0] = 0;
          dp[i][j][1] = 0;
        }
        if (s[i][j] == 'T')
          s[i][j] = '0';
      }
    }

    int ans = inf;
    while (!pq.empty()) {
      State u = pq.top();
      pq.pop();

      if (dp[u.h][u.w][u.leg] < u.cost)
        continue;

      if (s[u.h][u.w] == '0') {
        ans = min(ans, u.cost);
        continue;
      }

      for (int k = 0; k < 9; k++) {
        int a = u.h + y[u.leg][k], b = u.w + x[u.leg][k], c = (u.leg + 1) % 2;
        if (!in(a, b))
          continue;
        if (s[a][b] == 'X' || s[a][b] == 'S')
          continue;
        if ((u.cost + (s[a][b] - '0')) < dp[a][b][c]) {
          dp[a][b][c] = u.cost + (s[a][b] - '0');
          pq.push(State(a, b, c, dp[a][b][c]));
        }
      }
    }
    if (ans != inf)
      cout << ans << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define inf 1 << 30
using namespace std;

class State {
public:
  int h, w, leg, cost;
  State(int a, int b, int c, int d) : h(a), w(b), leg(c), cost(d) {}
  bool operator<(State s) const { return cost < s.cost; }
};

int w, h;
char s[100][100];
int dp[100][100][2] = {};
//            a                    b
int x[2][9] = {{1, 1, 1, 1, 1, 2, 2, 2, 3},
               {-1, -1, -1, -1, -1, -2, -2, -2, -3}};
int y[2][9] = {{-2, -1, 0, 1, 2, -1, 0, 1, 0}, {-2, -1, 0, 1, 2, -1, 0, 1, 0}};

bool in(int a, int b) {
  if (a < 0 || b < 0 || h <= a || w <= b)
    return false;
  return true;
}

int main() {
  while (1) {
    cin >> w >> h;
    if (w + h == 0)
      break;
    priority_queue<State> pq;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> s[i][j];
        dp[i][j][0] = inf;
        dp[i][j][1] = inf;
        if (s[i][j] == 'S') {
          pq.push(State(i, j, 0, 0));
          pq.push(State(i, j, 1, 0));
          dp[i][j][0] = 0;
          dp[i][j][1] = 0;
        }
        if (s[i][j] == 'T')
          s[i][j] = '0';
      }
    }

    int ans = inf;
    while (!pq.empty()) {
      State u = pq.top();
      pq.pop();

      if (dp[u.h][u.w][u.leg] < u.cost)
        continue;

      if (s[u.h][u.w] == '0') {
        ans = min(ans, u.cost);
        continue;
      }

      for (int k = 0; k < 9; k++) {
        int a = u.h + y[u.leg][k], b = u.w + x[u.leg][k], c = (u.leg + 1) % 2;
        if (!in(a, b))
          continue;
        if (s[a][b] == 'X' || s[a][b] == 'S')
          continue;
        if ((u.cost + (s[a][b] - '0')) < dp[a][b][c]) {
          dp[a][b][c] = u.cost + (s[a][b] - '0');
          pq.push(State(a, b, c, dp[a][b][c]));
        }
      }
    }
    if (ans != inf)
      cout << ans << endl;
    else
      cout << -1 << endl;
  }
  return 0;
}