Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
Tags
- BFS
- ์์ ์ ๋ ฌ
- ๋ฐฑํธ๋ํน
- c++
- error
- thymeleaf
- ์ต๋จ ๊ฒฝ๋ก
- java
- Reversing
- OS
- ๋์ ํฉ
- ๊ทธ๋ฆฌ๋
- ๋งต
- ์๋ฎฌ๋ ์ด์
- ์ฌ๊ท
- ๋ถํ ์ ๋ณต
- ์คํ
- dynamic debugging
- ๋ฐ์ดํฌ์คํธ๋ผ
- ๊ตฌํ
- ๋ฌธ์์ด
- GCP
- JPA
- CVE
- web
- Spring
- DP
- ์ฐ์ ์์ ํ
- dfs
- ์ด๋ถ ํ์
Archives
- Today
- Total
hades
[Baekjoon] 9663๋ฒ: N-Queen ๋ณธ๋ฌธ
๐ฅ ๋ฌธ์
https://www.acmicpc.net/problem/9663
๐ ์ค๊ณ
N-Queen์์ ํธ๋ค์ ๊ฐ์ ํ, ๊ฐ์ ์ด, ๋๊ฐ์ ์ ๋์ด์ง ๋ง์์ผ ํ๋ค.
ํ ํ์ฉ ๋ชจ๋ ์ด์ ๋ฐฐ์น๋ฅผ ์๋ํ๊ณ , ๋ฐฐ์น๊ฐ ๊ฐ๋ฅํ๋ฉด, ๋ค์ ํ์ผ๋ก ๋์ด๊ฐ์ ๋ฐฐ์นํ๋ค. ์ด ๊ณผ์ ์ ๋ฐ๋ณตํ๋ค.
check์์ ํ์ฌ ํ๊ณผ ํ์ฌ ํ ์ด์ ๊น์ง์ ํ์ ์ดํด๋ณด๊ธฐ ๋๋ฌธ์ ๊ฐ์ ํ์ ๋์ผ ์ ์๊ณ , ๊ฐ์ ์ด์ธ์ง, ๋๊ฐ์ ์ ์์นํ๋์ง๋ง ํ์ธํ๋ฉด ๋๋ค.
๐ ํ์ด
#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
int n, result = 0;
vector<int> col(15);
bool check(int cur_row) {
for (int i = 1; i < cur_row; i++) {
if (col[i] == col[cur_row]) {
return false;
}
if (cur_row - i == abs(col[i] - col[cur_row])) {
return false;
}
}
return true;
}
void nqueen(int cur_row) {
if (cur_row == n + 1) {
result += 1;
return;
}
for (int i = 1; i <= n; i++) {
col[cur_row] = i;
if (check(cur_row)) {
nqueen(cur_row + 1);
}
}
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
nqueen(1);
cout << result << "\n";
return 0;
}
'๐ PS > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 9935๋ฒ: ๋ฌธ์์ด ํญ๋ฐ (0) | 2024.09.04 |
---|---|
[Baekjoon] 1197๋ฒ: ์ต์ ์คํจ๋ ํธ๋ฆฌ (0) | 2024.09.02 |
[Baekjoon] 1932๋ฒ: ์ ์ ์ผ๊ฐํ (0) | 2024.08.19 |
[Baekjoon] 15666๋ฒ: N๊ณผ M (12) (0) | 2024.08.13 |
[Baekjoon] 2638๋ฒ: ์น์ฆ (0) | 2024.08.13 |