alexjercan/bugnet huggingface dataset:
Problem p00735 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 1)
      break;
    vector<int> v;
    vector<bool> w(n + 1, true);
    int d = 6;
    bool bAdd2 = true;
    while (d <= n) {
      if (w[d]) {
        for (int i = d; i <= n; i += d) {
          w[i] = false;
        }
        if (n % d == 0)
          v.push_back(d);
      }
      if (bAdd2) {
        d += 2;
      } else {
        d += 5;
      }
      bAdd2 = !bAdd2;
    }
    cout << n << ":";
    for (int i = 0; i < v.size(); i++) {
      cout << " " << v[i];
    }
    cout << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 1)
      break;
    vector<int> v;
    vector<int> w;
    int d = 6;
    bool bAdd2 = true;
    while (d <= n) {
      bool bfind = false;
      for (int i = 0; i < w.size(); i++) {
        if (d % w[i] == 0) {
          bfind = true;
          break;
        }
      }
      if (!bfind) {
        w.push_back(d);
        if (n % d == 0)
          v.push_back(d);
      }
      if (bAdd2) {
        d += 2;
      } else {
        d += 5;
      }
      bAdd2 = !bAdd2;
    }
    cout << n << ":";
    for (int i = 0; i < v.size(); i++) {
      cout << " " << v[i];
    }
    cout << endl;
  }
  return 0;
}
Problem p00735 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
static int prime[300000 + 1];
static int primeLength = 0;
void setPrime() {
  prime[primeLength] = 7 * 0 + 6;
  primeLength++;
  for (int i = 1; 7 * i + 6 <= 300000; i++) {
    bool flag = false;
    for (int j = 0; j < primeLength; j++) {
      if ((i * 7 + 1) % prime[j] == 0) {
        flag = true;
        break;
      }
    }
    if (!flag)
      prime[primeLength++] = i * 7 + 1;
    flag = false;
    for (int j = 0; j < primeLength; j++) {
      if ((i * 7 + 6) % prime[j] == 0) {
        flag = true;
        break;
      }
    }
    if (!flag)
      prime[primeLength++] = i * 7 + 6;
  }
}
int main() {
  setPrime();
  int n;
  while (cin >> n && n != 1) {
    int ansList[150000];
    int ansCount = 0;
    int i = 0;
    while (i < primeLength && prime[i] <= n) {
      if (n % prime[i] == 0) {
        ansList[ansCount++] = prime[i];
      }
      i++;
    }
    cout << n << ":";
    for (int i = 0; i < ansCount; i++) {
      cout << " " << ansList[i];
    }

    cout << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
static int saturdayNum[300000 + 1];
static int numLength = 0;
static int prime[300000 + 1];
static int primeLength = 0;
void setNumber() {
  saturdayNum[numLength] = 7 * 0 + 6;
  numLength = 1;
  for (int i = 1; 7 * i + 6 <= 300000; i++) {
    saturdayNum[numLength++] = i * 7 + 1;
    saturdayNum[numLength++] = i * 7 + 6;
  }
}
void setPrime() {
  for (int i = 0; i < numLength; i++) {
    bool flag = false;
    for (int j = 0; saturdayNum[j] < saturdayNum[i]; j++) {
      if (saturdayNum[i] % saturdayNum[j] == 0) {
        flag = true;
        break;
      }
    }
    if (!flag)
      prime[primeLength++] = saturdayNum[i];
  }
}
int main() {
  setNumber();
  setPrime();
  int n;
  while (cin >> n && n != 1) {
    int ansList[150000];
    int ansCount = 0;
    int i = 0;
    while (i < primeLength && prime[i] <= n) {
      if (n % prime[i] == 0) {
        ansList[ansCount++] = prime[i];
      }
      i++;
    }
    cout << n << ":";
    for (int i = 0; i < ansCount; i++) {
      cout << " " << ansList[i];
    }

    cout << endl;
  }
  return 0;
}
Problem p00735 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef complex<double> P;
static const double EPS = 1e-8;
static const int INF = (int)1e8;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOREQ(i, k, n) for (int i = (k); i <= (int)(n); ++i)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define FIND(m, w) ((m).find(w) != (m).end())
bool msnum(int n) { return (n % 7 == 1 || n % 7 == 6); }
int main(void) {
  vector<int> ms;
  vector<int> msp;
  FOR(i, 2, 300000) if (msnum(i)) ms.push_back(i);
  bool msunp[300000] = {};
  REP(i, ms.size()) {
    if (!msunp[ms[i]]) {
      msp.push_back(ms[i]);
      for (int k = 2; ms[i] * k < 300000; k++) {
        msunp[ms[i] * k] = true;
      }
    }
  }
  int n;
  while (cin >> n, n - 1) {
    printf("%d:", n);
    REP(i, msp.size()) {
      if (msp[i] > n)
        break;
      if (n % msp[i] == 0) {
        printf(" %d", msp[i]);
      }
    }
    cout << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef complex<double> P;
static const double EPS = 1e-8;
static const int INF = (int)1e8;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOREQ(i, k, n) for (int i = (k); i <= (int)(n); ++i)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define FIND(m, w) ((m).find(w) != (m).end())
bool msnum(int n) { return (n % 7 == 1 || n % 7 == 6); }
int main(void) {
  vector<int> ms;
  vector<int> msp;
  FOR(i, 2, 300000) if (msnum(i)) ms.push_back(i);
  REP(i, ms.size()) {
    bool pr = true;
    REP(j, msp.size()) {
      if (ms[i] % msp[j] == 0) {
        pr = false;
        break;
      }
    }
    if (pr) {
      msp.push_back(ms[i]);
    }
  }
  int n;
  while (cin >> n, n) {
    printf("%d:", n);
    REP(i, msp.size()) {
      if (msp[i] > n)
        break;
      if (n % msp[i] == 0) {
        printf(" %d", msp[i]);
      }
    }
    cout << endl;
  }
  return 0;
}
Problem p00735 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define range(i, s, n) for (int i = s; i < n; i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define fs first
#define sc second
constexpr int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
constexpr int lcm(int a, int b) { return a * b / gcd(a, b); }

int main() {
  vi prime;
  vi isP(300001, 1);
  for (int i = 2; i < 300001; i++) {
    if (i % 7 != 1 && i % 7 != 6)
      isP[i] = 0;
  }
  for (int i = 2; i < ceil(sqrt(300001)); i++) {
    if (isP[i]) {
      for (int j = i * i; j <= 300000; j += i) {
        isP[j] = 0;
      }
    }
  }
  range(i, 2, 300001) if (isP[i]) prime.push_back(i);
  int n;
  while (cin >> n, n != 1) {
    cout << n << ":";
    for (auto p : prime) {
      if (n % p == 0) {
        cout << " " << p;
        while (n % p)
          n /= p;
      }
    }
    cout << endl;
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define range(i, s, n) for (int i = s; i < n; i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define fs first
#define sc second
constexpr int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
constexpr int lcm(int a, int b) { return a * b / gcd(a, b); }

int main() {
  vi prime;
  vi isP(300001, 1);
  range(i, 2, 300001) {
    if (i % 7 != 1 && i % 7 != 6) {
      isP[i] = 0;
      continue;
    }
    if (isP[i]) {
      for (int j = i * i; j <= 300000; j += i) {
        isP[j] = 0;
      }
    }
  }
  range(i, 2, 300001) if (isP[i]) prime.push_back(i);
  int n;
  while (cin >> n, n != 1) {
    cout << n << ":";
    for (auto p : prime) {
      if (n % p == 0) {
        cout << " " << p;
        while (n % p)
          n /= p;
      }
    }
    cout << endl;
  }
}
Problem p00735 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
using namespace std;

int calced;

int prime[400000];
int count;

void make_prime(int end) {

  if (end >= calced) {
    for (int i = calced; i <= end; i++) {
      if (i % 7 == 1 || i % 7 == 6) {
        bool pass = true;

        int now = (int)sqrt((double)i) + 1;

        for (int j = 0; j < count; j++) {
          if (prime[j] > now)
            break;
          if (i % prime[j] == 0) {
            pass = false;
            break;
          }
        }
        if (pass) {
          prime[count] = i;
          count++;
        }
      }
    }
  }
  calced = end + 1;
}

void view_prime(int end) {

  cout << end << ':';

  for (int i = 0; i < count; i++) {
    if (end % prime[i] == 0) {
      cout << ' ' << prime[i];
    }
  }
  cout << endl;
}

int main() {

  for (int i = 0; i < 300000; i++) {
    prime[i] = 100000000;
  }

  calced = 7;
  prime[0] = 6;
  count = 1;

  make_prime(300000);

  while (true) {
    int input;
    cin >> input;
    if (input == 1)
      break;
    view_prime(input);

    for (int i = 0; i < count - 1; i++) {
      if (prime[i] == prime[i + 1]) {
        cout << prime[i] << endl;
      }
    }
  }
}

Error: 0

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;

int calced;

int prime[400000];
int count;

void make_prime(int end) {
  if (end >= calced) {

    for (int i = calced; i <= end; i++) {
      if (i % 7 == 1 || i % 7 == 6) {
        bool pass = true;

        int now = (int)sqrt((double)i) + 1;

        for (int j = 0; j < count; j++) {
          if (prime[j] > now)
            break;
          if (i % prime[j] == 0) {
            pass = false;
            break;
          }
        }
        if (pass) {
          prime[count] = i;
          count++;
        }
      }
    }
  }
  calced = end + 1;
}

void view_prime(int end) {

  cout << end << ':';
  make_prime(end);

  for (int i = 0; i < count; i++) {
    if (end % prime[i] == 0) {
      cout << ' ' << prime[i];
    }
  }
  cout << endl;
}

int main() {

  for (int i = 0; i < 300000; i++) {
    prime[i] = 100000000;
  }

  calced = 7;
  prime[0] = 6;
  count = 1;

  while (true) {
    int input;
    cin >> input;
    if (input == 1)
      break;
    view_prime(input);

    for (int i = 0; i < count - 1; i++) {
      if (prime[i] == prime[i + 1]) {
        cout << prime[i] << endl;
      }
    }
  }
}
Problem p00736 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
string S, T;
int pos;
int solve() {
  if (T[pos] == '0') {
    pos++;
    return 0;
  }
  if (T[pos] == '1') {
    pos++;
    return 1;
  }
  if (T[pos] == '2') {
    pos++;
    return 2;
  }
  if (T[pos] == '-') {
    pos++;
    return 2 - solve();
  }
  if (T[pos] == '(') {
    pos++;
    int a = solve();
    char c = T[pos];
    pos++;
    int b = solve();
    pos++;
    if (c == '+')
      return max(a, b);
    if (c == '*')
      return min(a, b);
  }
  return 0;
}
int main() {
  while (true) {
    cin >> S;
    if (S == ".")
      break;
    int ret = 0;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        for (int k = 0; k < 3; k++) {
          T = S;
          for (int l = 0; l < S.size(); l++) {
            if (S[l] == 'P')
              T[l] = i + 48;
            if (S[l] == 'Q')
              T[l] = j + 48;
            if (S[l] == 'R')
              T[l] = k + 48;
          }
          pos = 0;
          if (solve() == 2) {
            ret++;
          }
        }
      }
    }
    printf("%d\\n", ret);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
string S, T;
int pos;
int solve() {
  if (T[pos] == '0') {
    pos++;
    return 0;
  }
  if (T[pos] == '1') {
    pos++;
    return 1;
  }
  if (T[pos] == '2') {
    pos++;
    return 2;
  }
  if (T[pos] == '-') {
    pos++;
    return 2 - solve();
  }
  if (T[pos] == '(') {
    pos++;
    int a = solve();
    pos++;
    int b = solve();
    if (T[pos] == '+')
      return max(a, b);
    if (T[pos] == '*')
      return min(a, b);
  }
  return 0;
}
int main() {
  while (true) {
    cin >> S;
    int ret = 0;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        for (int k = 0; k < 3; k++) {
          T = S;
          for (int l = 0; l < S.size(); l++) {
            if (S[l] == 'P')
              T[l] = i + 48;
            if (S[l] == 'Q')
              T[l] = j + 48;
            if (S[l] == 'R')
              T[l] = k + 48;
          }
          pos = 0;
          if (solve() == 2) {
            ret++;
          }
        }
      }
    }
    printf("%d\\n", ret);
  }
  return 0;
}
Problem p00737 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#define REP(i, a, n) for (int i = (a); i < (n); i++)
#define INF 100000000
using namespace std;
struct state {
  int dir, y, x, s;
};

int H, W, S[30][30], C[4], dp[30][30][4];
int dd[5] = {0, 1, 0, -1, 0};

class Compare {
public:
  bool operator()(struct state a, struct state b) { return a.s > b.s; }
};

bool isin(int y, int x) { return 0 <= y && y < H && 0 <= x && x < W; }

int main(void) {
  while (cin >> W >> H, H) {
    REP(i, 0, H) REP(j, 0, W) cin >> S[i][j];
    REP(i, 0, 4) cin >> C[i];

    REP(i, 0, H) REP(j, 0, W) REP(k, 0, 4) dp[i][j][k] = INF;

    priority_queue<struct state, vector<struct state>, Compare> q;
    struct state first = {0, 0, 0, 0};
    q.push(first);

    while (!q.empty()) {
      struct state c = q.top();
      q.pop();

      dp[c.y][c.x][c.dir] = min(dp[c.y][c.x][c.dir], c.s);
      if (c.y == H - 1 && c.x == W - 1)
        break;

      REP(i, 0, 4) {
        int dir = (c.dir + i) % 4;
        int w = i == S[c.y][c.x] ? 0 : C[i];
        struct state n = {dir, c.y + dd[dir], c.x + dd[dir + 1], c.s + w};
        if (isin(n.y, n.x) && n.s < dp[n.y][n.x][n.dir])
          q.push(n);
      }
    }

    int ans = INF;
    REP(i, 0, 4) ans = min(ans, dp[H - 1][W - 1][i]);
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define REP(i, a, n) for (int i = (a); i < (n); i++)
#define INF 100000000
using namespace std;

int W, H, S[30][30], C[4];
int dd[5] = {0, 1, 0, -1, 0};
bool vd[30][30][4];

int isin(int y, int x) { return 0 <= y && y < H && 0 <= x && x < W; }

void move(int cy, int cx, int dir, int inst, int *ny, int *nx, int *ndir) {
  *ndir = (dir + inst) % 4;
  *ny = cy + dd[*ndir];
  *nx = cx + dd[*ndir + 1];
  // cout << cy << " " << cx << " " << dir << " " << inst << " -> " << *ny << "
  // " << *nx << " " << *ndir << endl;
}

int dfs(int cy, int cx, int dir, int cost, int m) {
  // cout << cy << " " << cx << " " << dir << " " << cost << endl;
  if (cy == H - 1 && cx == W - 1)
    return cost;

  REP(i, 0, 4) {
    int ny, nx, ndir, temp = S[cy][cx];
    move(cy, cx, dir, S[cy][cx] = i, &ny, &nx, &ndir);

    int c = cost + (i != temp ? C[i] : 0);
    if (isin(ny, nx) && !vd[cy][cx][ndir] && c < m) {
      bool v = vd[cy][cx][ndir];
      vd[cy][cx][ndir] = true;
      m = min(m, dfs(ny, nx, ndir, c, m));
      vd[cy][cx][ndir] = v;
    }

    S[cy][cx] = temp;
  }

  return m;
}

int main(void) {
  while (cin >> W >> H, W || H) {
    REP(i, 0, H) REP(j, 0, W) cin >> S[i][j];
    REP(i, 0, 4) cin >> C[i];
    REP(i, 0, H) REP(j, 0, W) REP(k, 0, 4) vd[i][j][k] = false;
    cout << dfs(0, 0, 0, 0, INF) << endl;
  }
}
Problem p00737 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef pair<int, int> P;

int w, h;
int c[4];
int s[31][31];
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};

struct S {
  int y, x, st, dis;
  bool operator>(const S &s) const { return dis > s.dis; }
};

bool contain(int y, int x) { return (0 <= y && y < h && 0 <= x && x < w); }

const int INF = 1e9;

signed main() {
  while (1) {
    cin >> w >> h;
    if (w + h == 0)
      break;
    rep(i, 0, h) rep(j, 0, w) cin >> s[i][j];
    rep(i, 0, 4) cin >> c[i];
    int d[31][31][4];
    rep(i, 0, h) rep(j, 0, w) rep(k, 0, 4) d[i][j][k] = INF;
    queue<S> q;
    q.push((S){0, 0, 1, 0});
    d[0][0][1] = 0;
    while (!q.empty()) {
      S p = q.front();
      q.pop();
      // if(p.dis > d[p.y][p.x][p.dir]) continue;
      rep(i, 0, 4) {
        int dir = (p.st + i) % 4;
        int ny = p.y + dy[dir];
        int nx = p.x + dx[dir];
        int ndis = p.dis;
        if (!contain(ny, nx))
          continue;
        if (s[p.y][p.x] != i) {
          ndis += c[i];
        }
        if (ndis < d[ny][nx][dir]) {
          d[ny][nx][dir] = ndis;
          q.push((S){ny, nx, dir, ndis});
        }
      }
    }
    int ans = INF;
    rep(i, 0, 4) ans = min(ans, d[h - 1][w - 1][i]);
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef pair<int, int> P;

int w, h;
int c[4];
int s[31][31];
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};

struct S {
  int y, x, st, dis;
  bool operator>(const S &s) const { return dis > s.dis; }
};

bool contain(int y, int x) { return (0 <= y && y < h && 0 <= x && x < w); }

const int INF = 1e9;

signed main() {
  while (1) {
    cin >> w >> h;
    rep(i, 0, h) rep(j, 0, w) cin >> s[i][j];
    rep(i, 0, 4) cin >> c[i];
    int d[31][31][4];
    rep(i, 0, h) rep(j, 0, w) rep(k, 0, 4) d[i][j][k] = INF;
    queue<S> q;
    q.push((S){0, 0, 1, 0});
    d[0][0][1] = 0;
    while (!q.empty()) {
      S p = q.front();
      q.pop();
      rep(i, 0, 4) {
        int dir = (p.st + i) % 4;
        int ny = p.y + dy[dir];
        int nx = p.x + dx[dir];
        int ndis = p.dis;
        if (!contain(ny, nx))
          continue;
        if (s[p.y][p.x] != i) {
          ndis += c[i];
        }
        if (ndis < d[ny][nx][dir]) {
          d[ny][nx][dir] = ndis;
          q.push((S){ny, nx, dir, ndis});
        }
      }
    }
    int ans = INF;
    rep(i, 0, 4) ans = min(ans, d[h - 1][w - 1][i]);
    cout << ans << endl;
  }
}
Problem p00737 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define ck(a, b) (0 <= a && a < b)
using namespace std;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int w, h, c[4], s[30][30], cost[30][30][4];
struct S {
  int y, x, d, c;
  bool operator<(const S &a) const { return c > a.c; }
};
int main() {
  while (cin >> w >> h, w) {
    rep(i, h) rep(j, w) {
      rep(k, 4) cost[i][j][k] = INF;
      cin >> s[i][j];
    }
    rep(i, 4) cin >> c[i];
    cost[0][0][0] = 0;
    priority_queue<S> Q;
    S cs, ns;
    cs.y = cs.x = cs.d = cs.c = 0;
    Q.push(cs);
    while (!Q.empty()) {
      cs = Q.top(), Q.pop();
      if (cs.y == h - 1 && cs.x == w - 1) {
        cout << cs.c << endl;
        break;
      }
      rep(r, 4) {
        ns.d = cs.d + r & 3, ns.y = cs.y + dy[ns.d], ns.x = cs.x + dx[ns.d],
        ns.c = cs.c + (r == s[cs.y][cs.x] ? 0 : c[r]);
        if (ck(ns.y, h) && ck(ns.x, w) && cost[ns.y][ns.x][ns.d] > ns.c)
          cost[ns.y][ns.x][ns.d] = ns.c, Q.push(ns);
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define ck(a, b) (0 <= (a) && (a) < b)
using namespace std;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int w, h, c[4], s[30][30], cost[30][30][4], *g;
void rec(int y, int x, int d) {
  rep(r, 4) {
    int nd = d + r & 3, ny = y + dy[nd], nx = x + dx[nd],
        nc = cost[y][x][d] + (r == s[y][x] ? 0 : c[r]);
    if (ck(ny, h) && ck(nx, w) && cost[ny][nx][nd] > nc)
      cost[ny][nx][nd] = nc, rec(ny, nx, nd);
  }
}
int main() {
  while (cin >> w >> h, w) {
    rep(i, h) rep(j, w) {
      rep(k, 4) cost[i][j][k] = INF;
      cin >> s[i][j];
    }
    rep(i, 4) cin >> c[i];
    cost[0][0][0] = 0;
    rec(0, 0, 0);
    g = cost[h - 1][w - 1];
    cout << (g[0] > g[1] ? g[1] : g[0]) << endl;
  }
  return 0;
}
Problem p00737 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int w, h;
int mp[31][31];
int memo[4][31][31];
int memo2[31][31];
int c[4];
int ans;
int cnt;
string str = ">v<^";

void saiki(int x, int y, int dir, int cost) {
  int dx[] = {1, 0, -1, 0};
  int dy[] = {0, 1, 0, -1};

  if (x == w - 1 && y == h - 1 && ans > cost)
    ans = cost;
  else if (memo[dir % 4][y][x] > cost && ans > cost) {
    memo[dir % 4][y][x] = cost;
    cnt++;

    //    cout << str[dir%4];
    //  cout <<" "<< x <<" " << y <<" cost="<<cost<<endl

    for (int i = 0; i < 4; i++) {
      int ii = (i + dir % 4) % 4;
      int nx = x + dx[ii];
      int ny = y + dy[ii];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h)
        continue;
      if (i == mp[y][x]) {
        saiki(nx, ny, (dir + i), cost);
      } else
        saiki(nx, ny, (dir + i), cost + c[i]);
    }
  }
}

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 >> mp[i][j];

    for (int i = 0; i < 4; i++)
      cin >> c[i];

    for (int i = 0; i < 4; i++)
      for (int j = 0; j < 31; j++)
        for (int k = 0; k < 31; k++)
          memo[i][j][k] = 1000000000;

    ans = 1000000000;
    cnt = 0;
    saiki(0, 0, 0, 0);

    cout << ans << endl;
    //  cout << cnt <<endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int w, h;
int mp[31][31];
int memo[4][31][31];
int c[4];
int ans;
int cnt;
string str = ">v<^";

void saiki(int x, int y, int dir, int cost) {
  int dx[] = {1, 0, -1, 0};
  int dy[] = {0, 1, 0, -1};

  if (x == w - 1 && y == h - 1 && ans > cost)
    ans = cost;
  else if (memo[dir % 4][y][x] > cost) {
    memo[dir % 4][y][x] = cost;
    cnt++;

    //    cout << str[dir%4];
    //  cout <<" "<< x <<" " << y <<" cost="<<cost<<endl

    for (int i = 0; i < 4; i++) {
      int ii = (i + dir % 4) % 4;
      int nx = x + dx[ii];
      int ny = y + dy[ii];
      if (nx < 0 || nx >= w || ny < 0 || ny >= h)
        continue;
      if (i == mp[y][x]) {
        saiki(nx, ny, (dir + i), cost);
      } else
        saiki(nx, ny, (dir + i), cost + c[i]);
    }
  }
}

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 >> mp[i][j];

    for (int i = 0; i < 4; i++)
      cin >> c[i];

    for (int i = 0; i < 4; i++)
      for (int j = 0; j < 31; j++)
        for (int k = 0; k < 31; k++)
          memo[i][j][k] = 1000000000;

    ans = 1000000000;
    cnt = 0;
    saiki(0, 0, 0, 0);

    cout << ans << endl;
    //  cout << cnt <<endl;
  }

  return 0;
}