Problem p02855 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define repn(i, x) for (int i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())

const int MAX = 510000;
const int MOD = 1000000007;

vector<string> field;

int main() {
  // 入力
  int H, W, K;
  cin >> H >> W >> K;
  field.resize(H);
  for (int h = 0; h < H; ++h)
    cin >> field[h];

  vector<int> cnt(H);
  int ans[H][K];
  rep(i, H) {
    rep(j, W) { ans[i][j] = 0; }
  }

  rep(i, H) {
    rep(j, W) {
      if (field[i][j] == '#')
        cnt[i]++;
    }
  }

  // rep(i,H) cout<<cnt[i]<<endl;

  // countが0ではなければそれぞれのれつで分割
  ll tmp = 1;

  rep(i, H) {
    bool ok = false;
    if (cnt[i] != 0) {
      rep(j, W) {
        if (ok and field[i][j] == '#')
          tmp++;
        ans[i][j] = tmp;
        if (ok == false and field[i][j] == '#')
          ok = true;
      }
      tmp++;
    }
  }

  // 0を埋めていく作業
  rep(i, W) {
    int tmp2 = 0;
    rep(j, H) {
      if (ans[j][i] == 0)
        ans[j][i] = tmp2;
      if (ans[j][i] != 0)
        tmp2 = ans[j][i];
    }
  }

  rep(i, W) {
    int tmp3 = ans[H - 1][i];
    for (int j = H - 1; j >= 0; j--) {
      if (ans[j][i] == 0)
        ans[j][i] = tmp3;
      if (ans[j][i] != 0)
        tmp3 = ans[j][i];
    }
  }

  // 出力
  rep(i, H) {
    rep(j, W) { cout << ans[i][j] << " "; }
    cout << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define repn(i, x) for (int i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())

const int MAX = 510000;
const int MOD = 1000000007;

vector<string> field;

int main() {
  // 入力
  int H, W, K;
  cin >> H >> W >> K;
  field.resize(H);
  for (int h = 0; h < H; ++h)
    cin >> field[h];

  vector<int> cnt(H);
  int ans[H][W];
  rep(i, H) {
    rep(j, W) { ans[i][j] = 0; }
  }

  rep(i, H) {
    rep(j, W) {
      if (field[i][j] == '#')
        cnt[i]++;
    }
  }

  // rep(i,H) cout<<cnt[i]<<endl;

  // countが0ではなければそれぞれのれつで分割
  ll tmp = 1;

  rep(i, H) {
    bool ok = false;
    if (cnt[i] != 0) {
      rep(j, W) {
        if (ok and field[i][j] == '#')
          tmp++;
        ans[i][j] = tmp;
        if (ok == false and field[i][j] == '#')
          ok = true;
      }
      tmp++;
    }
  }

  // 0を埋めていく作業
  rep(i, W) {
    int tmp2 = 0;
    rep(j, H) {
      if (ans[j][i] == 0)
        ans[j][i] = tmp2;
      if (ans[j][i] != 0)
        tmp2 = ans[j][i];
    }
  }

  rep(i, W) {
    int tmp3 = ans[H - 1][i];
    for (int j = H - 1; j >= 0; j--) {
      if (ans[j][i] == 0)
        ans[j][i] = tmp3;
      if (ans[j][i] != 0)
        tmp3 = ans[j][i];
    }
  }

  // 出力
  rep(i, H) {
    rep(j, W) { cout << ans[i][j] << " "; }
    cout << endl;
  }
}