Problem p00711 (C++) — buggy submission, verdict: Time Limit Exceeded

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

class DoubleArray {
  int X, Y;
  int *top;

public:
  DoubleArray(int *p, int x, int y);
  int *operator[](int i);
  int getx();
  int gety();
};

DoubleArray::DoubleArray(int *p, int x, int y) {
  top = p;
  X = x;
  Y = y;
}

int *DoubleArray::operator[](int i) { return top + Y * i; }
int DoubleArray::getx() { return X; }
int DoubleArray::gety() { return Y; }

int calc(DoubleArray &map) {
  int i, j, a = 0;
  int x = map.getx();
  int y = map.gety();
  queue<int> q;
  for (i = 0; i < x; i++) {
    for (j = 0; j < y; j++) {
      if (map[i][j] == 2) {
        q.push(i);
        q.push(j);
        a = 1;
        map[i][j] = 0;
      }
    }
  }
  while (!q.empty()) {
    i = q.front();
    q.pop();
    j = q.front();
    q.pop();
    if (i > 0)
      if (map[i - 1][j] == 1) {
        q.push(i - 1);
        q.push(j);
        a++;
        map[i - 1][j] = 0;
      }
    if (j > 0)
      if (map[i][j - 1] == 1) {
        q.push(i);
        q.push(j - 1);
        a++;
        map[i][j - 1] = 0;
      }
    if (i < x - 1)
      if (map[i + 1][j] == 1) {
        q.push(i + 1);
        q.push(j);
        a++;
        map[i + 1][j] = 0;
      }
    if (j < y - 1)
      if (map[i][j + 1] == 1) {
        q.push(i);
        q.push(j + 1);
        a++;
        map[i][j + 1] = 0;
      }
  }
  return a;
}

int main() {
  int i, j, x, y, a[10000], b = 0;
  string s;
  char c;
  for (b = 0; b < 10000; b++) {
    cin >> x;
    cin >> y;
    if (x == 0 && y == 0)
      break;
    if (x > 20)
      x = 20;
    if (y > 20)
      y = 20;
    if (x < 1 || y < 1)
      continue;
    int m[x][y];
    DoubleArray map(&m[0][0], x, y);
    for (j = 0; j < y; j++) {
      cin >> s;
      for (i = 0; i < x; i++) {
        c = s[i];
        if (c == '#')
          map[i][j] = 0;
        if (c == '.')
          map[i][j] = 1;
        if (c == '@')
          map[i][j] = 2;
      }
    }
    a[b] = calc(map);
  }
  for (i = 0; i < b; i++) {
    cout << a[i] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

class DoubleArray {
  int X, Y;
  int *top;

public:
  DoubleArray(int *p, int x, int y);
  int *operator[](int i);
  int getx();
  int gety();
};

DoubleArray::DoubleArray(int *p, int x, int y) {
  top = p;
  X = x;
  Y = y;
}

int *DoubleArray::operator[](int i) { return top + Y * i; }
int DoubleArray::getx() { return X; }
int DoubleArray::gety() { return Y; }

int calc(DoubleArray &map) {
  int i, j, a = 0;
  int x = map.getx();
  int y = map.gety();
  queue<int> q;
  for (i = 0; i < x; i++) {
    for (j = 0; j < y; j++) {
      if (map[i][j] == 2) {
        q.push(i);
        q.push(j);
        a = 1;
        map[i][j] = 0;
      }
    }
  }
  while (!q.empty()) {
    i = q.front();
    q.pop();
    j = q.front();
    q.pop();
    if (i > 0)
      if (map[i - 1][j] == 1) {
        q.push(i - 1);
        q.push(j);
        a++;
        map[i - 1][j] = 0;
      }
    if (j > 0)
      if (map[i][j - 1] == 1) {
        q.push(i);
        q.push(j - 1);
        a++;
        map[i][j - 1] = 0;
      }
    if (i < x - 1)
      if (map[i + 1][j] == 1) {
        q.push(i + 1);
        q.push(j);
        a++;
        map[i + 1][j] = 0;
      }
    if (j < y - 1)
      if (map[i][j + 1] == 1) {
        q.push(i);
        q.push(j + 1);
        a++;
        map[i][j + 1] = 0;
      }
  }
  return a;
}

int main() {
  int i, j, x, y, a[10000], b = 0;
  string c;
  while (114514) {
    cin >> x;
    cin >> y;
    if (x == 0 && y == 0)
      break;
    if (x > 20)
      x = 20;
    if (y > 20)
      y = 20;
    if (x < 1 || y < 1)
      continue;
    int m[x][y];
    DoubleArray map(&m[0][0], x, y);
    for (j = 0; j < y; j++) {
      for (i = 0; i < x; i++) {
        cin >> c;
        if (c == "#")
          map[i][j] = 0;
        if (c == ".")
          map[i][j] = 1;
        if (c == "@")
          map[i][j] = 2;
      }
    }
    a[b++] = calc(map);
  }
  for (i = 0; i < b; i++) {
    cout << a[i] << endl;
  }
  return 0;
}