Problem p03640 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define ll long long
#define bit __builtin_popcountll
#define sqr(x) (x) * (x)
#define forit(it, S)                                                           \
  for (__typeof((S).begin()) it = (S).begin(); it != (S).end(); it++)
#define debug(x) cout << #x << " = " << x << endl
#define forn(i, n) for (int i = 0; (i) < (n); ++i)
#define printvpair(v)                                                          \
  for (int i = 0; (i) < (v.size()); ++i)                                       \
    cout << v[i].first << " " << v[i].second << endl;
#define printv(v)                                                              \
  for (int i = 0; (i) < (v.size()); ++i)                                       \
    cout << v[i] << " ";                                                       \
  cout << endl;

using namespace std;

typedef pair<int, int> pii;

const double eps = 1e-9;
const double pi = acos(-1.0);
const int INF = 1000000000;

const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
const int N = 1e2 + 7;
int a[N];
bool used[N][N];
int ans[N][N];
int n, m, k;
void dfs(int x, int y, int color) {
  used[x][y] = 1;
  ans[x][y] = color;
  a[color]--;
  for (int i = 0; i < 4; i++) {
    int x1 = x + dx[i];
    int y1 = y + dy[i];

    if (0 <= x1 && x1 < n && 0 <= y1 && y1 < m && !used[x1][y1]) {
      if (a[color] > 0) {
        dfs(x1, y1, color);
      } else {
        color++;
        dfs(x1, y1, color);
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m >> k;
  for (int i = 1; i <= k; i++)
    cin >> a[i];
  dfs(0, 0, 1);

  // cout << endl;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++)
      cout << ans[i][j] << " ";
    cout << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define ll long long
#define bit __builtin_popcountll
#define sqr(x) (x) * (x)
#define forit(it, S)                                                           \
  for (__typeof((S).begin()) it = (S).begin(); it != (S).end(); it++)
#define debug(x) cout << #x << " = " << x << endl
#define forn(i, n) for (int i = 0; (i) < (n); ++i)
#define printvpair(v)                                                          \
  for (int i = 0; (i) < (v.size()); ++i)                                       \
    cout << v[i].first << " " << v[i].second << endl;
#define printv(v)                                                              \
  for (int i = 0; (i) < (v.size()); ++i)                                       \
    cout << v[i] << " ";                                                       \
  cout << endl;

using namespace std;

typedef pair<int, int> pii;

const double eps = 1e-9;
const double pi = acos(-1.0);
const int INF = 1000000000;

const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
const int N = 1e2 + 7;
int a[N * N];
bool used[N][N];
int ans[N][N];
int n, m, k;
void dfs(int x, int y, int color) {
  used[x][y] = 1;
  ans[x][y] = color;
  a[color]--;
  for (int i = 0; i < 4; i++) {
    int x1 = x + dx[i];
    int y1 = y + dy[i];

    if (0 <= x1 && x1 < n && 0 <= y1 && y1 < m && !used[x1][y1]) {
      if (a[color] > 0) {
        dfs(x1, y1, color);
      } else {
        color++;
        dfs(x1, y1, color);
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m >> k;
  for (int i = 1; i <= k; i++)
    cin >> a[i];
  dfs(0, 0, 1);

  // cout << endl;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++)
      cout << ans[i][j] << " ";
    cout << endl;
  }
  return 0;
}