Problem p00737 (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;

#define mod 1000000007

int w, h;
int s[31][31];
int c[4];

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int solve() {
  int cost[31][31][4];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      for (int k = 0; k < 4; k++) {
        cost[i][j][k] = mod;
      }
    }
  }
  queue<pair<pair<int, int>, int>> qu;
  cost[0][0][0] = 0;
  qu.push(make_pair(make_pair(0, 0), 0));
  while (!qu.empty()) {
    int x = ((qu.front()).first).first;
    int y = ((qu.front()).first).second;
    int d = (qu.front()).second;
    // (x, y)?????????????????????d (dx, dy??¨??????)
    qu.pop();
    // cout << x << " " << y << " " << d << ":" << endl;
    for (int i = 0; i < 4; i++) {
      // i : ???????????????
      int nc = cost[x][y][d];
      int nd = (d + i) % 4;
      int nx = x + dx[nd];
      int ny = y + dy[nd];
      if (nx < 0 || ny < 0 || nx >= h || ny >= w)
        continue;
      if (i != s[x][y])
        nc += c[i];
      if (cost[nx][ny][nd] > nc) {
        cost[nx][ny][nd] = nc;
        qu.push(make_pair(make_pair(nx, ny), nd));
        // cout << nx << " " << ny << " " << nd << " " << nc << endl;
      }
    }
  }
  int ans = mod;
  for (int i = 0; i < 4; i++) {
    ans = min(ans, cost[h - 1][w - 1][i]);
  }
  return ans;
}

int main() {
  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 < 4; i++) {
      cin >> c[i];
    }
    cout << solve() << 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 w, h;
int s[31][31];
int c[4];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool ischecked[31][31][4];
// N : 2 S : 0 E: 1 W : 3

bool solve(int x, int y, int d, int cost, int maxcost) {
  if (cost > maxcost)
    return false;
  // cout << x << y << d << cost << endl;
  if (x == h - 1 && y == w - 1)
    return true;
  for (int i = 0; i < 4; i++) {
    int nx, ny;
    int newd = d;
    if (i == 1) {
      newd += 3;
    } else if (i == 2) {
      newd += 2;
    } else if (i == 3) {
      newd += 1;
    }
    newd = newd % 4;
    nx = x + dx[newd];
    ny = y + dy[newd];
    if (nx < 0 || ny < 0 || nx >= h || ny >= w || ischecked[nx][ny][newd])
      continue;
    ischecked[nx][ny][newd] = true;
    if ((i == s[x][y] && solve(nx, ny, newd, cost, maxcost)) ||
        solve(nx, ny, newd, cost + c[i], maxcost)) {
      ischecked[nx][ny][newd] = false;
      return true;
    }
    ischecked[nx][ny][newd] = false;
  }
  return false;
  // cout << x << y << d << " " << mincost[x][y][d] << endl;
  // if(mincost[x][y][d] == 1000000) mincost[x][y][d] = 1000001;
  // return mincost[x][y][d];
}

int main() {
  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 k = 0; k < 4; k++) {
          ischecked[i][j][k] = false;
        }
      }
    }
    for (int i = 0; i < 4; i++) {
      cin >> c[i];
    }
    ischecked[0][0][1] = true;
    int left = 0, right = 9 * 31 * 31;
    int mid;
    while (right - left > 1) {
      mid = (left + right) / 2;
      // cout << mid << endl;
      if (solve(0, 0, 1, 0, mid))
        right = mid;
      else
        left = mid;
    }
    cout << right << endl;
  }
  return 0;
}