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

#include <iostream>
#include <queue>
#include <string>
#include <vector>

int main() {
  std::vector<int> tiles;
  std::queue<int> unvisited_tile_q;
  int width, height, counter, pos;
  std::string line;

  while (true) {
    std::cin >> width >> height;
    if (width == 0 && height == 0)
      break;
    tiles.resize(width * height);
    pos = 0;
    for (int h = 0; h < height; h++) {
      std::cin >> line;
      for (std::string::iterator it = line.begin(); it != line.end(); ++it) {
        switch (*it) {
        case '#':
          tiles[pos] = 0;
          break;
        case '@':
          unvisited_tile_q.push(pos);
        case '.':
          tiles[pos] = 1;
        }
        pos++;
      }
    }

    counter = 0;
    while (!unvisited_tile_q.empty()) {
      pos = unvisited_tile_q.front();
      unvisited_tile_q.pop();
      if (tiles[pos] == 1) {
        counter++;
        tiles[pos] = -1;
      } else
        continue;
      // left
      if (pos - 1 >= 0 && pos % width > 0 && tiles[pos - 1] == 1)
        unvisited_tile_q.push(pos - 1);
      // right
      if (pos + 1 <= tiles.size() && pos % width < width - 1 &&
          tiles[pos + 1] == 1)
        unvisited_tile_q.push(pos + 1);
      // up
      if (pos - width >= 0 && tiles[pos - width] == 1)
        unvisited_tile_q.push(pos - width);
      // down
      if (pos + width < tiles.size() && tiles[pos + width] == 1)
        unvisited_tile_q.push(pos + width);
    }
    // output result
    std::cout << counter << std::endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  std::vector<int> row;
  int prev_col, width, height, max = 0, last = 0;
  bool connect_prev_row;

  std::cin >> width >> height;
  row.resize(width + 1);
  for (int i = 0; i <= width; i++)
    row[i] = 0;

  std::string line;
  while (true) {
    std::cin >> line;
    if (line == "00") {
      std::cout << max;
      break;
    }
    connect_prev_row = false;
    for (std::string::size_type i = 0; i < line.size(); i++) {
      if (line[i] == '.') {
        if (connect_prev_row) {
          if (row[i] > 0)
            row[i + 1] = row[i] + 1;
          else
            row[i + 1] = row[i] - 1;
        } else {
          connect_prev_row = true;
          if (row[i] <= 0 && row[i + 1] <= 0)
            row[i + 1] = row[i + 1] + row[i] - 1;
          else
            row[i + 1] = std::abs(row[i + 1]) + std::abs(row[i]) + 1;
        }
      } else if (line[i] == '@') {
        if (connect_prev_row)
          row[i + 1] = -row[i] + 1;
        else {
          connect_prev_row = true;
          row[i + 1] = -row[i + 1] - row[i] + 1;
        }
      } else {
        row[i + 1] = 0;
        connect_prev_row = false;
      }
      max = max > row[i + 1] ? max : row[i + 1];
    }
    last = 0;
    for (int i = line.size(); i > 0; i--) {
      if (row[i] == 0)
        last = 0;
      else if (row[i] > 0) {
        if (last == 0)
          last = row[i];
        else
          row[i] = last;
      } else {
        if (last == 0)
          last = row[i];
        else
          row[i] = last;
      }
    }
  }
  return 0;
}