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

#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

int solve(int, int, int);

int n, k, cnt;
int d[102];
int memo[102][4][4];

int main() {
  cin >> n >> k;

  for (int i = 0; i < k; i++) {
    int tmp;
    cin >> tmp;
    cin >> d[tmp];
  }
  cout << solve(1, 0, 0) << endl;
  return 0;
}

int solve(int day, int old_type, int type) {
  int rec = 0;
  if (memo[day][old_type][type] != 0)
    return memo[day][old_type][type];

  if (day == n + 1) {
    rec = 1;
  } else if (d[day] != 0) {
    if (old_type == type && type == d[day])
      rec = 0;
    else
      rec = solve(day + 1, type, d[day]);
  } else {
    for (int i = 1; i <= 3; i++) {
      if (old_type == type && type == i)
        continue;
      rec += solve(day + 1, type, i);
    }
  }
  return memo[day][old_type][type] = rec % 10000;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int solve(int, int, int);

int n, k, cnt;
vector<int> d;
int memo[101][4][4];

int main() {
  cin >> n >> k;
  d.resize(n);

  for (int i = 0; i < k; i++) {
    int tmp;
    cin >> tmp;
    cin >> d[tmp];
  }
  cout << solve(1, 0, 0) << endl;
  return 0;
}

int solve(int day, int old_type, int type) {
  int rec = 0;
  if (memo[day][old_type][type] != 0)
    return memo[day][old_type][type];

  if (day == n + 1) {
    rec = 1;
  } else if (d[day] != 0) {
    if (old_type == type && type == d[day])
      rec = 0;
    else
      rec = solve(day + 1, type, d[day]);
  } else {
    for (int i = 1; i <= 3; i++) {
      if (old_type == type && type == i)
        continue;
      rec += solve(day + 1, type, i);
    }
  }
  return memo[day][old_type][type] = rec % 10000;
}
Problem p00491 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

inline int to_int(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string to_str(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef long long ll;

ll ans;
int plan[128];
int memo[128][4][4];
int num;

// n : day, next : kind of pasta, c : 今日選んだパスタが何日続いたか
int pasta(int n, int prev, int c) {
  if (c == 2) {
    return 0;
  } else if (n == num) {
    return 1;
  } else if (memo[n][prev][c]) {
    return memo[n][prev][c];
  }
  int ans = 0;
  if (plan[n + 1] != 0) {
    if (plan[n + 1] == prev)
      ans = pasta(n + 1, plan[n + 1], c + 1);
    else
      ans = pasta(n + 1, plan[n + 1], 0);
  } else {
    for (int i = 1; i <= 3; i++) {
      if (prev == i)
        ans += pasta(n + 1, i, c + 1);
      else
        ans += pasta(n + 1, i, 0);
    }
  }

  return memo[n][prev][c] = ans % 10000;
}

int main() {
  int K, wk, tk;
  ans = 0;
  cin >> num >> K;
  for (int i = 0; i < K; i++) {
    cin >> wk >> tk;
    plan[wk] = tk;
  }
  if (plan[1] == 0) {
    cout << pasta(0, 0, 0) << endl;
  } else {
    cout << pasta(1, plan[1], 0) << endl;
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

inline int to_int(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string to_str(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef long long ll;

ll ans;
int plan[128];
int memo[4][128];
int num;

// n : day, next : kind of pasta, c : 今日選んだパスタが何日続いたか
int pasta(int n, int prev, int c) {
  if (c == 2) {
    return 0;
  } else if (n == num) {
    return 1;
  } else if (plan[n + 1] != 0) {
    if (plan[n + 1] == prev)
      return pasta(n + 1, plan[n + 1], c + 1);
    else
      return pasta(n + 1, plan[n + 1], 0);
  } else {
    int num = 0;
    for (int i = 1; i <= 3; i++) {
      if (prev == i)
        num += pasta(n + 1, i, c + 1);
      else
        num += pasta(n + 1, i, 0);
    }
    return num;
  }
}

int main() {
  int K, wk, tk;
  ans = 0;
  cin >> num >> K;
  for (int i = 0; i < K; i++) {
    cin >> wk >> tk;
    plan[wk] = tk;
  }
  if (plan[1] == 0) {
    cout << pasta(0, 0, 0) % 10000 << endl;
  } else {
    cout << pasta(1, plan[1], 0) % 10000 << endl;
  }
}
Problem p00491 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long

using namespace std;

int n, k, q[110];
int mem[110][5][5];
static const int mod = 10000;
int dfs(int day, int p, int pp) {
  int ans = 0;
  if (day == 0)
    return 1;
  if (mem[day][p][pp] != -1)
    return mem[day][p][pp];
  if (q[day] != 0) {
    if (p == pp && q[day] == p)
      return 0;
    ans = dfs(day - 1, q[day], p);
    return mem[day][p][pp] = ans;
  }
  for (int i = 1; i <= 3; i++) {
    if (p == pp && p == i)
      continue;
    ans += dfs(day - 1, i, p);
  }
  ans = ans % mod;
  mem[day][p][pp] = ans;
  return ans;
}

signed main() {
  cin >> n >> k;
  memset(mem, -1, sizeof(mem));
  int a, b;
  for (int i = 1; i <= k; ++i) {
    cin >> a >> b;
    q[a] = b;
  }
  cout << dfs(n, 0, 0) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long

using namespace std;

int n, k, q[110];
int mem[110][5][5];
static const int mod = 10000;
int dfs(int day, int p, int pp) {
  if (day == 0)
    return 1;
  if (mem[day][p][pp] != -1)
    mem[day][p][pp];
  int ans = 0;
  if (q[day] != 0) {
    if (p == pp && q[day] == p)
      return 0;
    ans = dfs(day - 1, q[day], p);
    return ans;
  }
  for (int i = 1; i <= 3; i++) {
    if (p == pp && p == i)
      continue;
    ans += dfs(day - 1, i, p);
  }
  ans = ans % mod;
  mem[day][p][pp] = ans;
  return ans;
}

signed main() {
  cin >> n >> k;
  memset(mem, -1, sizeof(mem));
  int a, b;
  for (int i = 1; i <= k; ++i) {
    cin >> a >> b;
    q[a] = b;
  }
  cout << dfs(n, 0, 0) << endl;
}
Problem p00492 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;

int w, h;
int a[102][102];
int odx[6] = {1, 1, 1, 0, -1, 0};
int edx[6] = {0, 1, 0, -1, -1, -1};
int dy[6] = {-1, 0, 1, 1, 0, -1};
int ans;
typedef pair<int, int> P;
queue<P> q;

void bfs() {
  q.push(P(0, 0));
  a[0][0] = -1;
  while (q.size()) {
    P p = q.front();
    q.pop();
    for (int i = 0; i < 6; i++) {
      int nx, ny;
      ny = p.second + dy[i];
      if (p.second % 2 == 1) {
        nx = p.first + odx[i];
      } else {
        nx = p.first + edx[i];
      }
      if (0 <= nx && nx < w + 2 && 0 <= ny && ny < h + 2 && a[nx][ny] != -1) {
        if (a[nx][ny] == 0) {
          q.push(P(nx, ny));
          a[nx][ny] = -1;
        } else {
          ans++;
        }
      }
    }
  }
}

int main() {
  scanf("%d %d", &w, &h);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      scanf("%d", &a[j + 1][i + 1]);
    }
  }
  bfs();
  printf("%d\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
using namespace std;

int w, h;
int a[100][100];
int odx[6] = {1, 1, 1, 0, -1, 0};
int edx[6] = {0, 1, 0, -1, -1, -1};
int dy[6] = {-1, 0, 1, 1, 0, -1};
int ans;

bool dfs(int x, int y) {
  if (x < 0 || w <= x || y < 0 || h <= y)
    return true;
  if (a[x][y] == -1)
    return true;
  if (a[x][y] == 1)
    return false;
  for (int i = 0; i < 6; i++) {
    int nx, ny;
    if (y % 2 == 0)
      nx = x + odx[i], ny = y + dy[i];
    else
      nx = x + edx[i], ny = y + dy[i];
    if ((x != nx || y != ny) && dfs(nx, ny))
      return true;
  }
  return false;
}

int main() {
  scanf("%d %d", &w, &h);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      scanf("%d", &a[j][i]);
    }
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (dfs(j, i))
        a[j][i] = -1;
      ;
    }
  }
  /*
          for(int i=0; i<h; i++){
                  for(int j=0; j<w; j++){
                          printf("%d\\t", a[j][i]);
                  }
                  printf("\\n");
          }
  */
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[j][i] == 1) {
        for (int k = 0; k < 6; k++) {
          int nx, ny;
          if (i % 2 == 0) {
            nx = j + odx[k], ny = i + dy[k];
          } else {
            ny = j + edx[k], ny = i + dy[k];
          }
          if (0 <= nx && nx < w && 0 <= ny && ny < h) {
            if (a[nx][ny] == -1) {
              ans++;
            }
          } else {
            ans++;
          }
        }
      }
    }
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p00492 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int H, W, cnt, t[102][102];
int dx[2][6] = {{-1, 0, 0, -1, -1, 1}, {1, 1, 1, 0, -1, 0}};

int dy[2][6] = {{0, -1, 1, -1, 1, 0}, {-1, 0, 1, 1, 0, -1}};
void dfs(int y, int x) {
  int nx, ny;
  t[y][x] = -1;
  for (int i = 0; i < 6; i++) {
    nx = x + dx[y % 2][i];
    ny = y + dy[y % 2][i];
    if (nx < 0 || W + 1 < nx || ny < 0 || ny > H + 1)
      continue;
    if (t[ny][nx] == 1) {
      cnt++;
    }
  }
  for (int i = 0; i < 6; i++) {
    nx = x + dx[y % 2][i];
    ny = y + dy[y % 2][i];
    if (nx < 0 || W + 1 < nx || ny < 0 || ny > H + 1)
      continue;
    if (t[ny][nx] == 0) {
      dfs(ny, nx);
    }
  }
}
int main() {

  cin >> W >> H;

  for (int i = 0; i < 102; i++) {
    for (int j = 0; j < 102; j++) {
      t[i][j] = 0;
    }
  }
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> t[i][j];
    }
  }
  cnt = 0;
  dfs(0, 0);
  cout << cnt << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int H, W, cnt, t[102][102];
int dx[2][6] = {{-1, 0, 0, -1, -1, 1}, {1, 1, 1, 0, -1, 0}};

int dy[2][6] = {{0, -1, 1, -1, 1, 0}, {-1, 0, 1, 1, 0, -1}};
void dfs(int y, int x) {
  int nx, ny;
  t[y][x] = -1;
  for (int i = 0; i < 6; i++) {
    nx = x + dx[y % 2][i];
    ny = y + dy[y % 2][i];
    if (nx < 0 || W + 1 < x || ny < 0 || ny > H + 1)
      continue;
    if (t[ny][nx] == 1) {
      cnt++;
    }
  }
  for (int i = 0; i < 6; i++) {
    nx = x + dx[y % 2][i];
    ny = y + dy[y % 2][i];
    if (nx < 0 || W + 1 < x || ny < 0 || ny > H + 1)
      continue;
    if (t[ny][nx] == 0) {
      dfs(ny, nx);
    }
  }
}
int main() {
  while (1) {
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;
    for (int i = 0; i < 102; i++) {
      for (int j = 0; j < 102; j++) {
        t[i][j] = 0;
      }
    }
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        cin >> t[i][j];
      }
    }
    cnt = 0;
    dfs(0, 0);
    cout << cnt << endl;
  }
  return 0;
}
Problem p00492 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <queue>
#include <utility>

using namespace std;

queue<pair<int, int>> q;
int dx[2][6] = {{0, 0, 1, -1, 1, 1}, {0, 0, 1, -1, -1, -1}};
int dy[2][6] = {{-1, 1, 0, 0, -1, 1}, {-1, 1, 0, 0, 1, -1}};
int map[102][102];

int main() {
  int i;
  int w, h;
  int sum = 0;

  scanf("%d %d", &w, &h);
  for (i = 0; i < 102; i++) {
    int j;
    for (j = 0; j < 102; j++) {
      map[i][j] = 0;
    }
  }

  for (i = 0; i < h; i++) {
    int j;
    for (j = 0; j < w; j++) {
      scanf("%d", &map[i + 1][j + 1]);
    }
  }

  q.push(make_pair(0, 0));

  while (!q.empty()) {
    pair<int, int> tmp = q.front();
    q.pop();
    int x = tmp.first;
    int y = tmp.second;

    if (map[y][x] == 1 || map[y][x] == 2)
      continue;
    if (map[y][x] == 0) {
      int k;
      map[y][x] = 2;
      for (k = 0; k < 6; k++) {
        int new_x = x + dx[(y + 1) % 2][k];
        int new_y = y + dy[(y + 1) % 2][k];
        if (new_x < 0 || new_y < 0 || new_x >= 102 || new_y >= 102) {
          continue;
        }
        q.push(make_pair(new_x, new_y));
      }
    }
  }

  for (i = 1; i < h + 1; i++) {
    int j;
    for (j = 1; j < w + 1; j++) {
      int k;
      for (k = 0; k < 6; k++) {
        int new_x = j + dx[(i + 1) % 2][k];
        int new_y = i + dy[(i + 1) % 2][k];

        if (map[i][j] == 1 && map[new_y][new_x] == 2)
          sum++;
      }
    }
  }

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

Error: -11

Fixed submission:

#include <cstdio>
#include <queue>
#include <utility>

using namespace std;

int dx[2][6] = {{0, 0, 1, -1, 1, 1}, {0, 0, 1, -1, -1, -1}};
int dy[2][6] = {{-1, 1, 0, 0, -1, 1}, {-1, 1, 0, 0, 1, -1}};
int map[102][102];

int main() {
  int i;
  int w, h;
  int sum = 0;
  queue<pair<int, int>> q;

  scanf("%d %d", &w, &h);
  for (i = 0; i < h + 2; i++) {
    int j;
    for (j = 0; j < w + 2; j++) {
      map[i][j] = 0;
    }
  }

  for (i = 0; i < h; i++) {
    int j;
    for (j = 0; j < w; j++) {
      scanf("%d", &map[i + 1][j + 1]);
    }
  }

  q.push(make_pair(0, 0));

  while (q.size() > 0) {
    pair<int, int> tmp = q.front();
    int x = tmp.first;
    int y = tmp.second;

    if (map[x][y] == 0) {
      int k;
      map[x][y] = 2;
      for (k = 0; k < 6; k++) {
        int new_x = x + dx[x % 2][k];
        int new_y = y + dy[y % 2][k];
        if (new_x < 0 || new_y < 0 || map[new_x][new_y] != 0) {
          continue;
        }
        q.push(make_pair(new_x, new_y));
      }
    }
    q.pop();
  }

  for (i = 1; i < h; i++) {
    int j;
    for (j = 1; j < w; j++) {
      int k;
      for (k = 0; k < 6; k++) {
        int new_x = j + dx[i % 2][k];
        int new_y = i + dy[i % 2][k];

        if (map[i][j] == 1 && map[new_y][new_x] == 2)
          sum++;
      }
    }
  }

  printf("%d\\n", sum);
}
Problem p00492 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <map>
#include <queue>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define MP make_pair

typedef pair<int, int> PII;

int w, h, maps[102][102];
int dx[2][6] = {{0, 1, 0, -1, -1, -1}, {1, 1, 1, 0, -1, 0}};
int dy[6] = {-1, 0, 1, 1, 0, -1};

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

int main() {
  cin >> w >> h;
  REP(i, h) REP(j, w) scanf("%d", &maps[i + 1][j + 1]);
  w += 2;
  h += 2;

  queue<PII> q;
  q.push(MP(0, 0));

  while (!q.empty()) {
    PII now = q.front();
    q.pop();
    int x = now.first, y = now.second;
    if (maps[y][x] == 2)
      continue;
    maps[y][x] = 2;
    REP(i, 6) {
      int nx = x + dx[y % 2][i];
      int ny = y + dy[i];
      if (check(nx, ny) && maps[ny][nx] == 0)
        q.push(MP(nx, ny));
    }
  }

  int ans = 0;
  FOR(i, 1, h - 1) FOR(j, 1, w - 1) if (maps[i][j] == 1) {
    REP(k, 6) {
      int nx = j + dx[i % 2][k];
      int ny = i + dy[k];
      if (check(nx, ny) && maps[ny][nx] == 2)
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <map>
#include <queue>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define MP make_pair

typedef pair<int, int> PII;

int w, h, maps[102][102];
int dx[2][6] = {{0, 1, 0, -1, -1, -1}, {1, 1, 1, 0, -1, 0}};
int dy[6] = {-1, 0, 1, 1, 0, -1};

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

int main() {
  cin >> w >> h;
  REP(i, h) REP(j, w) scanf("%d", &maps[i + 1][j + 1]);
  w += 2;
  h += 2;

  queue<PII> q;
  q.push(MP(0, 0));

  while (!q.empty()) {
    PII now = q.front();
    q.pop();
    int x = now.first, y = now.second;
    maps[y][x] = 2;
    REP(i, 6) {
      int nx = x + dx[y % 2][i];
      int ny = y + dy[i];
      if (check(nx, ny) && maps[ny][nx] == 0)
        q.push(MP(nx, ny));
    }
  }

  int ans = 0;
  FOR(i, 1, h - 1) FOR(j, 1, w - 1) if (maps[i][j] == 1) {
    REP(k, 6) {
      int nx = j + dx[i % 2][k];
      int ny = i + dy[k];
      if (check(nx, ny) && maps[ny][nx] == 2)
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00492 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>

using namespace std;

struct position {
  int x, y;
};

int w, h, ans = 0;
bool building[102][102], pushed[102][102];
queue<position> wait;
/*wjx,hiy*/
int dx[2][6] = {{-1, 0, -1, 1, -1, 0}, {0, 1, -1, 1, 0, 1}};
int dy[6] = {-1, -1, 0, 0, 1, 1};

int solve(position now) {
  int total = 0;
  for (int i = 0; i < 6; i++) {
    position next = {now.x + dx[now.y % 2][i], now.y + dy[i]};
    if (0 <= next.x && next.x <= w + 1 && 0 <= next.y && next.y <= h + 1) {
      if (building[next.x][next.y]) {
        total++;
      } else if (!pushed[next.x][next.y]) {
        pushed[next.x][next.y] = true;
        wait.push(next);
      }
    }
  }
  return total;
}

int main() {
  cin >> w >> h;
  for (int i = 0; i <= h + 1; i++) {
    for (int j = 0; j <= w + 1; j++) {
      building[j][i] = false;
      pushed[j][i] = false;
    }
  }
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> building[j][i];
    }
  }

  position start = {0, 0};
  wait.push(start);

  while (!wait.empty()) {
    ans += solve(wait.front());
    wait.pop();
  }

  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <queue>
#define plusy now.y + dy[now.y % 2][i]
#define plusx now.x + dx[now.y % 2][i]
using namespace std;

struct position {
  int y, x;
};

int w, h, answer = 0;
bool building[102][102], check[102][102];
queue<position> search;
int dx[2][6] = {{-1, -1, -1, 0, 0, 1}, {-1, 0, 0, 1, 1, 1}};
int dy[2][6] = {{-1, 0, 1, -1, 1, 0}, {0, -1, 1, -1, 0, 1}};

int solve(position now) {
  int total = 0;
  for (int i = 0; i < 6; i++) {
    if (building[plusy][plusx]) {
      total++;
    } else if (0 <= plusy && plusy <= h + 1 && 0 <= plusx && plusx <= w + 1 &&
               !check[plusy][plusx]) {
      check[plusy][plusx] = true;
      position in = {plusy, plusx};
      search.push(in);
    }
  }
  return total;
}

int main() {
  cin >> w >> h;
  for (int j = 0; j <= h + 1; j++) {
    for (int i = 0; i <= w + 1; i++) {
      building[j][i] = false;
      check[j][i] = false;
    }
  }
  for (int j = 1; j <= h; j++) {
    for (int i = 1; i <= w; i++) {
      cin >> building[j][i];
    }
  }

  position start = {0, 0};
  search.push(start);

  while (!search.empty()) {
    answer += solve(search.front());
    search.pop();
  }

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

#include <iostream>
using namespace std;
int H, W, cnt, t[102][102], vis[102][102];
int dx[2][6] = {{-1, 0, 0, -1, -1, 1}, {1, 1, 1, 0, -1, 0}};

int dy[2][6] = {{0, -1, 1, -1, 1, 0}, {-1, 0, 1, 1, 0, -1}};
void dfs(int y, int x) {
  int nx, ny;

  t[y][x] = -1;
  for (int i = 0; i < 6; i++) {
    nx = x + dx[y % 2][i];
    ny = y + dy[y % 2][i];
    if (nx < 0 || W + 1 < nx || ny < 0 || ny > H + 1)
      continue;
    if (t[ny][nx] == 1) {
      cnt++;
    }
    if (t[ny][nx] == 0) {
      dfs(ny, nx);
    }
  }
}
int main() {
  cin >> W >> H;
  for (int i = 0; i < 102; i++) {
    for (int j = 0; j < 102; j++) {
      t[i][j] = 0;
    }
  }
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> t[i][j];
    }
  }
  cnt = 0;
  dfs(0, 0);
  cout << cnt << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int H, W, cnt, t[102][102];
int dx[2][6] = {{-1, 0, 0, -1, -1, 1}, {1, 1, 1, 0, -1, 0}};

int dy[2][6] = {{0, -1, 1, -1, 1, 0}, {-1, 0, 1, 1, 0, -1}};
void dfs(int y, int x) {
  int nx, ny;
  if (t[y][x] == -1)
    return;
  t[y][x] = -1;
  for (int i = 0; i < 6; i++) {
    nx = x + dx[y % 2][i];
    ny = y + dy[y % 2][i];
    if (nx < 0 || W + 1 < nx || ny < 0 || ny > H + 1)
      continue;
    if (t[ny][nx] == 1) {
      cnt++;
    }
    if (t[ny][nx] == 0) {
      dfs(ny, nx);
    }
  }
}
int main() {
  while (1) {
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;
    for (int i = 0; i < 102; i++) {
      for (int j = 0; j < 102; j++) {
        t[i][j] = 0;
      }
    }
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        cin >> t[i][j];
      }
    }
    cnt = 0;
    dfs(0, 0);
    cout << cnt << endl;
  }
  return 0;
}
Problem p00492 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int H, W, cnt, t[102][102];
bool vis[102][102];

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

void dfs(int y, int x) {
  int nx, ny;
  vis[y][x] = true;
  // t[y][x]=-1;
  for (int i = 0; i < 6; i++) {
    nx = x + dx[y % 2][i];
    ny = y + dy[y % 2][i];
    if (nx < 0 || W + 1 < nx || ny < 0 || ny > H + 1)
      continue;
    if (vis[ny][nx])
      continue;
    if (t[ny][nx] == 1) {
      cnt++;
    }
    if (t[ny][nx] == 0) {
      dfs(ny, nx);
    }
  }
}
int main() {
  cin >> W >> H;
  for (int i = 0; i < 102; i++) {
    for (int j = 0; j < 102; j++) {
      t[i][j] = 0;
      vis[i][j] = false;
    }
  }
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> t[i][j];
    }
  }

  cnt = 0;
  dfs(0, 0);
  cout << cnt << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int H, W, cnt, t[102][102], vis[102][102];
int dx[2][6] = {{-1, 0, 0, -1, -1, 1}, {1, 1, 1, 0, -1, 0}};

int dy[2][6] = {{0, -1, 1, -1, 1, 0}, {-1, 0, 1, 1, 0, -1}};

void dfs(int y, int x) {
  int nx, ny;
  vis[y][x] = true;
  // t[y][x]=-1;
  for (int i = 0; i < 6; i++) {
    nx = x + dx[y % 2][i];
    ny = y + dy[y % 2][i];
    if (nx < 0 || W + 1 < x || ny < 0 || ny > H + 1)
      continue;
    if (vis[ny][nx])
      continue;
    if (t[ny][nx] == 1) {
      cnt++;
    }
  }
  for (int i = 0; i < 6; i++) {
    nx = x + dx[y % 2][i];
    ny = y + dy[y % 2][i];
    if (nx < 0 || W + 1 < x || ny < 0 || ny > H + 1)
      continue;
    if (vis[ny][nx])
      continue;
    if (t[ny][nx] == 0) {
      dfs(ny, nx);
    }
  }
}
int main() {
  while (1) {
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;
    for (int i = 0; i < 102; i++) {
      for (int j = 0; j < 102; j++) {
        t[i][j] = 0;
        vis[i][j] = false;
      }
    }
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        cin >> t[i][j];
      }
    }
    cnt = 0;
    dfs(0, 0);
    cout << cnt << endl;
  }
  return 0;
}