λ¬Έμ λ§ν¬
https://www.acmicpc.net/problem/1799
λ°±νΈλνΉμ κ΄ν λ¬Έμ λ€.
νμ΄
μ²μ λ¬Έμ λ₯Ό μ½μμ λ μ μ΄κ² 골λ1 λμ΄λμ λ¬Έμ μΌκΉ? λΌλ μλ¬Έμ΄ λ€μλ€. λΉμμ΄ λ€μ΄κ° μμΉλ μ ν΄μ Έμκ³ κ±°κΈ°μ λΉμμ΄
1. λμ΄κ±°λ
2. μλμ΄κ±°λ λ μ€ νλλ€.
λΉμμ΄ λμΌ μ μλ λͺ¨λ μμΉλ₯Ό 벑ν°μ μ μ₯ν λ€ κ·Έ μμΉλ₯Ό λ°±νΈλνΉμΌλ‘ λͺ¨λ λλ©΄μ depthκ° λΉμμ΄ λμΌ μ μλ λͺ¨λ μΉΈμ κ°μμ κ°μμ§λ©΄ κ·Έλ 체μ€νμ λμ¬μλ 체μ€μ κ°μ μ€ μ΅λκ°μ μΆλ ₯νλ©΄ λκ² κ΅¬λ λΌκ³ μκ°νλ€. μκ°λ 10μ΄λ©΄ λλν νΈμ΄λ λ°λ‘ μ½λλ₯Ό μ§°λ€.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <climits>
#include <algorithm>
#include <tuple>
#include <cmath>
using namespace std;
typedef long long ll;
int N;
const int MAX_N = 10 + 3;
int chess[MAX_N][MAX_N];
vector<pair<int,int>> cango; // λΉμμ΄ λμΌ μ μλ μμΉ
int MAX;
bool is_ok(int y, int x) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (chess[i][j] == 2) {
if (abs(i - y) == abs(j - x))
return false;
}
}
}
return true;
}
void backtracking(int depth, int B) {
if (depth == cango.size()) {
MAX = max(MAX, B);
return;
}
if (is_ok(cango[depth].first, cango[depth].second)) { //λ€μμΉΈμ κ° μ μμΌλ©΄
chess[cango[depth].first][cango[depth].second]++;
backtracking(depth + 1, B + 1);
chess[cango[depth].first][cango[depth].second]--;
}
backtracking(depth + 1, B);
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> chess[i][j];
if (chess[i][j])
cango.push_back({ i,j });
}
}
backtracking(0, 0);
cout << MAX << "\n";
return 0;
}
|
cs |
λ¬Όλ‘ μμ κ° λ§μμ μ μΆνλλ° κ·Έλλ‘ μκ°μ΄κ³Ό. μ΄ λ°©λ²μ΄ μΈμ λ±ν λ μ€λ₯΄λ λ°©λ²μ΄ μμλ€. 1μκ° λκ² κ³ λ―Όνμ§λ§ κ²°κ΅ μ±μ λμ λΈλ‘κ·Έλ₯Ό μ°Έκ³ νλ€.
체μ€μ νΉμ±μ μ΄μ©ν΄μ κ²μμ μΉΈκ³Ό ν°μ μΉΈμ λλ μ μκ°νλ©΄ μκ°μ λ¨μΆν μ μλ€. μλ κ²μ μΉΈκ³Ό ν° μΉΈμ μλ‘ μ λ κ°μ λκ°μ μ μμ μ μλ€.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <climits>
#include <algorithm>
#include <tuple>
#include <cmath>
using namespace std;
typedef long long ll;
int N;
const int MAX_N = 10 + 3;
int chess[MAX_N][MAX_N];
vector<pair<int, int>> cango[2];
stack<pair<int, int>> bishop[2];
int MAX[2];
bool is_ok(int y, int x, int depth, int black_white) {
stack<pair<int, int>> S(bishop[black_white]);
while (!S.empty()) {
int temp_y = S.top().first;
int temp_x = S.top().second;
S.pop();
if (abs(y - temp_y) == abs(x - temp_x))
return false;
}
return true;
}
//void show_chess() {
// for (int i = 0; i < N; i++) {
// for (int j = 0; j < N; j++) {
// cout << chess[i][j] << " ";
// }
// cout << "\n";
// }
//}
void backtracking(int depth, int B, int black_white) {
if (depth == cango[black_white].size()) {
MAX[black_white] = max(MAX[black_white], B);
return;
}
if (is_ok(cango[black_white][depth].first, cango[black_white][depth].second, depth, black_white)) { //λ€μμΉΈμ κ° μ μμΌλ©΄
chess[cango[black_white][depth].first][cango[black_white][depth].second]++;
bishop[black_white].push({cango[black_white][depth].first, cango[black_white][depth].second});
backtracking(depth + 1, B + 1, black_white);
bishop[black_white].pop();
chess[cango[black_white][depth].first][cango[black_white][depth].second]--;
}
backtracking(depth + 1, B, black_white);
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> chess[i][j];
if (chess[i][j]) {
if (i % 2 == 0 && j % 2 == 0)
cango[0].push_back({ i,j });
else if (i % 2 == 1 && j % 2 == 1)
cango[0].push_back({ i,j });
else cango[1].push_back({ i,j });
}
}
}
backtracking(0, 0, 0);
backtracking(0, 0, 1);
cout << MAX[0] + MAX[1] << "\n";
return 0;
}
|
cs |
κ·Έλ κ² κΈ°μ‘΄ μ½λμμ κΈνκ² μ½λλ₯Ό λ°κΏ μ μΆν΄μ ACλ₯Ό λ°μλ€.
μ²μ μ½λμ μκ°λ³΅μ‘λλ 2 * N * N (2λ λ°±νΈλνΉμμ λΉμμ λλ μλλ)
λ°λ° λλ μ ν μ½λμ μκ°λ³΅μ‘λλ 2* N/2 * N/2 λ‘ ν¨μ¬ λ μ€μ΄λ€μλ€.
체μ€νμ νΉμ±μ νμ©νλ μΌμ€κ° νμνλ λ¬Έμ λ€.
'λ°±μ€ > Gold' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[BOJ] 1707 : μ΄λΆ κ·Έλν (0) | 2022.01.21 |
---|---|
[BOJ] 1579 : μ± (0) | 2022.01.20 |
[BOJ] 10942 : ν°λ¦°λ둬? (0) | 2022.01.20 |
[BOJ] 2629 : μνμ μΈ (0) | 2022.01.19 |
[BOJ] 2661: μ’μ μμ΄ (0) | 2022.01.19 |