BOJ 24727 - 인지융~

image.png

$N=4,~C=10,~E=3$ 을 고려해보면, 무조건 계단형식대로 차례로 쌓는것이 가장 최적임을 알 수 있다.

void fail() {
   cout << -1;
   exit(0);
}
const int dy[] = {-1, 0, 1, 0}, dx[] = {0, 1, 0, -1}, op[] = {2, 3, 0, 1};
void solve() {
   int n, c, e;
   cin >> n >> c >> e;
   if (c + e > n * n) fail();
   vs ans(n, string(n, '0'));

   for (int l = 0; l < 2 * n; l++) {
      for (int y = l >= n ? l - n + 1 : 0; y < n; y++) {
         int x = l - y;
         if (x < 0) break;
         if (c) {
            ans[y][x] = '1';
            c--;
         }
      }
   }
   for (int l = 0; l < 2 * n; l++) {
      for (int y = l >= n ? l - n + 1 : 0; y < n; y++) {
         int x = l - y;
         if (x < 0) break;
         int yy = n - 1 - y, xx = n - 1 - x;
         if (e) {
            ans[yy][xx] = '2';
            e--;
         }
      }
   }

   for (int y = 0; y < n; y++)
      for (int x = 0; x < n; x++) {
         if (ans[y][x] == '1') {
            for (int d = 0; d < 4; d++) {
               int ny = y + dy[d], nx = x + dx[d];
               if (ny >= 0 && ny < n && nx >= 0 && nx < n && ans[ny][nx] == '2') {
                  fail();
               }
            }
         }
      }
   cout << 1 << endl;
   for (int i = 0; i < n; i++) cout << ans[i] << endl;
}

Tags:

Categories:

Updated:

Comments