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

#include <algorithm>
#include <ciso646>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
typedef unsigned int ui;
int main() {
  int w, h;
  string s;
  int i, j;
  while (cin >> w >> h) {
    if (w == 0)
      break;
    char tile[22][22];
    int used[22][22];
    for (i = 0; i < 22; i++) {
      for (j = 0; j < 22; j++) {
        used[i][j] = 0;
        tile[i][j] = '#';
      }
    }
    vector<int> q1, q2;
    for (j = 1; j <= h; j++) {
      cin >> s;
      for (i = 1; i <= w; i++) {
        tile[i][j] = s[i - 1];
        if (s[i - 1] == '@') {
          q1.push_back(i);
          q2.push_back(j);
          used[i][j] = 1;
        }
      }
    }
    while (!q1.empty()) {
      int x = q1[0];
      int y = q2[0];
      q1.erase(q1.begin() + 0);
      q2.erase(q2.begin() + 0);
      if (used[x - 1][y] == 0 && tile[x - 1][y] == '.') {
        q1.push_back(x - 1);
        q2.push_back(y);
        used[x - 1][y] = 1;
      }
      if (used[x + 1][y] == 0 && tile[x + 1][y] == '.') {
        q1.push_back(x + 1);
        q2.push_back(y);
        used[x + 1][y] = 1;
      }
      if (used[x][y - 1] == 0 && tile[x][y - 1] == '.') {
        q1.push_back(x);
        q2.push_back(y - 1);
        used[x][y - 1] = 1;
      }
      if (used[x][y + 1] == 0 && tile[x][y + 1] == '.') {
        q1.push_back(x);
        q2.push_back(y + 1);
        used[x][y + 1] = 1;
      }
    }
    int count = 0;
    for (i = 0; i < 22; i++) {
      for (j = 0; j < 22; j++) {
        if (used[i][j] == 1)
          count++;
      }
    }
    cout << count << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <ciso646>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
typedef unsigned int ui;
int main() {
  int w, h;
  string s;
  int i, j;
  while (cin >> w >> h) {
    if (w == 0)
      break;
    char tile[22][22];
    int used[22][22];
    for (i = 0; i < 22; i++) {
      for (j = 0; j < 22; j++) {
        used[i][j] = 0;
        tile[i][j] = '#';
      }
    }
    vector<int> q1, q2;
    for (j = 1; j <= h; j++) {
      cin >> s;
      for (i = 1; i <= w; i++) {
        tile[i][j] = s[i - 1];
        if (s[i - 1] == '@') {
          q1.push_back(i);
          q2.push_back(j);
        }
      }
    }
    while (!q1.empty()) {
      int x = q1[0];
      int y = q2[0];
      used[x][y] = 1;
      q1.erase(q1.begin() + 0);
      q2.erase(q2.begin() + 0);
      if (used[x - 1][y] == 0 && tile[x - 1][y] == '.') {
        q1.push_back(x - 1);
        q2.push_back(y);
      }
      if (used[x + 1][y] == 0 && tile[x + 1][y] == '.') {
        q1.push_back(x + 1);
        q2.push_back(y);
      }
      if (used[x][y - 1] == 0 && tile[x][y - 1] == '.') {
        q1.push_back(x);
        q2.push_back(y - 1);
      }
      if (used[x][y + 1] == 0 && tile[x][y + 1] == '.') {
        q1.push_back(x);
        q2.push_back(y + 1);
      }
    }
    int count = 0;
    for (i = 0; i < 22; i++) {
      for (j = 0; j < 22; j++) {
        if (used[i][j] == 1)
          count++;
      }
    }
    cout << count << endl;
  }
  return 0;
}