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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

bool ischecked[61][61][2];
char cliff[61][61];
int tesu[61][61][2];
int w, h;

int dy[2][9] = {{1, 1, 1, 1, 1, 2, 2, 2, 3},
                {-1, -1, -1, -1, -1, -2, -2, -2, -3}};
int dx[9] = {2, 1, 0, -1, -2, 1, 0, -1, 0};

typedef struct Data {
  int x;
  int y;
  int isnowleft;
} d;

int solve(int isleft, int sx, int sy) {
  int ans = 1000000;
  queue<d> qu;
  d tmp;
  tmp.x = sx;
  tmp.y = sy;
  tmp.isnowleft = isleft;
  qu.push(tmp);
  while (!qu.empty()) {
    d top = qu.front();
    qu.pop();
    int x = top.x;
    int y = top.y;
    int isnowleft = top.isnowleft;
    // cout << x << " " << y << " " << tesu[x][y][isnowleft] << " " << isnowleft
    // << endl;
    for (int i = 0; i < 9; i++) {
      int nx = x + dx[i];
      int ny = y + dy[isnowleft][i];
      if (nx < 0 || ny < 0 || nx >= h || ny >= w || cliff[nx][ny] == 'X')
        continue;
      if (cliff[nx][ny] == 'T')
        ans = min(tesu[x][y][isnowleft], ans);
      // else if(cliff[nx][ny] == 'S') solve(!isnowleft, nx, ny, nowtesu);
      else {
        d next;
        next.x = nx;
        next.y = ny;
        next.isnowleft = !isnowleft;
        int nexttesu;
        if (cliff[nx][ny] == 'S')
          nexttesu = tesu[x][y][isnowleft];
        else
          nexttesu = tesu[x][y][isnowleft] + cliff[nx][ny] - '0';
        if (nexttesu < tesu[nx][ny][!isnowleft]) {
          tesu[nx][ny][!isnowleft] = nexttesu;
          // ischecked[nx][ny][!isnowleft] = true;
          qu.push(next);
        }
      }
    }
  }
  return ans;
}

int main() {
  while (1) {
    cin >> w >> h;
    vector<int> sx, sy, tx, ty;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> cliff[i][j];
        if (cliff[i][j] == 'S') {
          sx.push_back(i);
          sy.push_back(j);
        } else if (cliff[i][j] == 'T') {
          tx.push_back(i);
          ty.push_back(j);
        }
      }
    }
    // memset(ischecked, false, sizeof(ischecked));
    memset(tesu, 1000000, sizeof(tesu));
    int ans = 1000000;
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < sx.size(); j++) {
        tesu[sx[j]][sy[j]][i] = 0;
        // ischecked[sx[j]][sy[j]][i] = true;
        ans = min(ans, solve(i, sx[j], sy[j]));
      }
    }
    // for(int k = 0; k < 2; k++){
    //     for(int i = 0; i < h; i++){
    //             for(int j = 0; j < w; j++){
    //                 cout << tesu[i][j][k] << " ";
    //             }
    //             cout << endl;
    //         }
    //         cout << endl;
    //     }
    if (ans == 1000000)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int tesu[61][61][2];
char cliff[61][61];
int w, h;

int dy[2][9] = {{1, 1, 1, 1, 1, 2, 2, 2, 3},
                {-1, -1, -1, -1, -1, -2, -2, -2, -3}};
int dx[9] = {2, 1, 0, -1, -2, 1, 0, -1, 0};

void solve(int isnowleft, int x, int y, int nowtesu) {
  if (nowtesu >= tesu[x][y][isnowleft])
    return;
  tesu[x][y][isnowleft] = nowtesu;
  for (int i = 0; i < 9; i++) {
    int nx = x + dx[i];
    int ny = y + dy[isnowleft][i];
    if (nx < 0 || ny < 0 || nx >= h || ny >= w || cliff[nx][ny] == 'X' ||
        cliff[nx][ny] == 'S')
      continue;
    if (cliff[nx][ny] == 'T')
      tesu[nx][ny][!isnowleft] = min(tesu[nx][ny][!isnowleft], nowtesu);
    // else if(cliff[nx][ny] == 'S') solve(!isnowleft, nx, ny, nowtesu);
    else
      solve(!isnowleft, nx, ny, nowtesu + cliff[nx][ny] - '0');
  }
  return;
}

int main() {
  while (1) {
    cin >> w >> h;
    vector<int> sx, sy, tx, ty;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> cliff[i][j];
        if (cliff[i][j] == 'S') {
          sx.push_back(i);
          sy.push_back(j);
        } else if (cliff[i][j] == 'T') {
          tx.push_back(i);
          ty.push_back(j);
        }
      }
    }
    memset(tesu, 1000000, sizeof(tesu));
    int ans = 1000000;
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < sx.size(); j++) {
        solve(i, sx[j], sy[j], 0);
      }
    }
    // for(int k = 0; k < 2; k++){
    //     for(int i = 0; i < h; i++){
    //             for(int j = 0; j < w; j++){
    //                 cout << tesu[i][j][k] << " ";
    //             }
    //             cout << endl;
    //         }
    //         cout << endl;
    //     }
    for (int i = 0; i < tx.size(); i++) {
      for (int j = 0; j < 2; j++) {
        ans = min(ans, tesu[tx[i]][ty[i]][j]);
      }
    }
    if (ans == 1000000)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}