Skip to content

Commit cc55a99

Browse files
committed
백준_문제풀이
1 parent d92ad50 commit cc55a99

2 files changed

Lines changed: 120 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <cstring>
2+
#include <iostream>
3+
4+
using namespace std;
5+
6+
long long MAX = 1000000000;
7+
long long bino[201]
8+
[201]; // 경우의 수가 기하 급수적으로 늘어나기 때문에 long long
9+
int n, m;
10+
long long k;
11+
12+
void combi() {
13+
for (int c = 0; c < 201; c++) {
14+
bino[c][0] = bino[c][c] = 1;
15+
for (int r = 1; r < c; r++) {
16+
bino[c][r] = min(MAX, bino[c - 1][r - 1] + bino[c - 1][r]);
17+
}
18+
}
19+
}
20+
21+
string dictionary(int n, int m, long long k) {
22+
if (n == 0) {
23+
return string(m, 'z');
24+
}
25+
if (m == 0)
26+
return string(n, 'a');
27+
28+
if (k <= bino[n + m - 1][n - 1]) {
29+
return "a" + dictionary(n - 1, m, k);
30+
}
31+
return "z" + dictionary(n, m - 1, k - bino[n + m - 1][n - 1]);
32+
}
33+
34+
int main(void) {
35+
memset(bino, 0, sizeof(bino));
36+
cin >> n >> m >> k;
37+
38+
combi();
39+
if (bino[n + m][n] < k) {
40+
cout << -1;
41+
} else {
42+
cout << dictionary(n, m, k);
43+
}
44+
45+
return 0;
46+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <iostream>
2+
#include <vector>
3+
using namespace std;
4+
5+
const int N = 10;
6+
const int INF = 987654321;
7+
8+
int board[N][N];
9+
vector<int> stock(6, 5);
10+
int bestAns = INF;
11+
12+
bool canPlace(int y, int x, int k) {
13+
if (y + k > N || x + k > N)
14+
return false;
15+
for (int i = 0; i < k; ++i)
16+
for (int j = 0; j < k; ++j)
17+
if (board[y + i][x + j] == 0)
18+
return false;
19+
return true;
20+
}
21+
22+
void fillSquare(int y, int x, int k, int val) {
23+
for (int i = 0; i < k; ++i)
24+
for (int j = 0; j < k; ++j)
25+
board[y + i][x + j] = val;
26+
}
27+
28+
bool findNextOne(int &y, int &x) {
29+
for (int i = 0; i < N; ++i)
30+
for (int j = 0; j < N; ++j)
31+
if (board[i][j] == 1) {
32+
y = i;
33+
x = j;
34+
return true;
35+
}
36+
return false;
37+
}
38+
39+
void dfs(int used) {
40+
if (used >= bestAns)
41+
return;
42+
43+
int y, x;
44+
if (!findNextOne(y, x)) {
45+
bestAns = min(bestAns, used);
46+
return;
47+
}
48+
49+
for (int k = 5; k >= 1; --k) {
50+
if (stock[k] == 0)
51+
continue;
52+
if (!canPlace(y, x, k))
53+
continue;
54+
fillSquare(y, x, k, 0);
55+
stock[k]--;
56+
dfs(used + 1);
57+
stock[k]++;
58+
fillSquare(y, x, k, 1);
59+
}
60+
}
61+
62+
int main() {
63+
ios::sync_with_stdio(false);
64+
cin.tie(nullptr);
65+
66+
for (int i = 0; i < N; ++i)
67+
for (int j = 0; j < N; ++j)
68+
cin >> board[i][j];
69+
70+
dfs(0);
71+
72+
cout << (bestAns == INF ? -1 : bestAns) << '\n';
73+
return 0;
74+
}

0 commit comments

Comments
 (0)