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

#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> V;
int H, W, cnt, vx, vy;
void solve(vector<vector<int>> &p, int x, int y, int cr) {
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};
  int cx = x, cy = y;
  if (cr == 0) {
    if (x == vx || y == vy)
      cnt++;
    return;
  }
  for (int i = 0; i < 4; i++) {
    cx = x;
    cy = y;
    while (true) {
      if (cx < 0 || cx >= H || cy < 0 || cy >= W)
        break;
      if (p[cx][cy] == 3) {
        p[cx][cy] = 1;
        solve(p, cx, cy, cr - 1);
        p[cx][cy] = 3;
        break;
      }
      cx += dx[i];
      cy += dy[i];
    }
  }
}
int main() {
  while (true) {
    cin >> W >> H;
    if (H == 0)
      break;
    cnt = 0;
    int vr = 0;
    vector<vector<int>> a(H, vector<int>(W));
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> a[i][j];
        if (a[i][j] == 2) {
          vx = i;
          vy = j;
        }
        if (a[i][j] == 1) {
          a[i][j] = 3;
          vr++;
        }
      }
    }
    solve(a, vx, vy, vr);
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> V;
int H, W, cnt, vx, vy;
void solve(vector<vector<int>> p, int x, int y, int cr) {
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};
  int cx = x, cy = y;
  if (cr == 0) {
    if (x == vx || y == vy)
      cnt++;
    return;
  }
  for (int i = 0; i < 4; i++) {
    cx = x;
    cy = y;
    while (true) {
      if (cx < 0 || cx >= H || cy < 0 || cy >= W)
        break;
      if (p[cx][cy] == 3) {
        vector<vector<int>> q = p;
        q[cx][cy] = 1;
        solve(q, cx, cy, cr - 1);
        break;
      }
      cx += dx[i];
      cy += dy[i];
    }
  }
}
int main() {
  while (true) {
    cin >> W >> H;
    if (H == 0)
      break;
    cnt = 0;
    int vr = 0;
    vector<vector<int>> a(H, vector<int>(W));
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> a[i][j];
        if (a[i][j] == 2) {
          vx = i;
          vy = j;
        }
        if (a[i][j] == 1) {
          a[i][j] = 3;
          vr++;
        }
      }
    }
    solve(a, vx, vy, vr);
    cout << cnt << endl;
  }
  return 0;
}
Problem p00471 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
using namespace std;

int n, m, a[10][10], maxN = 0;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int dfs(int y, int x, int counter) {
  int ans = 0;
  for (int i = 0; i < 4; i++) {
    int ny = y, nx = x;
    while (1) {
      ny += dy[i], nx += dx[i];
      if (ny < 0 || nx < 0 || ny >= n || nx >= m)
        break;
      // if (a[ny][nx] == -1) break;
      if (a[ny][nx] == 1) {
        if (counter > 0) {
          a[ny][nx] = -1;
          ans += dfs(ny, nx, counter - 1);
          a[ny][nx] = 1;
        }
        break;
      }
      if (a[ny][nx] == 2 && counter == 0)
        return 1;
    }
  }
  return ans;
}

int main() {
  while (1) {
    scanf("%d%d", &m, &n);
    if (m == 0)
      break;
    int cx, cy;
    maxN = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) {
        scanf("%d", &a[i][j]);
        if (a[i][j] == 2)
          cy = i, cx = j;
        if (a[i][j] == 1)
          maxN++;
      }
    printf("%d\\n", dfs(cy, cx, maxN));
  }
}


Error: TLE

Fixed submission:

#include <cstdio>
using namespace std;

int n, m, a[10][10], maxN = 0;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int dfs(int y, int x, int counter) {
  int ans = 0;
  for (int i = 0; i < 4; i++) {
    int ny = y, nx = x;
    while (1) {
      ny += dy[i], nx += dx[i];
      if (ny < 0 || nx < 0 || ny >= n || nx >= m)
        break;
      if (a[ny][nx] == -1)
        break;
      if (a[ny][nx] == 1 && counter > 0) {
        a[ny][nx] = -1;
        ans += dfs(ny, nx, counter - 1);
        a[ny][nx] = 1;
      }
      if (a[ny][nx] == 2 && counter == 0)
        return 1;
    }
  }
  return ans;
}

int main() {
  while (1) {
    scanf("%d%d", &m, &n);
    if (m == 0)
      break;
    int cx, cy;
    maxN = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) {
        scanf("%d", &a[i][j]);
        if (a[i][j] == 2)
          cy = i, cx = j;
        if (a[i][j] == 1)
          maxN++;
      }
    printf("%d\\n", dfs(cy, cx, maxN));
  }
}
Problem p00471 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#pragma warning(disable : 4996)
#define rng(x, y) (0 <= x && x < W && 0 <= y && y < H)
int H, W, n, gx, gy, x[12][12], d[4] = {1, 0, -1, 0};
int solve(int sx, int sy, int depth) {
  if (depth == n) {
    if (sx == gx || sy == gy)
      return 1;
    return 0;
  }
  int ret = 0;
  for (int i = 0; i < 4; i++) {
    int tx = sx + d[i], ty = sy + d[i ^ 1];
    while (rng(tx, ty)) {
      if (x[ty][tx] == 1) {
        x[ty][tx] = 0;
        ret += solve(tx, ty, depth + 1);
        x[ty][tx] = 1;
        break;
      }
      tx += d[i];
      ty += d[i ^ 1];
    }
  }
  return ret;
}
int main() {
  while (scanf("%d%d", &W, &H), W | H) {
    n = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++)
        scanf("%d", &x[i][j]);
    }
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (x[i][j] == 1)
          n++;
        if (x[i][j] == 2)
          gx = j, gy = i;
      }
    }
    printf("%d\\n", solve(gx, gy, 0));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

#pragma warning(disable : 4996)

using namespace std;

int H, W, C, sx, sy;
vector<vector<int>> M;

const vector<int> dx = {0, 1, 0, -1};
const vector<int> dy = {-1, 0, 1, 0};

int solve(int x, int y, int d, vector<vector<bool>> used) {
  if (x == sx && y == sy) {
    if (d == C) {
      return 1;
    }

    if (d != 0) {
      return 0;
    }
  }

  int ret = 0;

  for (int dir = 0; dir < 4; dir++) {
    int x2 = x + dx[dir];
    int y2 = y + dy[dir];

    while (0 <= x2 && x2 < W && 0 <= y2 && y2 < H) {
      if (used[y2][x2]) {
        vector<vector<bool>> used2(used.begin(), used.end());
        used2[y2][x2] = false;

        ret += solve(x2, y2, d + 1, used2);
        break;
      }

      x2 += dx[dir];
      y2 += dy[dir];
    }
  }

  return ret;
}

int main() {
  while (true) {
    scanf("%d", &W);
    scanf("%d", &H);

    if (W == 0 && H == 0) {
      break;
    }

    M = vector<vector<int>>(H, vector<int>(W));

    vector<vector<bool>> start(H, vector<bool>(W, false));
    C = 0;

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        scanf("%d", &M[i][j]);

        if (M[i][j] != 0) {
          C++;
          start[i][j] = true;
        }

        if (M[i][j] == 2) {
          sx = j;
          sy = i;
        }
      }
    }

    printf("%d\\n", solve(sx, sy, 0, start));
  }

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

#include <stdio.h>
#include <string.h>

int map[10 + 2][10 + 2];

int housecnt;
int ans;
int cathx, cathy;

int calc(int x, int y, int depth) {
  int i;

  if (depth == housecnt && (cathx == x || cathy == y)) {
    ans++;
    return (0);
  }

  map[x][y] = 0;
  i = 1;
  while (map[x + i][y] == 0)
    i++;
  if (map[x + i][y] == 1) {
    calc(x + i, y, depth + 1);
  }
  i = 1;
  while (map[x - i][y] == 0)
    i++;
  if (map[x - i][y] == 1) {
    calc(x - i, y, depth + 1);
  }
  i = 1;
  while (map[x][y + i] == 0)
    i++;
  if (map[x][y + i] == 1) {
    calc(x, y + i, depth + 1);
  }
  i = 1;
  while (map[x][y - i] == 0)
    i++;
  if (map[x][y - i] == 1) {
    calc(x, y - i, depth + 1);
  }
  map[x][y] = 1;

  return (0);
}

int main(void) {
  int w, h;

  while (1) {
    scanf("%d%d", &w, &h);

    if (w == 0 && h == 0) {
      break;
    }

    memset(map, -1, sizeof(map));

    housecnt = ans = 0;

    for (int y = 1; y <= h; y++) {
      for (int x = 1; x <= w; x++) {
        scanf("%d", &map[x][y]);
        if (map[x][y] == 2) {
          cathx = x;
          cathy = y;
          map[x][y] = 0;
        }
        if (map[x][y] == 1) {
          housecnt++;
        }
      }
    }

    calc(cathx, cathy, 0);

    printf("%d\\n", ans);
  }

  return (0);
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <string.h>

int map[10 + 2][10 + 2];

int housecnt;
int ans;
int cathx, cathy;

int calc(int x, int y, int depth) {
  int i;

  if (depth == housecnt && (cathx == x || cathy == y)) {
    ans++;
    return (0);
  }

  map[x][y] = 0;
  i = 1;
  while (map[x + i][y] == 0)
    i++;
  if (map[x + i][y] == 1) {
    calc(x + i, y, depth + 1);
  }
  i = 1;
  while (map[x - i][y] == 0)
    i++;
  if (map[x - i][y] == 1) {
    calc(x - i, y, depth + 1);
  }
  i = 1;
  while (map[x][y + i] == 0)
    i++;
  if (map[x][y + i] == 1) {
    calc(x, y + i, depth + 1);
  }
  i = 1;
  while (map[x][y - i] == 0)
    i++;
  if (map[x][y - i] == 1) {
    calc(x, y - i, depth + 1);
  }
  map[x][y] = 1;

  return (0);
}

int main(void) {
  int w, h;

  while (1) {
    scanf("%d%d", &w, &h);

    memset(map, -1, sizeof(map));

    housecnt = ans = 0;

    for (int y = 1; y <= h; y++) {
      for (int x = 1; x <= w; x++) {
        scanf("%d", &map[x][y]);
        if (map[x][y] == 2) {
          cathx = x;
          cathy = y;
          map[x][y] = 0;
        }
        if (map[x][y] == 1) {
          housecnt++;
        }
      }
    }

    calc(cathx, cathy, 0);

    printf("%d\\n", ans);
  }

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

#include <bits/stdc++.h>
using namespace std;
const int dy[] = {1, 0, -1, 0};
const int dx[] = {0, 1, 0, -1};
int W, H, N;
int mas[11][11];
bool isin(int y, int x) { return (y >= 0 && x >= 0 && y < H && x < W); }
int dfs(int y, int x, int bit) {
  int ret = 0;
  if (bit == 0)
    return (1);
  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i], nx = x + dx[i];
    while (isin(ny, nx) &&
           (mas[ny][nx] < 0 || (bit != (1 << N) && mas[ny][nx] == N) ||
            (bit & ~(1 << mas[ny][nx])) == bit))
      ny += dy[i], nx += dx[i];
    if (isin(ny, nx))
      ret += dfs(ny, nx, bit & ~(1 << mas[ny][nx]));
  }
  return (ret);
}

int main() {
  int px, py;
  while (cin >> W >> H, W || H) {
    N = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> mas[i][j];
        if (mas[i][j] == 0)
          mas[i][j] = -1;
        else if (mas[i][j] == 1)
          mas[i][j] = N, N++;
        else
          py = i, px = j;
      }
    }
    mas[py][px] = N;
    cout << dfs(py, px, (1 << (N + 1)) - 1) << endl;
  }
  return (0);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int dy[] = {1, 0, -1, 0};
const int dx[] = {0, 1, 0, -1};
int W, H, N;
int mas[11][11];
bool isin(int y, int x) { return (y >= 0 && x >= 0 && y < H && x < W); }
int dfs(int y, int x, int bit) {
  int ret = 0;
  if (bit == 0)
    return (1);
  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i], nx = x + dx[i];
    while (isin(ny, nx) &&
           (mas[ny][nx] < 0 || (bit != (1 << N) && mas[ny][nx] == N) ||
            (bit & ~(1 << mas[ny][nx])) == bit))
      ny += dy[i], nx += dx[i];
    if (isin(ny, nx))
      ret += dfs(ny, nx, bit & ~(1 << mas[ny][nx]));
  }
  return (ret);
}

int main() {
  int px, py, N = 0;
  while (cin >> W >> H, W || H) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> mas[i][j];
        if (mas[i][j] == 0)
          mas[i][j] = -1;
        else if (mas[i][j] == 1)
          mas[i][j] = N, N++;
        else
          py = i, px = j;
      }
    }
    mas[py][px] = N;
    cout << dfs(py, px, (1 << (N + 1)) - 1) << endl;
  }
  return (0);
}
Problem p00471 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

enum Block { BLANK, HOUSE, CHURCH };
enum { Y, X };
const int NEXT[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int m, n;
int map[10][10];
int cy, cx;

int dfs(int y, int x, int remain) {
  if (!remain)
    return (y == cy) || (x == cx);
  auto bak = map[y][x];
  map[y][x] = BLANK;
  int res = 0;
  for (auto d : NEXT) {
    int ny = y + d[Y];
    int nx = x + d[X];
    while (true) {
      if (ny < 0 || n <= ny)
        break;
      if (nx < 0 || m <= nx)
        break;
      if (map[ny][nx] == HOUSE) {
        res += dfs(ny, nx, remain - 1);
        break;
      }
      ny += d[Y];
      nx += d[X];
    }
  }
  map[y][x] = bak;
  return res;
}

int main() {
  while (cin >> m >> n, m | n) {
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j)
        cin >> map[i][j];

    int all = 0;
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j) {
        if (map[i][j] == HOUSE)
          ++all;
        if (map[i][j] == CHURCH) {
          cy = i;
          cx = j;
        }
      }
    cout << dfs(cy, cx, all) << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

enum Block { BLANK, HOUSE, CHURCH };
enum { Y, X };
const int NEXT[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int cy, cx;

int dfs(vector<vector<int>> &map, int y, int x, int remain) {
  if (!remain)
    return (y == cy) || (x == cx);
  auto bak = map[y][x];
  map[y][x] = BLANK;
  int res = 0;
  for (auto d : NEXT) {
    int ny = y + d[Y];
    int nx = x + d[X];
    while (true) {
      if (ny < 0 || map.size() <= ny)
        break;
      if (nx < 0 || map[ny].size() <= nx)
        break;
      if (map[ny][nx] == HOUSE) {
        res += dfs(map, ny, nx, remain - 1);
        break;
      }
      ny += d[Y];
      nx += d[X];
    }
  }
  map[y][x] = bak;
  return res;
}

int main() {
  int m, n;
  while (cin >> m >> n, m | n) {
    vector<vector<int>> map(n, vector<int>(m));
    for (auto &line : map)
      for (auto &i : line)
        cin >> i;

    int all = 0;
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j) {
        if (map[i][j] == HOUSE)
          ++all;
        if (map[i][j] == CHURCH) {
          cy = i;
          cx = j;
        }
      }
    cout << dfs(map, cy, cx, all) << endl;
  }
}
Problem p00471 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int n, m, sx, sy, num;
int grd[11][11];
const int mx[] = {1, 0, -1, 0}, my[] = {0, 1, 0, -1};
int dfs(int x, int y) {
  if (num == 0)
    return (x == sx || y == sy);
  int ans = 0;
  grd[y][x] = 0;
  rep(i, 4) {
    int tx = x + mx[i], ty = y + my[i];
    while (0 <= tx && tx < n && 0 <= ty && ty < m) {
      if (grd[ty][tx] == 1) {
        num--;
        ans += dfs(tx, ty);
        num++;
        break;
      }
      tx += mx[i];
      ty += my[i];
    }
  }
  grd[y][x] = 1;
  return ans;
}

int main() {
  while (true) {
    cin >> n >> m;
    if (n == 0)
      return 0;
    memset(grd, 0, sizeof(grd));
    num = 0;
    rep(i, m) rep(j, n) {
      cin >> grd[i][j];
      if (grd[i][j] == 2) {
        sx = j, sy = i;
      } else if (grd[i][j] == 1)
        num++;
    }
    cout << dfs(sx, sy) << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> grd;
map<tuple<vector<vector<int>>, int, int>, int> dp;
int m, n;
int mx[] = {1, -1, 0, 0}, my[] = {0, 0, 1, -1};
int dfs(int x, int y, int left) {
  if (dp.find(make_tuple(grd, x, y)) != dp.end()) {
    return dp[make_tuple(grd, x, y)];
  }
  int ans = 0;
  for (int i = 0; i < 4; i++) {
    int tx = x + mx[i], ty = y + my[i];
    while (0 <= tx && tx < m && 0 <= ty && ty < n) {
      if (grd[ty][tx] == -1)
        break;
      if (grd[ty][tx] == 2 && left == 0)
        return 1;
      else if (grd[ty][tx] == 1) {
        grd[ty][tx] = -1;
        ans += dfs(tx, ty, left - 1);
        grd[ty][tx] = 1;
      }
      tx += mx[i];
      ty += my[i];
    }
  }
  dp[make_tuple(grd, x, y)] = ans;
  return ans;
}

int main() {
  while (true) {
    cin >> m >> n;
    if (m == 0)
      return 0;
    grd.clear();
    grd.assign(n, vector<int>(m, 0));
    int cnt = 0, sx, sy;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> grd[i][j];
        if (grd[i][j] == 1)
          cnt++;
        else if (grd[i][j] == 2) {
          sx = j;
          sy = i;
        }
      }
    }
    cout << dfs(sx, sy, cnt) << endl;
  }
}
Problem p00471 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
bool hs[10][10];
bool used[10][10];
int cx, cy;
int ans;
int nh;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
int w, h;
void dfs(int x, int y, int f) {
  if (f == nh) {
    if (x == cx || y == cy)
      ans++;
    return;
  }
  int i, j;
  for (i = 0; i < 4; i++) {
    for (j = 1; j < 10; j++) {
      int nx = x + j * dx[i];
      int ny = y + j * dy[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        break;
      if (hs[nx][ny]) {
        if (!used[nx][ny]) {
          used[nx][ny] = true;
          dfs(nx, ny, f + 1);
          used[nx][ny] = false;
          break;
        }
      }
    }
  }
}
int main() {
  while (1) {
    int i, j;
    scanf("%d %d", &w, &h);
    if (w == 0)
      return 0;
    for (i = 0; i < h; i++)
      for (j = 0; j < w; j++) {
        hs[i][j] = false;
        used[i][j] = false;
      }
    nh = 0;
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        char c;
        scanf(" %c", &c);
        if (c == '1') {
          hs[i][j] = true;
          used[i][j] = false;
          nh++;
        }
        if (c == '2') {
          cx = i;
          cy = j;
        }
      }
    }
    ans = 0;
    dfs(cx, cy, 0);
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <stdio.h>
bool hs[10][10];
bool used[10][10];
int cx, cy;
int ans;
int nh;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
int w, h;
void dfs(int x, int y, int f) {
  if (f == nh && (x == cx || y == cy)) {
    ans++;
    return;
  }
  int i, j;
  for (i = 0; i < 4; i++) {
    for (j = 1; j < 10; j++) {
      int nx = x + j * dx[i];
      int ny = y + j * dy[i];
      if (nx >= 0 && nx < h && ny >= 0 && ny < w && hs[nx][ny]) {
        if (!used[nx][ny]) {
          used[nx][ny] = true;
          dfs(nx, ny, f + 1);
          used[nx][ny] = false;
          break;
        }
      }
    }
  }
}
int main() {
  while (1) {
    int i, j;
    scanf("%d %d", &w, &h);
    if (w == 0)
      return 0;
    for (i = 0; i < h; i++)
      for (j = 0; j < w; j++) {
        hs[i][j] = false;
        used[i][j] = false;
      }
    nh = 0;
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        char c;
        scanf(" %c", &c);
        if (c == '1') {
          hs[i][j] = true;
          used[i][j] = false;
          nh++;
        }
        if (c == '2') {
          cx = i;
          cy = j;
        }
      }
    }
    ans = 0;
    dfs(cx, cy, 0);
    printf("%d\\n", ans);
  }
}
Problem p00471 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdio.h>
using namespace std;
int m[10][10];
int R;
int C;
int a, b;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int solve(int s, int t, int u) {
  if (u == 0) {
    if (s == R || t == C)
      return 1;
    else
      return 0;
  }
  int ret = 0;
  for (int i = 0; i < 4; i++) {
    int row = s;
    int col = t;
    while (1) {
      row += dx[i];
      col += dy[i];
      if (row < 0 || col < 0 || row >= a || col >= b)
        break;
      if (m[row][col] == 1) {
        m[row][col] = 3;
        ret += solve(row, col, u - 1);
        m[row][col] = 1;
        break;
      }
    }
  }
  return ret;
}
int main() {
  while (scanf("%d%d", &b, &a), a) {
    int c = 0;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++) {
        scanf("%d", &m[i][j]);
        if (m[i][j] == 1)
          c++;
        if (m[i][j] == 2) {
          R = i;
          C = j;
        }
      }
    printf("%d\\n", solve(R, C, c));
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int dat[10][10];
int a, b;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
int n;
int solve(int x, int y, int d) {
  //	printf("%d %d %d\\n",x,y,d);
  int ret = 0;
  for (int i = 0; i < 4; i++) {
    int X = x;
    int Y = y;
    while (1) {
      X += dx[i];
      Y += dy[i];
      if (!(X >= 0 && X < b && Y >= 0 && Y < a))
        break;
      if (dat[X][Y] == 2) {
        if (d == n) {
          ret++;
          break;
        }
      }
      if (dat[X][Y] == 1) {
        dat[X][Y] = -1;
        ret += solve(X, Y, d + 1);
        dat[X][Y] = 1;
        break;
      }
    }
  }
  return ret;
}
int main() {
  while (scanf("%d%d", &a, &b), a + b) {
    for (int i = 0; i < b; i++) {
      for (int j = 0; j < a; j++) {
        scanf("%d", &dat[i][j]);
      }
    }
    n = 0;
    for (int i = 0; i < b; i++)
      for (int j = 0; j < a; j++)
        if (dat[i][j] == 1)
          n++;
    for (int i = 0; i < b; i++) {
      for (int j = 0; j < a; j++) {
        if (dat[i][j] == 2)
          printf("%d\\n", solve(i, j, 0));
      }
    }
  }
}
Problem p00472 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;
int dist[100001];
int distC[100001];
int moving[100000];
int pm(int value) {
  if (value < 0)
    return -1;
  else
    return 1;
}
int main() {
  // your code goes here
  int n, m, now = 1, sum = 0;
  cin >> n >> m;
  for (int i = 2; i <= n; i++) {
    cin >> dist[i];
    distC[i] = distC[i - 1] + dist[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> moving[i];
    int a = now;
    now += moving[i];
    int b = min(now, a);
    a = max(now, a);
    sum = (sum + distC[a] - distC[b]) % 100000;
  }
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;
int dist[100001];
int distC[100001];
int moving[100000];
int pm(int value) {
  if (value < 0)
    return -1;
  else
    return 1;
}
int main() {
  while (true) {
    // your code goes here
    int n, m, now = 1, sum = 0;
    cin >> n >> m;
    if (n == 0 & m == 0)
      break;
    for (int i = 2; i <= n; i++) {
      cin >> dist[i];
      distC[i] = distC[i - 1] + dist[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> moving[i];
      int a = now;
      now += moving[i];
      int b = min(now, a);
      a = max(now, a);
      sum += distC[a] - distC[b];
    }
    cout << sum << endl;
  }
  return 0;
}