Problem p02855 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  string tmp;
  vector<vector<int>> ans;
  for (int i = 0; i < H; i++) {
    ans.emplace_back(vector<int>(W, 0));
  }
  int S = 1;
  vector<bool> is_exist(W, false);
  for (int i = 0; i < H; i++) {
    cin >> tmp;
    for (int j = 0; j < W; j++) {
      if (tmp[j] == '#') {
        ans[i][j] = S;
        S++;
        is_exist[j] = true;
      }
    }
  }
  int prev = 0;
  for (int i = 0; i < W; i++) {
    prev = 0;
    for (int j = 0; j < H; j++) {
      if (ans[j][i] == 0) {
        ans[j][i] = prev;
      }
      prev = ans[j][i];
    }
    prev = 0;
    for (int j = H - 1; j >= 0; j--) {
      if (ans[j][i] == 0) {
        if (ans[j][i] != 0) {
          prev = ans[j][i];
          continue;
        }
        ans[j][i] = prev;
      }
      prev = ans[j][i];
    }
  }
  bool flag = true;
  while (flag) {
    flag = false;
    for (int i = 0; i < W; i++) {
      if (!is_exist[i]) {
        flag = true;
        prev = 0;
        for (int j = 0; j < H; j++) {
          if (ans[j][i] != 0)
            continue;
          if (i == 0 || i == W - 1) {
            ans[j][i] = (i == 0 ? ans[j][i + 1] : ans[j][i - 1]);
          } else {
            ans[j][i] = ans[j][i + 1];
          }
          if (ans[j][i] != 0)
            is_exist[i] = true;
        }
      }
    }
  }
  for (auto v : ans) {
    for (int e : v) {
      cout << e << " ";
    }
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  string tmp;
  vector<vector<int>> ans;
  for (int i = 0; i < H; i++) {
    ans.emplace_back(vector<int>(W, 0));
  }
  int S = 1;
  vector<bool> is_exist(W, false);
  for (int i = 0; i < H; i++) {
    cin >> tmp;
    for (int j = 0; j < W; j++) {
      if (tmp[j] == '#') {
        ans[i][j] = S;
        S++;
        is_exist[j] = true;
      }
    }
  }
  int prev = 0;
  for (int i = 0; i < W; i++) {
    prev = 0;
    for (int j = 0; j < H; j++) {
      if (ans[j][i] == 0) {
        ans[j][i] = prev;
      }
      prev = ans[j][i];
    }
    prev = 0;
    for (int j = H - 1; j >= 0; j--) {
      if (ans[j][i] == 0) {
        if (ans[j][i] != 0) {
          prev = ans[j][i];
          continue;
        }
        ans[j][i] = prev;
      }
      prev = ans[j][i];
    }
  }
  bool flag = true;
  while (flag) {
    flag = false;
    for (int i = 0; i < W; i++) {
      if (!is_exist[i]) {
        flag = true;
        prev = 0;
        for (int j = 0; j < H; j++) {
          if (ans[j][i] != 0)
            continue;
          if (i == 0 || i == W - 1) {
            ans[j][i] = (i == 0 ? ans[j][i + 1] : ans[j][i - 1]);
          } else {
            if (ans[j][i - 1] * ans[j][i + 1] == 0) {
              ans[j][i] = (ans[j][i - 1] == 0 ? ans[j][i + 1] : ans[j][i - 1]);
            } else {
              ans[j][i] = ans[j][i + 1];
            }
          }
          if (ans[j][i] != 0)
            is_exist[i] = true;
        }
      }
    }
  }
  for (auto v : ans) {
    for (int e : v) {
      cout << e << " ";
    }
    cout << endl;
  }
  return 0;
}