Post

BOJ9663 N-Queen

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
#include <iostream>
#include <bits/stdc++.h>

using namespace std;

vector<bool> diag1,diag2,col;
int c = 0;

void search(int N,int y){
    if (y==N){
        c++;
        return;
    }
    for(int x=0;x<N;x++){
        if (col[x] || diag1[x+y] || diag2[x-y+N-1]) continue;
        col[x] = true;
        diag1[x+y] = true;
        diag2[x-y+N-1] = true;

        search(N,y+1);

        col[x] = false;
        diag1[x+y] = false;
        diag2[x-y+N-1] = false;
    }
}

int main() {
	cin.tie(0); cout.tie(0);
	ios::sync_with_stdio(0);

    int N; cin >> N;

    col.resize(N, false);
    diag1.resize(2*N-1, false);
    diag2.resize(2*N-1, false);

    search(N,0);

    cout << c;

	return 0;
}

예전부터 풀어야겠다고 생각은 했지만…
책에 코드가 나와서 풀이 제출함
N도 전역변수로 쓰면되는건데….

This post is licensed under CC BY 4.0 by the author.