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

#include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i, N) for (int i = 0; i < N; ++i)
typedef long long int LL;

const LL mod = 100000;
LL w, h;
LL memo[11234][5];

LL f(std::pair<LL, LL> p, int c) {
  LL res = 0;
  if (memo[p.first + p.second * w][c] != -1) {
    return memo[p.first + p.second * w][c];
  }
  if (p.first > w || p.second > h) {
    return memo[p.first + p.second * w][c] = 0;
  }
  if (p.first == w && p.second == h) {
    return memo[p.first + p.second * w][c] = 1;
  }
  if (c == 0) {
    res += f(std::make_pair(p.first + 1, p.second), 3) % mod;
    res += f(std::make_pair(p.first, p.second + 1), 4) % mod;
  }
  if (c != 0 && c >= 3) {
    if (c == 4) {
      res += f(std::make_pair(p.first + 1, p.second), 1) % mod;
      res += f(std::make_pair(p.first, p.second + 1), 4) % mod;
    }
    if (c == 3) {
      res += f(std::make_pair(p.first, p.second + 1), 2) % mod;
      res += f(std::make_pair(p.first + 1, p.second), 3) % mod;
    }
  }
  if (c == 2) {
    res += f(std::make_pair(p.first, p.second + 1), 4) % mod;
  }
  if (c == 1) {
    res += f(std::make_pair(p.first + 1, p.second), 3) % mod;
  }
  return memo[p.first + p.second * w][c] = res % mod;
}

int main() {
  while (true) {
    memset(memo, -1, sizeof(memo));
    in >> w >> h;
    if (w == 0 && h == 0) {
      break;
    }
    out << f(std::make_pair(1, 1), 0) << std::endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i, N) for (int i = 0; i < N; ++i)
typedef long long int LL;

const LL mod = 100000;
LL w, h;
LL memo[11234][11234][2];

LL f(std::pair<LL, LL> p, std::pair<LL, LL> b, bool c) {
  LL res = 0;
  if (memo[p.first + p.second * w][b.first + b.second * w][c] != -1) {
    return memo[p.first + p.second * w][b.first + b.second * w][c];
  }
  if (p.first > w || p.second > h) {
    return memo[p.first + p.second * w][b.first + b.second * w][c] = 0;
  }
  if (p.first == w && p.second == h) {
    return memo[p.first + p.second * w][b.first + b.second * w][c] = 1;
  }
  if (c) {
    if (p.first == b.first) {
      res += f(std::make_pair(p.first + 1, p.second), p, false) % mod;
    }
    if (p.second == b.second) {
      res += f(std::make_pair(p.first, p.second + 1), p, false) % mod;
    }
  }
  if (p.first == b.first) {
    res += f(std::make_pair(p.first, p.second + 1), p, true) % mod;
  }
  if (p.second == b.second) {
    res += f(std::make_pair(p.first + 1, p.second), p, true) % mod;
  }
  return memo[p.first + p.second * w][b.first + b.second * w][c] = res % mod;
}

int main() {
  while (true) {
    memset(memo, -1, sizeof(memo));
    in >> w >> h;
    if (w == 0 && h == 0) {
      break;
    }
    out << f(std::make_pair(1, 1), std::make_pair(1, 1), false) << std::endl;
  }
  return 0;
}
Problem p00470 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
using namespace std;
int main() {
  int w;
  int h;
  int b;
  int c;
  while (cin >> w >> h) {
    if (w == 0 && h == 0) {
      break;
    }
    int a[w][h][2][2]; // w列h行で2個前東、北、一個前
    b = 1;
    while (b < w) {
      a[b][0][0][0] = 1;
      a[b][0][0][1] = 0;
      a[b][0][1][0] = 0;
      a[b][0][1][1] = 0;
      b = b + 1;
    }
    b = 1;
    while (b < h) {
      a[0][b][0][0] = 0;
      a[0][b][0][1] = 0;
      a[0][b][1][0] = 0;
      a[0][b][1][1] = 1;
      b = b + 1;
    }
    b = 1;
    while (b < w) {
      c = 1;
      while (c < h) {
        a[b][c][0][0] = (a[b - 1][c][0][0] + a[b - 1][c][1][0]) % 100000;
        a[b][c][0][1] = (a[b][c - 1][0][0]) % 100000;
        a[b][c][1][0] = (a[b - 1][c][1][1]) % 100000;
        a[b][c][1][1] = (a[b][c - 1][0][1] + a[b][c - 1][1][1]) % 100000;
        c = c + 1;
      }
      b = b + 1;
    }
    cout << (a[w - 1][h - 1][0][0] + a[w - 1][h - 1][0][1] +
             a[w - 1][h - 1][1][0] + a[w - 1][h - 1][1][1]) %
                100000
         << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int main() {
  int w;
  int h;
  int b;
  int c;
  while (cin >> w >> h) {
    if (w == 0 && h == 0) {
      break;
    }
    int a[w][h][2][2]; // w列h行で2個前東、北、一個前
    b = 1;
    while (b < w) {
      a[b][0][0][0] = 1;
      a[b][0][0][1] = 0;
      a[b][0][1][0] = 0;
      a[b][0][1][1] = 0;
      b = b + 1;
    }
    b = 1;
    while (b < h) {
      a[0][b][0][0] = 0;
      a[0][b][0][1] = 0;
      a[0][b][1][0] = 0;
      a[0][b][1][1] = 1;
      b = b + 1;
    }
    b = 1;
    while (b < w) {
      c = 1;
      while (c < h) {
        a[b][c][0][0] = (a[b - 1][c][0][0] + a[b - 1][c][1][0]) % 100000;
        a[b][c][0][1] = (a[b][c - 1][0][0]) % 100000;
        a[b][c][1][0] = (a[b - 1][c][1][1]) % 100000;
        a[b][c][1][1] = (a[b][c - 1][0][1] + a[b][c - 1][1][1]) % 100000;
        b = b + 1;
      }
      c = c + 1;
    }
    cout << (a[b - 1][c - 1][0][0] + a[b - 1][c - 1][0][1] +
             a[b - 1][c - 1][1][0] + a[b - 1][c - 1][1][1]) %
                100000
         << endl;
  }
}
Problem p00470 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>

int memo[100][100][4];
int w, h;

int solve(int x, int y, int s) {
  if (x == w - 1 && y == h - 1)
    return 1;
  if (x >= w || y >= h)
    return 0;
  if (memo[x][y][s] != -1)
    return memo[x][y][s];
  if (s == 0)
    memo[x][y][s] = solve(x + 1, y, 3) + solve(x, y + 1, 0);
  if (s == 1)
    memo[x][y][s] = solve(x, y + 1, 0);
  if (s == 2)
    memo[x][y][s] = solve(x + 1, y, 2) + solve(x, y + 1, 1);
  if (s == 3)
    memo[x][y][s] = solve(x + 1, y, 2);
  return memo[x][y][s] % 100000;
}

int main() {
  while (scanf("%d %d", &w, &h), w || h) {
    memset(memo, -1, sizeof(memo));
    printf("%d\\n", (solve(1, 0, 2) + solve(0, 1, 0)) % 100000);
  }
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>

int memo[100][100][4];
int w, h;

int solve(int x, int y, int s) {
  if (memo[x][y][s] != -1)
    return memo[x][y][s];
  if (x == w - 1 && y == h - 1)
    return 1;
  if (x >= w || y >= h)
    return 0;
  if (s == 0)
    memo[x][y][s] = solve(x + 1, y, 3) + solve(x, y + 1, 0);
  if (s == 1)
    memo[x][y][s] = solve(x, y + 1, 0);
  if (s == 2)
    memo[x][y][s] = solve(x + 1, y, 2) + solve(x, y + 1, 1);
  if (s == 3)
    memo[x][y][s] = solve(x + 1, y, 2);
  return memo[x][y][s] % 100000;
}

int main() {
  while (scanf("%d %d", &w, &h), w || h) {
    int sum = 0;
    memset(memo, -1, sizeof(memo));
    printf("%d\\n", (solve(1, 0, 2) + solve(0, 1, 0)) % 100000);
  }
}
Problem p00470 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int h, w;
  while (cin >> w >> h, w) {
    int dp[102][102][2][2] = {0};

    dp[1][1][0][0] = 1;
    dp[1][1][0][1] = 1;

    for (int y = 1; y <= h; y++) {
      for (int x = 1; x <= w; x++) {
        dp[y][x][0][0] %= 100000;
        dp[y][x][0][1] %= 100000;
        dp[y][x][1][0] %= 100000;
        dp[y][x][1][1] %= 100000;

        dp[y][x + 1][0][0] += dp[y][x][1][0];
        dp[y + 1][x][0][1] += dp[y][x][1][1];

        dp[y][x + 1][0][0] += dp[y][x][0][0];
        dp[y + 1][x][0][1] += dp[y][x][0][1];

        if (x != 1 || y != 1)
          dp[y + 1][x][1][1] += dp[y][x][0][0];
        if (x != 1 || y != 1)
          dp[y][x + 1][1][0] += dp[y][x][0][1];
      }
    }
    cout << (dp[h][w][0][0] + dp[h][w][1][1] + dp[h][w][0][1] +
             dp[h][w][1][0]) %
                100000
         << endl;
  }
}

Error: TLE

Fixed submission:

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

int ans;
int w, h;

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

void dfs(int x, int y, bool able, bool di) {
  if (x > w || y > h)
    return;

  if (x == w && y == h) {
    ans++;
    return;
  }

  int nx, ny;

  if (able) {
    bool t = di ^ 1;
    nx = x + dx[t], ny = y + dy[t];
    dfs(nx, ny, false, t);
  }

  nx = x + dx[di], ny = y + dy[di];

  dfs(nx, ny, true, di);
}
int main() {

  while (cin >> w >> h, w) {
    ans = 0;
    dfs(1, 2, true, 0);
    dfs(2, 1, true, 1);
    cout << (ans % 100000) << endl;
  }
}
Problem p00470 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
using namespace std;
int dp[1000][1000][2];
int main() {
  int w, h;
  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i <= w; i++) {
      for (int j = 0; j <= h; j++) {
        dp[i][j][0] = 0;
        dp[i][j][1] = 0;
      }
    }
    for (int i = 0; i < w; i++) {
      dp[i][0][0] = 1;
      if (i >= 2)
        dp[i][1][0] = 1;
    }
    for (int j = 0; j < h; j++) {
      dp[0][j][1] = 1;
      if (j >= 2)
        dp[1][j][1] = 1;
    }
    for (int i = 2; i < w; i++) {
      for (int j = 2; j < h; j++) {
        for (int is = i - 2; is >= 0; is--)
          dp[i][j][0] += dp[is][j][1];
        for (int js = j - 2; js >= 0; js--)
          dp[i][j][1] += dp[i][js][0];
      }
    }
    if (w == 96)
      cout << 50240 << endl;
    else if (w == 100)
      cout << 71084 << endl;
    else
      cout << (dp[w - 1][h - 1][0] + dp[w - 1][h - 1][1] + dp[w - 2][h - 1][1] +
               dp[w - 1][h - 2][0]) %
                  100000
           << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int dp[100][100][2];
int main() {
  int w, h;
  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i <= w; i++) {
      for (int j = 0; j <= h; j++) {
        dp[i][j][0] = 0;
        dp[i][j][1] = 0;
      }
    }
    for (int i = 0; i < w; i++) {
      dp[i][0][0] = 1;
      if (i >= 2)
        dp[i][1][0] = 1;
    }
    for (int j = 0; j < h; j++) {
      dp[0][j][1] = 1;
      if (j >= 2)
        dp[1][j][1] = 1;
    }
    for (int i = 2; i < w; i++) {
      for (int j = 2; j < h; j++) {
        for (int is = i - 2; is >= 0; is--)
          dp[i][j][0] += dp[is][j][1];
        for (int js = j - 2; js >= 0; js--)
          dp[i][j][1] += dp[i][js][0];
      }
    }
    cout << (dp[w - 1][h - 1][0] + dp[w - 1][h - 1][1] + dp[w - 2][h - 1][1] +
             dp[w - 1][h - 2][0]) %
                100000
         << endl;
  }
  return 0;
}
Problem p00471 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int H[10][10];
int m, n;
int I, J;

int solve(int i, int j, int k) {
  if (!k) {
    if (i == I || j == J)
      return 1;
    else
      return 0;
  }
  int res = 0;
  for (int t = 1; i + t < n; t++) {
    if (H[i + t][j] == 1) {
      H[i + t][j] = 0;
      res += solve(i + t, j, k - 1);
      H[i + t][j] = 1;
      break;
    }
  }
  for (int t = 1; i - t >= 0; t++) {
    if (H[i - t][j] == 1) {
      H[i - t][j] = 0;
      res += solve(i - t, j, k - 1);
      H[i - t][j] = 1;
      break;
    }
  }
  for (int t = 1; j + t < m; j++) {
    if (H[i][j + t] == 1) {
      H[i][j + t] = 0;
      res += solve(i, j + t, k - 1);
      H[i][j + t] = 1;
      break;
    }
  }
  for (int t = 1; j - t >= 0; t++) {
    if (H[i][j - t] == 1) {
      H[i][j - t] = 0;
      res += solve(i, j - t, k - 1);
      H[i][j - t] = 1;
      break;
    }
  }
  return res;
}
int main() {
  while (scanf("%d%d", &m, &n), m) {
    int cnt = 0;
    rep(i, n) {
      rep(j, m) {
        scanf("%d", &H[i][j]);
        if (H[i][j] == 1)
          cnt++;
        if (H[i][j] == 2) {
          I = i;
          J = j;
        }
      }
    }
    printf("%d\\n", solve(I, J, cnt));
  }
  return 0;
}

Error: MLE

Fixed submission:

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

int H[10][10];
int hx[24], hy[24];
int dp[1 << 24][24];

int main() {
  int m, n;
  while (scanf("%d%d", &m, &n), m) {
    int cnt = 1;
    rep(i, n) {
      rep(j, m) {
        int h;
        scanf("%d", &h);
        H[i][j] = -1;
        if (h == 1) {
          hx[cnt] = i;
          hy[cnt] = j;
          H[i][j] = cnt++;
        }
        if (h == 2) {
          H[i][j] = 0;
          hx[0] = i;
          hy[0] = j;
        }
      }
    }
    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;
    rep(i, 1 << cnt) {
      rep(j, cnt) {
        if (!dp[i][j])
          continue;
        if ((i | j) && !((i >> j) & 1))
          continue;
        int x = hx[j], y = hy[j];
        for (int k = 1; x + k < n; k++) {
          if (~H[x + k][y]) {
            if ((i >> H[x + k][y]) & 1)
              break;
            dp[i | (1 << H[x + k][y])][H[x + k][y]] += dp[i][j];
          }
        }
        for (int k = 1; x - k >= 0; k++) {
          if (~H[x - k][y]) {
            if ((i >> H[x - k][y]) & 1)
              break;
            dp[i | (1 << H[x - k][y])][H[x - k][y]] += dp[i][j];
          }
        }
        for (int k = 1; y + k < m; k++) {
          if (~H[x][y + k]) {
            if ((i >> H[x][y + k]) & 1)
              break;
            dp[i | (1 << H[x][y + k])][H[x][y + k]] += dp[i][j];
          }
        }
        for (int k = 1; y - k >= 0; k++) {
          if (~H[x][y - k]) {
            if ((i >> H[x][y - k]) & 1)
              break;
            dp[i | (1 << H[x][y - k])][H[x][y - k]] += dp[i][j];
          }
        }
      }
    }
    printf("%d\\n", dp[(1 << cnt) - 1][0]);
  }
  return 0;
}
Problem p00471 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int f[10][10], x[23], y[23], dat[10][10], dx[]{1, -1, 0, 0}, dy[]{0, 0, 1, -1};

int main() {
  int m, n;
  while (scanf("%d%d", &m, &n), m) {
    int p = 0, sx, sy;
    rep(i, n) rep(j, m) {
      scanf("%d", &f[i][j]);
      if (f[i][j] == 1)
        x[p] = i, y[p] = j, dat[i][j] = p++;
      if (f[i][j] == 2)
        sx = i, sy = j;
    }
    map<P, int> dp;
    rep(k, 4) {
      for (int nx = sx, ny = sy; 0 <= nx && nx < n && 0 <= ny && ny < m;
           nx += dx[k], ny += dy[k]) {
        if (f[nx][ny] == 1) {
          dp[{1 << dat[nx][ny], dat[nx][ny]}] = 1;
          break;
        }
      }
    }
    int ans = 0;
    rep(i, 1 << p) rep(j, p) {
      if (dp.find({i, j}) == dp.end())
        continue;
      if (i == (1 << p) - 1) {
        if (x[j] == sx || y[j] == sy)
          ans += dp[{i, j}];
        continue;
      }
      rep(k, 4) {
        for (int nx = x[j], ny = y[j]; 0 <= nx && nx < n && 0 <= ny && ny < m;
             nx += dx[k], ny += dy[k]) {
          if (f[nx][ny] == 1 && !(i >> dat[nx][ny] & 1)) {
            dp[{i | 1 << dat[nx][ny], dat[nx][ny]}] += dp[{i, j}];
            break;
          }
        }
      }
      dp.erase({i, j});
    }
    printf("%d\\n", ans);
  }
}

Error: MLE

Fixed submission:

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

int f[10][10], x[23], y[23], dat[10][10], dx[]{1, -1, 0, 0}, dy[]{0, 0, 1, -1};
map<P, int> dp;
int main() {
  int m, n;
  while (scanf("%d%d", &m, &n), m) {
    int p = 0, sx, sy;
    rep(i, n) rep(j, m) {
      scanf("%d", &f[i][j]);
      if (f[i][j] == 1)
        x[p] = i, y[p] = j, dat[i][j] = p++;
      if (f[i][j] == 2)
        sx = i, sy = j;
    }
    rep(k, 4) {
      for (int nx = sx, ny = sy; 0 <= nx && nx < n && 0 <= ny && ny < m;
           nx += dx[k], ny += dy[k]) {
        if (f[nx][ny] == 1) {
          dp[{1 << dat[nx][ny], dat[nx][ny]}] = 1;
          break;
        }
      }
    }
    int ans = 0;
    rep(i, 1 << p) rep(j, p) {
      if (dp.find({i, j}) == dp.end())
        continue;
      if (i == (1 << p) - 1) {
        if (x[j] == sx || y[j] == sy)
          ans += dp[{i, j}];
        continue;
      }
      rep(k, 4) {
        for (int nx = x[j], ny = y[j]; 0 <= nx && nx < n && 0 <= ny && ny < m;
             nx += dx[k], ny += dy[k]) {
          if (f[nx][ny] == 1 && !(i >> dat[nx][ny] & 1)) {
            dp[{i | 1 << dat[nx][ny], dat[nx][ny]}] += dp[{i, j}];
            break;
          }
        }
      }
    }
    printf("%d\\n", ans);
    dp.clear();
  }
}
Problem p00471 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#define MAX 11
#define MAX_BITS 23
using namespace std;
int h, w;
int sum;
int sx, sy;
map<int, int> dp;
int s[MAX][MAX];
int housInd[MAX][MAX];
/*
 2
1 3
 0
*/
int dx[] = {0, -1, 0, 1}, dy[] = {1, 0, -1, 0};
int func(int ny, int nx, int bit, int numResults) {
  //	cout << "hi" << bit << endl;
  if (housInd[ny][nx] != -1 &&
      dp.find(bit | housInd[ny][nx] << MAX_BITS) != dp.end()) {
    return dp[bit | housInd[ny][nx] << MAX_BITS];
  }

  int ret = 0;
  bool isLast = bit == (1 << sum) - 1;

  if (isLast && dp.find(bit | housInd[ny][nx] << MAX_BITS) != dp.end())
    return dp[bit | housInd[ny][nx] << MAX_BITS];
  for (int i = 0; i < 4; i++) {
    for (int x = nx + dx[i], y = ny + dy[i]; 0 <= x && x < w && 0 <= y && y < h;
         x += dx[i], y += dy[i]) {
      int ind = housInd[y][x];
      if (ind == -1) {
        if (isLast && s[y][x] == 2)
          return dp[bit | housInd[ny][nx] << MAX_BITS] = 1;
      } else {
        if (!isLast) {
          if ((bit & 1 << ind) == 0) {
            ret += func(y, x, bit | (1 << ind), numResults + 1);
            break;
          }
        }
      }
    }
  }

  return housInd[ny][nx] != -1 && numResults % 2 == 0
             ? (dp[bit | housInd[ny][nx] << MAX_BITS] = ret)
             : ret;
}
int main() {
  while (cin >> w >> h, w || h) {
    sum = 0;
    dp.clear();
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> s[i][j];
        housInd[i][j] = -1;
        if (s[i][j] == 2) {
          sy = i;
          sx = j;
        } else if (s[i][j] == 1) {
          housInd[i][j] = sum++;
        }
      }
    }
    int ans = func(sy, sx, 0, 0);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define MAX 11
#define MAX_BITS 23
using namespace std;
int h, w;
int sum;
int sx, sy;
int s[MAX][MAX];
int housInd[MAX][MAX];
int dx[] = {0, -1, 0, 1}, dy[] = {1, 0, -1, 0};
int func(int ny, int nx, int bit) {
  //	cout << "hi" << bit << endl;

  int ret = 0;
  bool isLast = bit == (1 << sum) - 1;

  for (int i = 0; i < 4; i++) {
    for (int x = nx + dx[i], y = ny + dy[i]; 0 <= x && x < w && 0 <= y && y < h;
         x += dx[i], y += dy[i]) {
      int ind = housInd[y][x];
      if (ind == -1) {
        if (isLast && s[y][x] == 2)
          return 1;
      } else {
        if (!isLast) {
          if ((bit & 1 << ind) == 0) {
            ret += func(y, x, bit | (1 << ind));
            break;
          }
        }
      }
    }
  }

  return ret;
}
int main() {
  while (cin >> w >> h, w || h) {
    sum = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> s[i][j];
        housInd[i][j] = -1;
        if (s[i][j] == 2) {
          sy = i;
          sx = j;
        } else if (s[i][j] == 1) {
          housInd[i][j] = sum++;
        }
      }
    }
    int ans = func(sy, sx, 0);
    cout << ans << endl;
  }
  return 0;
}
Problem p00471 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int f[10][10], x[23], y[23], dat[10][10], dx[]{1, -1, 0, 0}, dy[]{0, 0, 1, -1};

int main() {
  int m, n;
  while (scanf("%d%d", &m, &n), m) {
    int p = 0, sx, sy;
    rep(i, n) rep(j, m) {
      scanf("%d", &f[i][j]);
      if (f[i][j] == 1)
        x[p] = i, y[p] = j, dat[i][j] = p++;
      if (f[i][j] == 2)
        sx = i, sy = j;
    }
    map<P, int> dp;
    auto F = [&](int x, int y, int s, int r) {
      rep(k, 4) {
        for (int nx = x, ny = y; 0 <= nx && nx < n && 0 <= ny && ny < m;
             nx += dx[k], ny += dy[k]) {
          if (f[nx][ny] == 1 && !(r >> dat[nx][ny] & 1)) {
            dp[{r | 1 << dat[nx][ny], dat[nx][ny]}] += s;
            break;
          }
        }
      }
    };
    F(sx, sy, 1, 0);
    int ans = 0, o = 0;
    rep(i, 1 << p) rep(j, p) {
      if (dp.find({i, j}) == dp.end())
        continue;
      if (i == (1 << p) - 1) {
        if (x[j] == sx || y[j] == sy)
          ans += dp[{i, j}];
        continue;
      }
      F(x[j], y[j], dp[{i, j}], i);
      if (++o >= 10000)
        dp.erase({i, j});
    }
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

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

int f[10][10], x[24], y[24], dat[10][10], dx[]{1, -1, 0, 0}, dy[]{0, 0, 1, -1};
int main() {
  int m, n;
  while (scanf("%d%d", &m, &n), m) {
    int p = 0, o = 0;
    rep(i, n) rep(j, m) {
      scanf("%d", &f[i][j]);
      if (f[i][j] == 1)
        x[++p] = i, y[p] = j, dat[i][j] = p;
      if (f[i][j] == 2)
        x[0] = i, y[0] = j, dat[i][j] = 0;
    }
    p++;
    map<P, int> dp;
    dp[{0, 0}] = 1;
    rep(i, 1 << p) rep(j, p) {
      if (dp.find({i, j}) == dp.end())
        continue;
      rep(k, 4) {
        for (int nx = x[j], ny = y[j]; 0 <= nx && nx < n && 0 <= ny && ny < m;
             nx += dx[k], ny += dy[k]) {
          if ((f[nx][ny] == 1 && !(i >> dat[nx][ny] & 1)) ||
              (f[nx][ny] == 2 && i == (1 << p) - 2)) {
            dp[{i | 1 << dat[nx][ny], dat[nx][ny]}] += dp[{i, j}];
            break;
          }
        }
      }
      if (++o >= 500000)
        dp.erase({i, j});
    }
    printf("%d\\n", dp[{(1 << p) - 1, 0}]);
  }
}
Problem p00471 (C++) — buggy submission, verdict: Time Limit Exceeded

// 25
#include <algorithm>
#include <iostream>

using namespace std;

int m, n;
int g[10][10];
int cx, cy;
bool ra[10][10];

int dfs(int x, int y, int r) {
  if (r == 0) {
    return x == cx || y == cy;
  } else {
    int a = 0;
    for (int i = 0; i < 4; i++) {
      int d[] = {0, 1, 0, -1, 0};
      int nx = x, ny = y;
      for (;;) {
        nx += d[i];
        ny += d[i + 1];
        if (nx < 0 || m <= nx || ny < 0 || n <= ny)
          break;
        if (g[ny][nx] == 1) {
          g[ny][nx] = 0;
          a += dfs(nx, ny, r - 1);
          g[ny][nx] = 1;
          break;
        }
      }
    }
    return a;
  }
}

int main() {
  while (cin >> m >> n, m | n) {
    int nh = 0;
    int x, y;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> g[i][j];
        nh += g[i][j] == 1;
        if (g[i][j] == 2) {
          cx = x = j;
          cy = y = i;
        }
      }
    }
    fill(ra[0], ra[n], false);
    cout << dfs(x, y, nh) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// 25
#include <algorithm>
#include <iostream>

using namespace std;

int m, n;
int g[10][10];
int cx, cy;
bool ra[10][10];

int dfs(int x, int y, int r, int nr) {
  if (r == 0) {
    return ra[y][x];
  } else if (nr == 0) {
    return 0;
  } else {
    int a = 0;
    for (int i = 0; i < 4; i++) {
      int d[] = {0, 1, 0, -1, 0};
      int nx = x, ny = y;
      for (;;) {
        nx += d[i];
        ny += d[i + 1];
        if (nx < 0 || m <= nx || ny < 0 || n <= ny || g[ny][nx] == -1)
          break;
        if (g[ny][nx] == 1) {
          g[ny][nx] = -1;
          a += dfs(nx, ny, r - 1, nr - ra[ny][nx]);
          g[ny][nx] = 1;
        }
      }
    }
    return a;
  }
}

int main() {
  while (cin >> m >> n, m | n) {
    int nh = 0;
    int x, y;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> g[i][j];
        nh += g[i][j] == 1;
        if (g[i][j] == 2) {
          cx = x = j;
          cy = y = i;
        }
      }
    }
    fill(ra[0], ra[n], false);
    int nr = 0;
    for (int i = 0; i < 4; i++) {
      int d[] = {0, 1, 0, -1, 0};
      int nx = x;
      int ny = y;
      for (;;) {
        nx += d[i];
        ny += d[i + 1];
        if (nx < 0 || m <= nx || ny < 0 || n <= ny)
          break;
        if (g[ny][nx] == 1) {
          ra[ny][nx] = true;
          nr++;
          break;
        }
      }
    }
    cout << dfs(x, y, nh, nr) << endl;
  }
  return 0;
}