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

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

const int MAX_SIZE = 20;
typedef pair<int, int> P;

int calc(char f[][MAX_SIZE + 1], queue<P> q, int W, int H) {
  set<P> s;
  while (q.size()) {
    P p = q.front();
    q.pop();
    s.insert(p);
    int di[] = {-1, 1, 0, 0};
    int dj[] = {0, 0, -1, 1};
    for (int k = 0; k < 4; ++k) {
      int i = p.first + di[k];
      int j = p.second + dj[k];
      if (i >= 0 && j >= 0 && i < H && j < W && f[i][j] == '.') {
        pair<set<P>::iterator, bool> r = s.insert(P(i, j));
        if (r.second)
          q.push(*r.first);
      }
    }
  }
  return s.size();
}
int main() {
  int W, H;
  char f[MAX_SIZE + 1][MAX_SIZE + 1];
  while (true) {
    queue<P> q;
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;
    for (int i = 0; i < H; ++i) {
      cin >> f[i];
      for (int j = 0; j < W; ++j) {
        if (f[i][j] == '@') {
          q.push(P(i, j));
        }
      }
    }
    cout << calc(f, q, W, H) << endl;
  }
}

Error: 0

Fixed submission:

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

const int MAX_SIZE = 20;
typedef pair<int, int> P;

int calc(char f[][MAX_SIZE], queue<P> q, int W, int H) {
  set<P> s;
  while (q.size()) {
    P p = q.front();
    q.pop();
    s.insert(p);
    int di[] = {-1, 1, 0, 0};
    int dj[] = {0, 0, -1, 1};
    for (int k = 0; k < 4; ++k) {
      int i = p.first + di[k];
      int j = p.second + dj[k];
      if (i >= 0 && j >= 0 && i < H && j < W && f[i][j] == '.') {
        pair<set<P>::iterator, bool> r = s.insert(P(i, j));
        if (r.second)
          q.push(*r.first);
      }
    }
  }
  return s.size();
}
int main() {
  int W, H;
  char f[MAX_SIZE][MAX_SIZE];
  while (true) {
    queue<P> q;
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;
    for (int i = 0; i < H; ++i) {
      cin >> f[i];
      for (int j = 0; j < W; ++j) {
        if (f[i][j] == '@') {
          q.push(P(i, j));
        }
      }
    }
    cout << calc(f, q, W, H) << endl;
  }
}
Problem p00711 (C++) — buggy submission, verdict: Runtime Error

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

const int MAX_SIZE = 21;
typedef pair<int, int> P;

int calc(char f[][MAX_SIZE], queue<P> q, int W, int H) {
  int n = 0;
  while (q.size()) {
    n++;
    P p = q.front();
    q.pop();
    int di[] = {-1, 1, 0, 0};
    int dj[] = {0, 0, -1, 1};
    for (int k = 0; k < 4; ++k) {
      int i = p.first + di[k];
      int j = p.second + dj[k];
      if (i >= 0 && j >= 0 && i < H && j < W && f[i][j] == '.') {
        f[i][j] = '#';
        q.push(P(i, j));
      }
    }
  }
  return n;
}
int main() {
  int W, H;
  char f[MAX_SIZE][MAX_SIZE];
  while (true) {
    queue<P> q;
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;
    for (int i = 0; i < H; ++i) {
      cin >> f[i];
      for (int j = 0; j < W; ++j) {
        if (f[i][j] == '@') {
          q.push(P(i, j));
        }
      }
    }
    cout << calc(f, q, W, H) << endl;
  }
}

Error: 0

Fixed submission:

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

const int MAX_SIZE = 20;
typedef pair<int, int> P;

int calc(char f[][MAX_SIZE], queue<P> q, int W, int H) {
  int n = 0;
  while (q.size()) {
    n++;
    P p = q.front();
    q.pop();
    int di[] = {-1, 1, 0, 0};
    int dj[] = {0, 0, -1, 1};
    for (int k = 0; k < 4; ++k) {
      int i = p.first + di[k];
      int j = p.second + dj[k];
      if (i >= 0 && j >= 0 && i < H && j < W && f[i][j] == '.') {
        f[i][j] = '#';
        q.push(P(i, j));
      }
    }
  }
  return n;
}
int main() {
  int W, H;
  char f[MAX_SIZE][MAX_SIZE];
  while (true) {
    queue<P> q;
    cin >> W >> H;
    if (W == 0 && H == 0)
      break;
    for (int i = 0; i < H; ++i) {
      cin >> f[i];
      for (int j = 0; j < W; ++j) {
        if (f[i][j] == '@') {
          q.push(P(i, j));
        }
      }
    }
    cout << calc(f, q, W, H) << endl;
  }
}
Problem p00711 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define vec vector

int dp[22][22];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ans;
vec<string> a;

void solve(int p, int q) {
  if (dp[p + 1][q + 1])
    return;
  dp[p + 1][q + 1] = 1;
  if (a[p][q] != '#') {
    ans++;
  } else
    return;
  rep(i, 4) solve(p + dx[i], q + dy[i]);
}

int main(void) {
  while (1) {
    int w, h;
    cin >> w >> h;
    if (w == 0)
      break;
    rep(i, h + 2) rep(j, w + 2) dp[i][j] = 0;
    rep(i, w + 2) {
      dp[0][i] = -1;
      dp[h + 1][i] = -1;
    }
    rep(i, h + 2) {
      dp[i][0] = -1;
      dp[i][w + 1] = -1;
    }
    a.clear();
    a.resize(h);
    pair<int, int> start;
    rep(i, h) {
      cin >> a[i];
      rep(j, w) if (a[i][j] == '@') start = make_pair(i, j);
    }
    // dp[start.first + 1][start.second + 1] = 1;
    ans = 0;
    solve(start.first, start.second);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:


#include <stdio.h>
int x, y, p, q, i, w, h, r = 1;
int j, k[101][101];
char s[101][101];
int a[] = {1, -1, 0, 0}, b[] = {0, 0, 1, -1};
void func(int x, int y) {
  for (i = 0; i < 4; i++) {
    if (x + a[i] <= h && x + a[i] > 0 && y + b[i] <= w && y + b[i] > 0) {
      if (k[x + a[i]][y + b[i]] == 1)
        ;
      else if (s[x + a[i]][y + b[i]] == '.') {
        r++;
        k[x + a[i]][y + b[i]] = 1;
        func(x + a[i], y + b[i]);
      }
    }
  }
}

int main() {
  while (1) {
    scanf("%d %d", &w, &h);
    if (w == 0 && h == 0)
      break;
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        scanf("%c", &s[i][j]);
        if (s[i][j] == '@')
          k[i][j] = 1;
        else
          k[i][j] = 0;
        if (s[i][j] == '@') {
          p = i;
          q = j;
        }
      }
    }
    func(p, q);
    printf("%d\\n", r);
  }
  return 0;
}
Problem p00711 (C++) — buggy submission, verdict: Time Limit Exceeded

//??¨??¢?´¢:??±???????????¢?´¢??????????????¢?´¢

//??????????????¢?????¬?????????

//?????°??¢??°???????????????(?????????)????????\\???(?????????)
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
int W, H;
char c[101][101];
bool visited[100][100];

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

void compute(); //????????????????????£?¨?

int main() {

  while (scanf("%d %d", &W, &H), W | H) {
    int i;

    for (i = 0; i < H; i++) {
      scanf(" %s", &c[i][0]);
    }
    compute();
  }
  return 0;
}

void compute() {

  memset(
      visited, false,
      sizeof visited); //{????????????????????????1bit??????????????????1bit????????§?????????}

  queue<int> que;
  int i, j;
  for (j = 0; j < W; j++) {
    for (i = 0; i < H; i++) {
      if (c[i][j] == '@') {
        que.push(j + i * W);
        visited[i][j] = true;
        break;
      }
    }
  }
  int answer = 1;
  while (!que.empty()) {
    int cur = que.front();
    que.pop();
    int x = cur % W;
    int y = cur / W;

    for (i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];

      if (!(0 <= nx && nx < W && 0 <= ny && ny < H))
        continue;
      if (c[ny][nx] == '#' || visited[ny][nx])
        continue;

      que.push(nx + ny * W);
      visited[ny][nx] = true;
      ++answer;
    }
  }
  // cout << answer << endl;
  printf("%d\\n", answer);
}

Error: TLE

Fixed submission:

//??¨??¢?´¢:??±???????????¢?´¢??????????????¢?´¢

//??????????????¢?????¬?????????

//?????°??¢??°???????????????(?????????)????????\\???(?????????)
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
int W, H;
char c[101][101];
bool visited[100][100];

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

void compute(); //????????????????????£?¨?

int main() {

  while (scanf("%d %d", &W, &H), W | H) {
    int i;

    for (i = 0; i < H; i++) {
      scanf("%d", &c[i][0]);
    }
  }
  return 0;
}

void compute() {

  memset(
      visited, false,
      sizeof visited); //{????????????????????????1bit??????????????????1bit????????§?????????}

  queue<int> que;
  int i, j;
  for (j = 0; j < W; j++) {
    for (i = 0; i < H; i++) {
      if (c[i][j] == '@') {
        que.push(j + i * H);
        visited[i][j] == true;
        break;
      }
    }
  }
  int answer = 1;
  while (!que.empty()) {
    int cur = que.front();
    que.pop();
    int x = cur % W;
    int y = cur / W;

    for (i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[j];

      if (!(0 <= nx && nx < W && 0 <= ny))
        continue;
      if (c[ny][nx] == '#' || visited[ny][nx])
        continue;

      que.push(nx + ny * W);
      visited[ny][nx] = true;
    }
  }
}
Problem p00711 (C++) — buggy submission, verdict: Runtime Error

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

int w, h;
string room[20];

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

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

int dfs(int x, int y) {
  int cnt = 1;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];

    if (isRegion(nx, ny) && room[ny][nx] == '.') {
      room[ny][nx] = '#';
      cnt += dfs(nx, ny);
    }
  }
  return cnt;
}

int main(int argc, const char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

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

    int sx, sy;
    for (int i = 0; i < h; i++) {
      cin >> room[i];
      if (room[i].find('@') != -1) {
        sx = room[i].find('@');
        sy = i;
      }
    }

    cout << dfs(sx, sy) << endl;
  }
}

Error: 0

Fixed submission:

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

string room[20];

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int dfs(int x, int y) {
  int cnt = 1;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];

    if (room[ny][nx] == '.') {
      room[ny][nx] = '#';
      cnt += dfs(nx, ny);
    }
  }
  return cnt;
}

int main(int argc, const char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (1) {
    int w, h;
    cin >> w >> h;
    if (!w && !h)
      break;

    int sx, sy;
    for (int i = 0; i < h; i++) {
      cin >> room[i];
      if (room[i].find('@') != -1) {
        sx = room[i].find('@');
        sy = i;
      }
    }

    cout << dfs(sx, sy) << endl;
  }
}
Problem p00711 (C++) — buggy submission, verdict: Runtime Error

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

int W, H;
char visit[21][21];
int dfs(int h, int w) {
  int sum = 1;
  if (0 > w || w == W || 0 > h || h == H)
    return 0;
  if (visit[h][w] == '#')
    return 0;
  if (visit[h][w] == '!')
    return 0;
  visit[h][w] = '!';
  sum += dfs(h, w + 1);
  sum += dfs(h, w - 1);
  sum += dfs(h + 1, w);
  sum += dfs(h - 1, w);

  return sum;
}

int main(void) {
  int w1, h1;
  string str;
  while (cin >> W >> H) {
    if (W == 0 && H == 0)
      break;
    for (int i = 0; i < H; i++) {
      cin >> str;
      for (int j = 0; j < W; j++) {
        visit[i][j] = str[j];
        if (visit[i][j] == '@') {
          visit[i][j] = '.';
          w1 = j;
          h1 = i;
        }
      }
    }
    cout << dfs(h1, w1) << endl;
  }

  return 0;
}


Error: -11

Fixed submission:

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

int W, H;
char visit[21][21];
int sum;
int dfs(int w, int h) {
  int sum = 0;
  if (0 <= w && w < W && 0 <= h && h < H) {
    if (visit[w][h] == '.') {
      visit[w][h] = '!';
      return 1;
    }
    sum += dfs(w + 1, h);
    sum += dfs(w - 1, h);
    sum += dfs(w, h + 1);
    sum += dfs(w, h - 1);
    return sum;
  }
  return sum;
}

int main(void) {
  string str;
  while (cin >> W >> H) {
    if (W == 0 && H == 0)
      break;
    for (int i = 0; i < H; i++) {
      cin >> str;
      for (int j = 0; j < W; j++) {
        visit[i][j] = str[j];
        if (visit[i][j] == '@')
          dfs(i, j);
      }
    }
    cout << sum << endl;
  }

  return 0;
}
Problem p00711 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

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

int paint(vector<string> &field, int x, int y) {
  int cnt = 0;

  for (int i = 0; i < 4; ++i) {
    int xx = x + dx[i];
    int yy = y + dy[i];

    if (xx < 0 || field[0].size() <= xx || yy < 0 || field.size() <= yy)
      continue;

    if (field[yy][xx] == '.') {
      field[yy][xx] = '#';
      cnt += paint(field, xx, yy) + 1;
    }
  }
  return cnt;
}

int main() {
  while (true) {
    int W, H;
    cin >> W >> H;

    if (!W && !H)
      break;

    int sx, sy;
    vector<string> field(H);
    for (int i = 0; i < H; ++i) {
      cin >> field[i];
      for (int x = 0; x < W; ++x) {
        if (field[i][x] == '@') {
          sx = x;
          sy = i;
        }
      }
    }

    cout << paint(field, sx, sy) + 1 << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

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

int paint(vector<string> &field, int x, int y) {
  int cnt = 0;

  for (int i = 0; i < 4; ++i) {
    int xx = x + dx[i];
    int yy = y + dy[i];

    if (xx < 0 || field[i].size() <= xx || yy < 0 || field.size() <= yy)
      continue;

    if (field[yy][xx] == '.') {
      field[yy][xx] = '#';
      cnt += paint(field, xx, yy) + 1;
    }
  }
  return cnt;
}

int main() {
  while (true) {
    int W, H;
    cin >> W >> H;

    if (!W && !H)
      break;

    int sx, sy;
    vector<string> field(H);
    for (int i = 0; i < H; ++i) {
      cin >> field[i];
      for (int x = 0; x < W; ++x) {
        if (field[i][x] == '@') {
          sx = x;
          sy = i;
        }
      }
    }

    cout << paint(field, sx, sy) + 1 << endl;
  }
  return 0;
}
Problem p00711 (C++) — buggy submission, verdict: Runtime Error

//
//  RedAndBlack.cpp
//
//
//  Created by ??°????????? on 2015/10/30.
//
//

#include <iostream>
using namespace std;

int w, h, num = 0, room[20][20];

void dfs(int x, int y) {
  if (room[y][x] == 0)
    return;

  num++;
  room[y][x] = 0;

  // ??????????????¢?´¢
  if (y - 1 > -1)
    dfs(x, y - 1);
  if (y + 1 < h)
    dfs(x, y + 1);
  if (x - 1 > -1)
    dfs(x - 1, y);
  if (x + 1 < w)
    dfs(x + 1, y);

  return;
}

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

    int manX, manY;

    for (int i = 0; i < h; i++) {
      string s;
      cin >> s;
      for (int j = 0; j < w; j++) {
        if (s[j] == '.') {
          room[i][j] = 1;
        } else if (s[j] == '#') {
          room[i][j] = 0;
        } else if (s[j] == '@') {
          room[i][j] = 1;
          manX = j;
          manY = i;
        }
      }
    }

    dfs(manX, manY);

    cout << num << endl;

    num = 0;
  }

  return 0;
}

Error: -11

Fixed submission:

//
//  RedAndBlack.cpp
//
//
//  Created by ??°????????? on 2015/10/30.
//
//

#include <iostream>
using namespace std;

int w, h, num = 0, room[20][20];

void dfs(int x, int y) {
  num++;

  room[y][x] = 0;

  // ??????????????¢?´¢
  if (y - 1 > -1 && room[y - 1][x] == 0)
    dfs(x, y - 1);
  if (y + 1 < h && room[y + 1][x] == 0)
    dfs(x, y + 1);
  if (x - 1 > -1 && room[y][x - 1] == 0)
    dfs(x - 1, y);
  if (x + 1 < w && room[y][x + 1] == 0)
    dfs(x + 1, y);

  return;
}

int main() {
  while (cin >> w >> h, w) {
    if (w == 0 && h == 0)
      break;

    int manX, manY;

    for (int i = 0; i < h; i++) {
      string s;
      cin >> s;
      for (int j = 0; j < w; j++) {
        if (s[j] == '.') {
          room[i][j] = 1;
        } else if (s[j] == '#') {
          room[i][j] = 0;
        } else if (s[j] == '@') {
          room[i][j] = 1;
          manX = j;
          manY = i;
        }
      }
    }

    dfs(manX, manY);

    cout << num << endl;

    num = 0;
  }

  return 0;
}
Problem p00711 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"

using namespace std;

#define rep(a, b) for (int(a) = 0; (a) < (b); (a)++)

int main() {
  int w, h;
  while (cin >> w >> h) {
    if (w == 0 && h == 0)
      break;
    int sx, sy;
    int sum = 0;
    int dy[] = {-1, 0, 1, 0};
    int dx[] = {0, -1, 0, 1};

    string data[30] = {};

    for (int i = 0; i < h; i++) {
      cin >> data[i];
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (data[i][j] == '@') {
          sx = j;
          sy = i;
          break;
        }
      }
    }

    bool visited[30][30] = {};

    queue<pair<int, int>> que;

    que.push(pair<int, int>(sy, sx));
    visited[sy][sx] = true;

    while (que.size()) {
      int qff = que.front().first, qfs = que.front().second;
      sum++;
      rep(i, 4) {
        if (qff + dy[i] < h && qff + dy[i] >= 0 && qfs + dx[i] < w &&
            qfs + dx[i] >= 0 && visited[qff + dy[i]][qfs + dx[i]] == false &&
            data[qff + dy[i]][qfs + dx[i]] != '#') {
          que.push(pair<int, int>(qff + dy[i], qfs + dx[i]));
          visited[qff + dy[i]][qfs + dx[i]] = true;
        }
      }

      que.pop();
    }
    cout << sum << endl;

    //        rep(i,h){rep(j,w)cout<<(visited[i][j]?'*':'_');cout<<endl;}
  }

  return 0;
}

Error: WA

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int w, h;
  while (cin >> w >> h) {
    if (w == 0 && h == 0)
      break;
    int sx, sy;
    int sum = 0;
    int dy[] = {-1, 0, 1, 0};
    int dx[] = {0, -1, 0, 1};

    string data[30] = {};

    for (int i = 0; i < h; i++) {
      cin >> data[i];
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (data[i][j] == '@') {
          sx = j;
          sy = i;
          break;
        }
      }
    }

    bool visited[30][30] = {};
    rep(i, 30) rep(j, 30) visited[i][j] = false;

    queue<pair<int, int>> que;

    que.push(pair<int, int>(sy, sx));
    visited[sy][sx] = true;

    while (que.size()) {
      int qff = que.front().first, qfs = que.front().second;
      sum++;
      rep(i, 4) {
        if (qff + dy[i] < h && qff + dy[i] >= 0 && qfs + dx[i] < w &&
            qfs >= 0 && visited[qff + dy[i]][qfs + dx[i]] == false &&
            data[qff + dy[i]][qfs + dx[i]] != '#') {
          que.push(pair<int, int>(qff + dy[i], qfs + dx[i]));
          visited[qff + dy[i]][qfs + dx[i]] = true;
        }
      }

      que.pop();
    }
    cout << sum << endl;
  }

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

#include <iostream>
using namespace std;
typedef pair<int, int> pii;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
char fld[20][21];
int used[20][20];
int W, H;

void rec(int i, int j) {
  used[i][j] = 1;
  for (int k = 0; k < 4; k++) {
    int ni = i + dy[k];
    int nj = j + dx[k];
    if (0 <= ni && ni < H && 0 <= nj && nj < W && fld[ni][nj] != '#' &&
        used[ni][nj] == 0) {
      rec(ni, nj);
    }
  }
}

int main() {

  while (1) {
    cin >> W >> H;
    if (!(H || W))
      break;
    int sx, sy;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> fld[i][j];
        if (fld[i][j] == '@') {
          sy = i;
          sx = j;
        }
      }
    }

    for (int i = 0; i < 20; i++)
      for (int j = 0; j < 20; j++)
        used[i][j] = 0;
    used[sy][sx] = 1;
    rec(sy, sx);
    int ans = 0;
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++) {
        ans += used[i][j];
      }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
typedef pair<int, int> pii;
pii dxdy[] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
char fld[20][20];
bool used[20][20];
int W, H;

void rec(int i, int j) {
  used[i][j] = 1;
  for (pii dd : dxdy) {
    int ni = i + dd.first;
    int nj = j + dd.second;
    if ((0 <= ni && ni < H && 0 <= nj && nj < W) && fld[ni][nj] == '#' &&
        used[ni][nj] == 0) {
      rec(ni, nj);
    }
  }
}

int main() {

  while (1) {
    cin >> W >> H;
    if (!(H || W))
      break;
    int sx, sy;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> fld[i][j];
        if (fld[i][j] == '@') {
          sy = i;
          sx = j;
        }
      }
    }

    for (int i = 0; i < 20; i++)
      for (int j = 0; j < 20; j++)
        used[i][j] = 0;
    used[sy][sx] = 1;
    rec(sy, sx);
    int ans = 0;
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++) {
        ans += used[i][j];
      }
    cout << ans << endl;
  }

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

#include <iostream>
using namespace std;
typedef pair<int, int> pii;
pii dxdy[] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
char fld[21][21];
bool used[20][20];
int W, H;

int rec(int i, int j) {

  if (fld[i][j] == '#')
    return 0;
  if (used[i][j])
    return 0;
  used[i][j] = true;
  int ret = 0;
  for (pii dd : dxdy) {
    int ni = i + dd.first;
    int nj = j + dd.second;
    if (!(0 <= ni && ni < H && 0 <= nj && nj < W))
      continue;
    ret += rec(ni, nj);
  }

  return ret + 1;
}

int main() {

  while (1) {
    cin >> W >> H;
    if (!(H || W))
      break;
    int sx, sy;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> fld[i][j];
        if (fld[i][j] == '@') {
          sy = i;
          sx = j;
          fld[i][j] = '.';
        }
      }
    }

    for (int i = 0; i < 20; i++)
      for (int j = 0; j < 20; j++)
        used[i][j] = false;
    cout << rec(sy, sx) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
typedef pair<int, int> pii;
pii dxdy[] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
char fld[21][21];
int used[20][20];
int W, H;

int rec(int i, int j) {

  if (fld[i][j] == '#')
    return 0;
  if (used[i][j])
    return 0;
  used[i][j] = 1;
  int ret = 0;
  for (pii dd : dxdy) {
    int ni = i + dd.first;
    int nj = j + dd.second;
    if (!(0 <= ni && ni < H && 0 <= nj && nj < W))
      continue;
    ret += rec(ni, nj);
  }

  return ret + 1;
}

int main() {

  while (1) {
    cin >> W >> H;
    if (!(H || W))
      break;
    int sx, sy;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> fld[i][j];
        if (fld[i][j] == '@') {
          sy = i;
          sx = j;
          fld[i][j] = '.';
        }
      }
    }

    for (int i = 0; i < 20; i++)
      for (int j = 0; j < 20; j++)
        used[i][j] = 0;
    cout << rec(sy, sx) << endl;
  }

  return 0;
}