alexjercan/bugnet huggingface dataset:
Problem p01389 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>

using namespace std;

int n, m;

int main() {
  string s[60];
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> s[i];
  int field[n][m];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      field[i][j] = stoi(s[i].substr(j, 1));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (i + j == 0)
        continue;
      int a = 9999999, b = 999999;
      if (i != 0)
        a = field[i - 1][j];
      if (j != 0)
        b = field[i][j - 1];
      field[i][j] = field[i][j] + min(a, b);
    }
  }
  cout << field[n - 1][m - 1] << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>

using namespace std;

int n, m;

int main() {
  string s[20];
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> s[i];
  int field[n][m];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      field[i][j] = stoi(s[i].substr(j, 1));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (i + j == 0)
        continue;
      int a = 9999999, b = 999999;
      if (i != 0)
        a = field[i - 1][j];
      if (j != 0)
        b = field[i][j - 1];
      field[i][j] = field[i][j] + min(a, b);
    }
  }
  cout << field[n - 1][m - 1] << endl;
}
Problem p01389 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

const int INF = (1 << 25);
const int MAX = 50;
int data[MAX][MAX];
int w, h;

void solve() {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (i - 1 >= 0 && j - 1 >= 0)
        data[i][j] =
            min(data[i - 1][j] + data[i][j], data[i][j - 1] + data[i][j]);
      else if (i - 1 >= 0)
        data[i][j] += data[i - 1][j];
      else if (j - 1 >= 0)
        data[i][j] += data[i][j - 1];
    }
  }
}
int main() {

  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char c;
      cin >> c;
      data[i][j] = c - '0';
    }
  }
  solve();
  cout << data[h - 1][w - 1] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

const int INF = (1 << 25);
const int MAX = 50;
char data[MAX][MAX];
int w, h;
int out = INF;

void solve(int i, int j, int sum) {
  sum += data[i][j] - '0';
  if (i == h - 1 && j == w - 1) {
    out = min(out, sum);
    return;
  }
  if (i + 1 < h)
    solve(i + 1, j, sum);
  if (j + 1 < w)
    solve(i, j + 1, sum);
}

int main() {

  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> data[i][j];
    }
  }
  solve(0, 0, 0);
  cout << out << endl;
  return 0;
}
Problem p01389 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int charToInt(char);
class SolveMinValue {
public:
  vector<vector<int>> field;
  int memo[51][51];
  void setField(vector<vector<int>> _field) {
    field = _field;
    memset(memo, -1, sizeof(memo));
  }
  int solve(int startx, int starty, int destx, int desty) {
    if (memo[destx][desty] != -1)
      return memo[destx][desty];
    int result =
        field[destx][desty] +
        ((startx == destx && starty == desty) ? 0
         : (startx == destx) ? solve(startx, starty, destx, desty - 1)
         : (starty == desty) ? solve(startx, starty, destx - 1, desty)
                             : min(solve(startx, starty, destx - 1, desty),
                                   solve(startx, starty, destx, desty - 1)));
    memo[destx][desty] = result;
    return result;
  }
};

int main() {
  int height, width;
  cin >> height >> width;
  vector<vector<int>> field(height, vector<int>(width));
  for (int h = 0; h < height; ++h)
    for (int w = 0; w < width; ++w) {
      char c;
      cin >> c;
      int n = charToInt(c);
      field[h][w] = n;
    }
  SolveMinValue solver;
  solver.setField(field);
  cout << solver.solve(0, 0, height - 1, width - 1) << endl;
  return 0;
}

int charToInt(char c) {
  stringstream ss;
  int d;
  ss << c;
  ss >> d;
  return d;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int charToInt(char);
class SolveMinValue {
public:
  vector<vector<int>> field;
  void setField(vector<vector<int>> _field) { field = _field; }
  int solve(int startx, int starty, int destx, int desty) {
    return field[destx][desty] +
           ((startx == destx && starty == desty) ? 0
            : (startx == destx) ? solve(startx, starty, destx, desty - 1)
            : (starty == desty) ? solve(startx, starty, destx - 1, desty)
                                : min(solve(startx, starty, destx - 1, desty),
                                      solve(startx, starty, destx, desty - 1)));
  }
};

int main() {
  int height, width;
  cin >> height >> width;
  vector<vector<int>> field(height, vector<int>(width));
  for (int h = 0; h < height; ++h)
    for (int w = 0; w < width; ++w) {
      char c;
      cin >> c;
      int n = charToInt(c);
      field[h][w] = n;
    }
  SolveMinValue solver;
  solver.setField(field);
  cout << solver.solve(0, 0, height - 1, width - 1) << endl;
  return 0;
}

int charToInt(char c) {
  stringstream ss;
  int d;
  ss << c;
  ss >> d;
  return d;
}
Problem p01389 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int main() {
  int H, W;
  cin >> H >> W;
  vector<string> cicada(H);
  vector<vector<int>> dp(H, vector<int>(W, 0));
  for (int i = 0; i < H; i++)
    cin >> cicada[i];
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      cicada[i][j] -= '0';
  for (int i = 1; i < H; i++)
    dp[i][0] = dp[i - 1][0] + cicada[i][0];
  for (int j = 1; j < W; j++)
    dp[0][j] = dp[0][j - 1] + cicada[0][j];
  for (int i = 1; i < H; i++)
    for (int j = 1; j < W; j++)
      dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + cicada[i][j];
  cout << dp[H - 1][W - 1] << endl;
}

Error: MLE

Fixed submission:

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

struct Data {
  int i, j, cost;
};
bool operator<(Data a, Data b) {
  return a.cost != b.cost ? a.cost > b.cost
                          : (a.i != b.i ? a.i > b.i : a.j > b.j);
}

int main() {
  int H, W;
  int ans;
  cin >> H >> W;
  vector<string> cicada(H);
  for (int i = 0; i < H; i++)
    cin >> cicada[i];
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      cicada[i][j] -= '0';
  priority_queue<Data> q;
  Data start = {0, 0, 0};
  q.push(start);
  while (q.size()) {
    Data now = q.top();
    q.pop();
    if (now.i == H - 1 && now.j == W - 1) {
      ans = now.cost;
      break;
    }
    if (now.i + 1 < H) {
      Data next = now;
      next.i++;
      next.cost += cicada[next.i][next.j];
      q.push(next);
    }
    if (now.j + 1 < W) {
      Data next = now;
      next.j++;
      next.cost += cicada[next.i][next.j];
      q.push(next);
    }
  }
  cout << ans << endl;
}
Problem p01389 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int h, w, ans;
int c[50][50];
int main() {
  cin >> h >> w;
  string s[h];
  int small[50][50] = {10000000};
  // int test=0;
  for (int i = 0; i < h; ++i)
    cin >> s[i];
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      c[i][j] = s[i][j] - '0';

  /*
  for(int i=0;i<h;++i)
    ans+=c[i][0];
  for(int i=1;i<w;++i)
    ans+=c[h-1][i];
  */
  /*
  for(int i=0;i<w;++i)
    test+=c[0][i];
  for(int i=1;i<h;++i)
    test+=c[i][w-1];

  ans=min(test,ans);
  */
  small[0][0] = 0;
  int blank = 0;
  for (int i = 1; i < w; i++) {
    blank += c[0][i];
    small[0][i] += blank;
  }

  blank = 0;
  for (int i = 1; i < h; i++) {
    blank += c[i][0];
    small[i][0] += blank;
  }

  for (int i = 1; i < h; i++) {

    for (int j = 1; j < w; j++) {

      if (small[i - 1][j] > small[i][j - 1])
        small[i][j] = small[i][j - 1] + c[i][j];
      else
        small[i][j] = small[i - 1][j] + c[i][j];
    }
  }
  /*
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++) cout<<small[i][j]<<" ";
    cout<<endl;
  }
  */

  cout << small[h - 1][w - 1] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int h, w, ans = 1e9;
int c[50][50];
void dfs(int y, int x, int n) {
  if (y >= h || x >= w)
    return;
  if (y == h - 1 && x == w - 1)
    ans = min(ans, n);
  else {
    dfs(y, x + 1, n + c[y][x + 1]);
    dfs(y + 1, x, n + c[y + 1][x]);
  }
}
int main() {
  cin >> h >> w;
  string s[h];
  for (int i = 0; i < h; ++i)
    cin >> s[i];
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      c[i][j] = s[i][j] - '0';

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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int h, w;
int a[52][52] = {0};

int main() {

  scanf("%d %d", &h, &w);

  for (int i = 0; i <= h + 1; i++)
    for (int j = 0; j <= w + 1; j++)
      a[i][j] = 500000;

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

  //  for(int i=0;i<=w;i++)
  // cout << a[1][i] << " " ;

  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (i == 1 && j == 1)
        continue;

      if (a[i][j - 1] > a[i - 1][j])
        a[i][j] += a[i - 1][j];
      else
        a[i][j] += a[i][j - 1];
    }
  }

  printf("%d\\n", a[h][w]);

  return 0;
}

Error: TLE

Fixed submission:

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

int h, w, a[51][51], dis[51][51];

void reset() {
  for (int i = 0; i <= h; i++) {
    for (int j = 0; j <= w; j++) {
      dis[i][j] = 0;
      a[i][j] = 1000000;
    }
  }
}

int main() {

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

    reset();

    for (int i = 1; i <= h; i++)
      for (int j = 1; j <= w; j++)
        cin >> a[i][j];

    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        if (i == 1 && j == 1)
          continue;

        a[i][j] += min(a[i][j - 1], a[i - 1][j]);
      }
    }

    cout << a[h][w] << endl;
  }

  return 0;
}
Problem p01389 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int b[50][50] = {};
int c[50][50];

int sr(int x, int y) {
  if (c[x][y] != 1000000) {
    return c[x][y];
  }
  if (x == 0) {
    return c[x][y] = b[x][y] + sr(x, y - 1);
  }
  if (y == 0) {
    return c[x][y] = b[x][y] + sr(x - 1, y);
  }
  return c[x][y] = b[x][y] + min(sr(x - 1, y), sr(x, y - 1));
}

int main() {
  int h, w;
  cin >> h >> w;
  char a[50][50] = {};

  for (int i = 0; i < h; i++) {
    for (int t = 0; t < w; t++) {
      c[i][t] = 1000000;
      cin >> a[i][t];
      b[i][t] = a[i][t] - '0';
    }
  }
  c[0][0] = 0;
  int ans = sr(h - 1, w - 1);
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int b[50][50] = {};
int c[50][50] = {};

int sr(int x, int y) {
  if (x == 0 && y == 0) {
    return 0;
  }
  if (c[x][y] != 0) {
    return c[x][y];
  }
  if (x == 0) {
    return c[x][y] = b[x][y] + sr(x, y - 1);
  }
  if (y == 0) {
    return c[x][y] = b[x][y] + sr(x - 1, y);
  }
  return c[x][y] = b[x][y] + min(sr(x - 1, y), sr(x, y - 1));
}

int main() {
  int h, w;
  cin >> h >> w;
  char a[50][50] = {};

  for (int i = 0; i < h; i++) {
    for (int t = 0; t < w; t++) {
      cin >> a[i][t];
      b[i][t] = a[i][t] - '0';
    }
  }

  int ans = sr(h - 1, w - 1);
  cout << ans << endl;

  return 0;
}
Problem p01389 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> m;
int H, W;
int main(void) {
  cin >> H >> W;
  m = vector<vector<int>>(H, vector<int>(W));
  for (int p = 0; p < H; p++) {
    string tmp;
    cin >> tmp;
    for (int j = 0; j < tmp.length(); j++)
      m[p][j] = tmp[j] - '0';
  }

  vector<vector<int>> dp(H, vector<int>(W, 9 * 50 * 50 * 2));
  dp[0][0] = 0;
  for (int p = 0; p < H; p++) {
    for (int q = 0; q < W; q++) {
      if (!(p | q))
        continue;
      if (!p)
        dp[p][q] = dp[p][q - 1] + m[p][q];
      else if (!q)
        dp[p][q] = dp[p - 1][q] + m[p][q];
      else
        dp[p][q] = min(dp[p - 1][q] + m[p][q], dp[p][q - 1] + m[p][q]);
    }
  }
  cout << dp[H - 1][W - 1] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> m;
int mi;
int H, W;
int dy[] = {0, 1};
int dx[] = {1, 0};

void dfs(int y, int x, int n) {
  if (y == H - 1 && x == W - 1) {
    if (mi > n)
      mi = n;
    return;
  }
  for (int p = 0; p < 2; p++) {
    int nx = x + dx[p];
    int ny = y + dy[p];
    if (nx < W && ny < H)
      dfs(ny, nx, n + m[ny][nx]);
  }
}

int main(void) {
  cin >> H >> W;
  m = vector<vector<int>>(H, vector<int>(W));
  for (int p = 0; p < H; p++) {
    string tmp;
    cin >> tmp;
    for (int j = 0; j < tmp.length(); j++)
      m[p][j] = tmp[j] - '0';
  }

  mi = 9 * 50 * 50 * 2;
  dfs(0, 0, 0);
  cout << mi << endl;
  return 0;
}
Problem p01389 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define pb(a) push_back(a)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int main() {
  long long H, W, dp[60][60];
  char inp[55][55];
  rep(i, 55) rep(j, 55) dp[i][j] = 200000000;

  cin >> H >> W;
  rep(i, H) { cin >> inp[i]; }
  rep(i, H) {
    rep(j, W) { dp[i + 1][j + 1] = inp[i][j] - '0'; }
  }

  range(i, 1, H + 1) {
    range(j, 1, W + 1) {
      if (i == j && i == 1)
        continue;
      dp[i][j] += min(dp[i - 1][j], dp[i][j - 1]);
    }
  }
  cout << dp[H][W] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define pb(a) push_back(a)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int main() {
  int n, f[35] = {0};
  f[0] = 1;
  rep(i, 31) {
    f[i + 1] += f[i];
    f[i + 2] += f[i];
    f[i + 3] += f[i];
  }
  while (cin >> n, n) {
    int day = f[n] / 10;
    if (f[n] / 10 == 0)
      day++;
    int y = day / 365;
    if (day % 365 != 0)
      y++;
    cout << y << endl;
  }
}
Problem p01389 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i))

int main() {
  int H, W;
  cin >> H >> W;
  char b[H][W];
  rep(i, H) rep(j, W) cin >> b[i][j];
  int dp[51][51];
  memset(dp, 0, sizeof(dp));

  for (int i = 0; i <= H; ++i) {
    for (int j = 0; j <= W; ++j) {
      if (i == 0 && j == 0) {
        dp[i][j] = (b[i][j] - '0');
      } else {
        if (i == 0)
          dp[i][j] = dp[i][j - 1] + (b[i][j] - '0');
        else if (j == 0)
          dp[i][j] = dp[i - 1][j] + (b[i][j] - '0');
        else
          dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + (b[i][j] - '0');
      }
    }
  }

  cout << dp[H - 1][W - 1] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i))

int H, W;
char b[51][51];
int ans = INT_MAX;

void dfs(int y, int x, int s) {
  if (y == H - 1 && x == W - 1) {
    ans = min(ans, s + (b[H - 1][W - 1] - '0'));
  } else {
    if (y < H - 1)
      dfs(y + 1, x, s + (b[y][x] - '0'));
    if (x < W - 1)
      dfs(y, x + 1, s + (b[y][x] - '0'));
  }
}

int main() {
  cin >> H >> W;
  rep(i, H) rep(j, W) cin >> b[i][j];
  dfs(0, 0, 0);
  cout << ans << endl;
  return 0;
}
Problem p01389 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

void solve() {
  int route[52][52];
  char cicada[52][52];
  int H, W;
  cin >> H >> W;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> cicada[i][j];
    }
  }
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      route[i][j] = 999999999;
    }
  }
  route[0][0] = 0;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (route[i][j + 1] > route[i][j] + cicada[i][j + 1] - '0') {
        route[i][j + 1] = route[i][j] + cicada[i][j + 1] - '0';
      }
      if (route[i + 1][j] > route[i][j] + cicada[i + 1][j] - '0') {
        route[i + 1][j] = route[i][j] + cicada[i + 1][j] - '0';
      }
    }
  }
  cout << route[H - 1][W - 1] << endl;
}

int main() {
  solve();
  return (0);
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

void solve() {
  int route[52][52];
  char cicada[52][52];
  int H, W;
  while (cin >> H >> W, H || W) {
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        cin >> cicada[i][j];
      }
    }
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        route[i][j] = 999999999;
      }
    }
    route[0][0] = 0;
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (route[i][j + 1] > route[i][j] + cicada[i][j + 1] - '0') {
          route[i][j + 1] = route[i][j] + cicada[i][j + 1] - '0';
        }
        if (route[i + 1][j] > route[i][j] + cicada[i + 1][j] - '0') {
          route[i + 1][j] = route[i][j] + cicada[i + 1][j] - '0';
        }
      }
    }
    cout << route[H - 1][W - 1] << endl;
  }
}

int main() {
  solve();
  return (0);
}
Problem p01389 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int semi[51][51], h, w, r[51][51];
int min(int a, int b) {
  if (a < b) {
    return a;
  } else {
    return b;
  }
}
int main() {
  cin >> h >> w;
  int i, j, k, n;
  char c[52];
  for (i = 0; i < 52; i++) {
    c[i] = '\\0';
  }
  for (i = 0; i < 51; i++) {
    for (j = 0; j < 51; j++) {
      semi[i][j] = -1;
    }
  }
  for (i = 0; i < h; i++) {
    cin >> c;
    for (j = 0; j < w; j++) {
      semi[i][j] = (int)(c[j] - '0');
    }
  }
  i = h - 1;
  j = w - 1;
  for (i = h - 1; i >= 0; i--) {
    for (j = w - 1; j >= 0; j--) {
      if (i == h - 1 && j == w - 1) {
        r[i][j] = 0;
      } else if (j == w - 1) {
        r[i][j] = r[i + 1][j] + semi[i][j];
      } else if (i == h - 1) {
        r[i][j] = r[i][j + 1] + semi[i][j];
      } else {
        r[i][j] = semi[i][j] + min(r[i + 1][j], r[i][j + 1]);
      }
    }
  }
  cout << r[0][0] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int semi[51][51], h, w;
int min(int a, int b) {
  if (a < b) {
    return a;
  } else {
    return b;
  }
}
int route(int i, int j) {
  int k, sum;
  if (i == h - 1 && j == w - 1) {
    return 0;
  } else if (i == h - 1) {
    sum = 0;
    for (k = j; k < w - 1; k++) {
      sum += semi[h - 1][k];
    }
    return sum;
  } else if (j == w - 1) {
    sum = 0;
    for (k = i; k < h - 1; k++) {
      sum += semi[k][w - 1];
    }
    return sum;
  } else {
    return semi[i][j] + min(route(i + 1, j), route(i, j + 1));
  }
}
int main() {
  cin >> h >> w;
  int i, j, k, n;
  char c[52];
  for (i = 0; i < 52; i++) {
    c[i] = '\\0';
  }
  for (i = 0; i < 51; i++) {
    for (j = 0; j < 51; j++) {
      semi[i][j] = -1;
    }
  }
  for (i = 0; i < h; i++) {
    cin >> c;
    for (j = 0; j < w; j++) {
      semi[i][j] = (int)(c[j] - '0');
    }
  }
  cout << route(0, 0) << endl;
  return 0;
}
Problem p01389 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#define INF 9999999
using namespace std;
int h, w;
char block[100][100];
int dp[100][100];
int solve(int x, int y);
int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> block[i][j];
    }
  }
  memset(dp, -1, sizeof(dp));
  cout << solve(0, 0) << endl;
}

int solve(int x, int y) {
  if (x == w - 1 && y == h - 1)
    return block[y][x] - '0';
  if (x < 0 || x >= w || y < 0 || y >= h)
    return INF;
  if (dp[y][x] != -1)
    return dp[y][x];
  return dp[y][x] = min(solve(x + 1, y) + block[y][x] - '0',
                        solve(x, y + 1) + block[y][x] - '0');
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#define INF 9999999
using namespace std;
int h, w;
char block[100][100];
int dp[100][100];
int solve(int x, int y);
int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> block[i][j];
    }
  }
  cout << solve(0, 0) << endl;
}

int solve(int x, int y) {
  // cout << x << " " << y << endl;
  if (x == w - 1 && y == h - 1)
    return block[y][x] - '0';
  if (x < 0 || x >= w || y < 0 || y >= h)
    return INF;
  if (dp[y][x] > 0)
    return dp[y][x];
  return dp[y][x] = min(solve(x + 1, y) + block[y][x] - '0',
                        solve(x, y + 1) + block[y][x] - '0');
}
Problem p01389 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
typedef pair<int, int> P;
int h, w;
int sx, sy;
int nextx[2] = {1, 0};
int nexty[2] = {0, 1};
string maps[52];
int total[52][52];
int bfs();

int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    cin >> maps[i];
  }
  bfs();
  cout << total[h - 1][w - 1] << endl;
}

int bfs() {
  queue<P> route;                   // 縦 - 横
  memset(total, -1, sizeof(total)); // mapの初期化
  route.push(P(0, 0));
  total[0][0] = 0;
  while (!route.empty()) {
    P now = route.front();
    route.pop();
    for (int i = 0; i < 2; i++) {
      int ny = now.first + nexty[i];
      int nx = now.second + nextx[i];
      if (total[ny][nx] != -1) {
        if (ny >= 0 && nx >= 0 && ny < h && nx < w &&
            total[ny][nx] >
                ((total[now.first][now.second]) + (maps[ny][nx] - '0'))) {
          total[ny][nx] =
              ((total[now.first][now.second]) + (maps[ny][nx] - '0'));
          route.push(P(ny, nx));
        }
      } else {
        if (ny >= 0 && nx >= 0 && ny < h && nx < w) {
          total[ny][nx] =
              ((total[now.first][now.second]) + (maps[ny][nx] - '0'));
          route.push(P(ny, nx));
        }
      }
    }
  }
  return 0;
}

// cicada.

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
typedef pair<int, int> P;
int h, w;
int sx, sy;
int nextx[2] = {1, 0};
int nexty[2] = {0, 1};
string maps[52];
int total[52][52];
int bfs();

int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    cin >> maps[i];
  }
  bfs();
  cout << total[h - 1][w - 1] << endl;
}

int bfs() {
  queue<P> route;                   // 縦 - 横
  memset(total, -1, sizeof(total)); // mapの初期化
  route.push(P(0, 0));
  total[0][0] = 0;
  while (!route.empty()) {
    P now = route.front();
    route.pop();
    for (int i = 0; i < 2; i++) {
      int ny = now.first + nexty[i];
      int nx = now.second + nextx[i];
      if (ny >= 0 && nx >= 0 && ny < h && nx < w) {
        if (total[ny][nx] != -1) {
          if (total[ny][nx] >
              (total[now.first][now.second]) + (maps[ny][nx] - '0')) {
            total[ny][nx] =
                (total[now.first][now.second]) + (maps[ny][nx] - '0');
          }
        } else {
          total[ny][nx] = (total[now.first][now.second]) + (maps[ny][nx] - '0');
        }
        route.push(P(ny, nx));
      }
    }
  }
}

// cicada.