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

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;
const ll INF = 1e18;

ll dp[65][35][3];
int main() {
  int w, h;
  int dx[9] = {1, 1, 2, 1, 2, 3, 1, 2, 1},
      dy[9] = {-2, -1, -1, 0, 0, 0, 1, 1, 2};
  char s[65][35];
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> s[i][j];
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        for (int k = 0; k < 2; k++) {
          dp[i][j][k] = INF;
        }
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (s[i][j] == 'S') {
          dp[i][j][0] = dp[i][j][1] = 0LL;
        }
      }
    }
    auto isOutOfRange = [](int x, int y, int h, int w) {
      return x < 0 || x >= w || y < 0 || y >= h;
    };
    auto cost = [&](int x, int y) {
      if (s[y][x] == 'X')
        return INF;
      if (s[y][x] == 'S')
        return INF;
      if (s[y][x] == 'T')
        return 0LL;
      return (ll)s[y][x] - '0';
    };
    while (1) {
      bool f = false;
      for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
          for (int now = 0; now < 2; now++) {
            if (dp[y][x][now] == INF)
              continue;
            for (int i = 0; i < 9; i++) {
              int nx = x + (now == 0 ? dx[i] : -dx[i]), ny = y + dy[i],
                  nxt = (now + 1) % 2;
              if (isOutOfRange(nx, ny, h, w) || s[ny][nx] == 'X')
                continue;
              // dp[ny][nx][nxt] = min(dp[ny][nx][nxt], dp[y][x][now] + cost(nx,
              // ny));
              if (dp[ny][nx][nxt] > dp[y][x][now] + cost(nx, ny)) {
                dp[ny][nx][nxt] = dp[y][x][now] + cost(nx, ny);
                f = true;
              }
            }
          }
        }
      }
      if (f == false)
        break;
    }
    /*for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                    cout << "(" << (dp[i][j][0]==INF?-1:dp[i][j][0]) << ", " <<
    (dp[i][j][1]==INF?-1:dp[i][j][1])<< ")";
            }
            cout << endl;
    }*/

    ll ans = INF;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (s[i][j] != 'T')
          continue;
        ans = min({ans, dp[i][j][0], dp[i][j][1]});
      }
    }
    if (ans == INF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;
const ll INF = 1e18;

ll dp[65][35][3];
int main() {
  int w, h;
  int dx[9] = {1, 1, 2, 1, 2, 3, 1, 2, 1},
      dy[9] = {-2, -1, -1, 0, 0, 0, 1, 1, 2};
  char s[65][35];
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> s[i][j];
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        for (int k = 0; k < 2; k++) {
          dp[i][j][k] = INF;
        }
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (s[i][j] == 'S') {
          dp[i][j][0] = dp[i][j][1] = 0LL;
        }
      }
    }
    auto isOutOfRange = [](int x, int y, int h, int w) {
      return x < 0 || x >= w || y < 0 || y >= h;
    };
    auto cost = [&](int x, int y) {
      if (s[y][x] == 'X')
        return INF;
      if (s[y][x] == 'S')
        return INF;
      if (s[y][x] == 'T')
        return 0LL;
      return (ll)s[y][x] - '0';
    };
    for (int _ = 0; _ < 1800; _++) {
      for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
          for (int now = 0; now < 2; now++) {
            if (dp[y][x][now] == INF)
              continue;
            for (int i = 0; i < 9; i++) {
              int nx = x + (now == 0 ? dx[i] : -dx[i]), ny = y + dy[i],
                  nxt = (now + 1) % 2;
              if (isOutOfRange(nx, ny, h, w) || s[ny][nx] == 'X')
                continue;
              dp[ny][nx][nxt] =
                  min(dp[ny][nx][nxt], dp[y][x][now] + cost(nx, ny));
            }
          }
        }
      }
    }
    /*for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                    cout << "(" << (dp[i][j][0]==INF?-1:dp[i][j][0]) << ", " <<
    (dp[i][j][1]==INF?-1:dp[i][j][1])<< ")";
            }
            cout << endl;
    }*/

    ll ans = INF;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (s[i][j] != 'T')
          continue;
        ans = min({ans, dp[i][j][0], dp[i][j][1]});
      }
    }
    if (ans == INF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
}