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

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#define INF 500000
#define P pair<int, pair<int, pair<int, int>>>
#define dis first
#define x second.first
#define y second.second.first
#define sta second.second.second
using namespace std;
string str[60];
int w, h, cnt;
priority_queue<P> Q;
int d[61][31][2];

void dijkstra(int sx, int sy, int rl) {
  bool visited[61][31][2] = {};
  while (!Q.empty())
    Q.pop();
  P ttt;
  ttt.dis = 0, ttt.x = sx, ttt.y = sy, ttt.sta = rl;
  d[sy][sx][rl] = 0;
  Q.push(ttt);

  while (!Q.empty()) {
    cnt++;
    P t = Q.top();
    Q.pop();

    if (visited[t.y][t.x][t.sta] == 1)
      continue;
    visited[t.y][t.x][t.sta] = 1;

    for (int i = 1; i <= 3; i++) {            // i = x
      for (int j = -3 + i; j <= 3 - i; j++) { // j = y
        int nx, ny = t.y + j;
        if (t.sta == 0)
          nx = t.x - i;
        if (t.sta == 1)
          nx = t.x + i;
        if (nx < 0 || ny < 0 || w <= nx || h <= ny)
          continue;
        if (str[ny][nx] == 'X')
          continue;
        int cost = str[ny][nx] - '0';
        if (str[ny][nx] == 'T' || str[ny][nx] == 'S')
          cost = 0;
        if (d[ny][nx][(t.sta + 1) % 2] < d[t.y][t.x][t.sta] - cost) {
          d[ny][nx][(t.sta + 1) % 2] = d[t.y][t.x][t.sta] - cost;
          P tt;
          tt.dis = d[ny][nx][(t.sta + 1) % 2];
          tt.x = nx, tt.y = ny, tt.sta = (t.sta + 1) % 2;
          Q.push(tt);
        }
      }
    }
  }
}

int main() {

  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;

    for (int i = 0; i < h; i++) {
      str[h - i - 1] = "";
      for (int j = 0; j < w; j++) {
        string a;
        cin >> a;
        str[h - i - 1] += a;
      }
    }

    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        for (int k = 0; k < 2; k++)
          d[i][j][k] = -INF;

    for (int i = 0; i < w; i++)
      if (str[0][i] == 'S') {
        dijkstra(i, 0, 0);
        dijkstra(i, 0, 1);
      }

    int ans = -INF;
    for (int i = 0; i < w; i++) {
      if (str[h - 1][i] == 'T')
        ans = max(ans, d[h - 1][i][0]), ans = max(ans, d[h - 1][i][1]);
    }

    if (ans == -INF)
      cout << -1 << endl;
    else
      cout << -ans << endl;
    // cout << cnt <<endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#define INF 1000000000
using namespace std;
int w, h;
string mp[61];
int memo[61][31][61][31];
int visited[61][31][61][31];
int ans;
int cnt = 0;

void check(int rx, int ry, int lx, int ly) {

  for (int i = h - 1; i >= 0; i--) {
    for (int j = 0; j < w; j++) {
      if (i == ry && j == rx)
        cout << '>';
      else if (i == ly && j == lx)
        cout << '<';
      else
        cout << mp[i][j];
    }
    cout << endl;
  }
  cout << endl;
}

void saiki(int rx, int ry, int lx, int ly, int sta, int sum) {
  cnt++;
  sta %= 2;
  // cout << cnt << endl;
  // check(rx,ry,lx,ly);

  if ((mp[ry][rx] == 'T' || mp[ly][lx] == 'T') && ans > sum)
    ans = sum;

  for (int i = 1; i <= 3; i++) {
    for (int j = -2 + i - 1; j <= 2 - i + 1; j++) {
      int rnx = rx, rny = ry, lnx = lx, lny = ly, cost, cost2;
      if (sta == 0)
        lnx = rx - i, lny = ry + j;
      if (sta == 1)
        rnx = lx + i, rny = ly + j;
      if (rnx < 0 || rny < 0 || rnx >= w || rny >= h)
        continue;
      if (lnx < 0 || lny < 0 || lnx >= w || lny >= h)
        continue;
      if (mp[rny][rnx] == 'X' || mp[lny][lnx] == 'X')
        continue;
      if (sta == 0) {
        cost = mp[lny][lnx] - '0';
        cost2 = mp[rny][rnx] - '0';
        if (mp[lny][lnx] == 'S' || mp[lny][lnx] == 'T')
          cost = 0;
        if (mp[rny][rnx] == 'S' || mp[rny][rnx] == 'T')
          cost2 = 0;
        if (mp[rny][rnx] == 'T')
          continue;

      } else if (sta == 1) {
        cost = mp[rny][rnx] - '0';
        cost2 = mp[lny][lnx] - '0';
        if (mp[rny][rnx] == 'S' || mp[rny][rnx] == 'T')
          cost = 0;
        if (mp[lny][lnx] == 'S' || mp[lny][lnx] == 'T')
          cost2 = 0;
        if (mp[lny][lnx] == 'T')
          continue;
      }
      if (visited[rny][rnx][lny][lnx] <= sum + cost)
        continue;
      else
        visited[rny][rnx][lny][lnx] = sum + cost;

      saiki(rnx, rny, lnx, lny, sta + 1, sum + cost);
    }
  }
}

int main() {

  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;

    for (int i = 0; i < h; i++) {
      mp[h - 1 - i] = "";
      for (int j = 0; j < w; j++) {
        string a;
        cin >> a;
        mp[h - 1 - i] += a;
      }
    }

    for (int i = 0; i <= h; i++)
      for (int j = 0; j <= w; j++)
        for (int k = 0; k <= h; k++)
          for (int m = 0; m <= w; m++)
            memo[i][j][k][m] = visited[i][j][k][m] = INF;
    cnt = 0;
    ans = INF;
    for (int i = 0; i < w; i++) {
      if (mp[0][i] == 'S')
        saiki(i, 0, i, 0, 0, 0), saiki(i, 0, i, 0, 1, 0);
    }

    if (ans == INF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }

  return 0;
}