SDJ( 수돈재 아님 ㅎ )

[C++] 9663 - N-Queen 본문

알고리즘/Backjoon

[C++] 9663 - N-Queen

ShinDongJun 2020. 1. 5. 19:29

문제 링크 : https://www.acmicpc.net/problem/9663

 

9663번: N-Queen

N-Queen 문제는 크기가 N × N인 체스판 위에 퀸 N개를 서로 공격할 수 없게 놓는 문제이다. N이 주어졌을 때, 퀸을 놓는 방법의 수를 구하는 프로그램을 작성하시오.

www.acmicpc.net

 

 

 

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include<iostream>
#include<algorithm>
#define endl '\n'
 
using namespace std;
 
int N[20][20];
int n;
int cnt;
 
void fill(int y, int x);
void clear(int y, int x);
void check(int k);
 
int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> n;
    check(0);
 
    cout << cnt << endl;
 
    return 0;    
}
 
void fill(int y, int x)
{
    int tmp_y = y;
    int tmp_x = x;
 
    while((0 < y) && (0 < x))
        {y--;x--;}
    while((0 <= y && y < n) && (0 <= x && x < n))
        N[y++][x++]+=1;
 
    y = tmp_y;
    x = tmp_x;
    
    while((0 < y) && (x < n-1))
        {y--;x++;}
    while((0 <= y && y < n) && (0 <= x && x < n))
        N[y++][x--]+=1;
 
    y = tmp_y;
    x = tmp_x;
 
    while((0 < y))
        y--;
    while((0 <= y && y < n) && (0 <= x && x < n))
        N[y++][x]+=1;
    
    y = tmp_y;
    x = tmp_x;
    
    while((0 < x))
        x--;
    while((0 <= y && y < n) && (0 <= x && x < n))
        N[y][x++]+=1;
 
    N[tmp_y][tmp_x]-=3;
}
 
void clear(int y, int x)
{
    int tmp_y = y;
    int tmp_x = x;
 
    while((0 < y) && (0 < x))
        {y--;x--;}
    while((0 <= y && y < n) && (0 <= x && x < n))
        N[y++][x++]-=1;
 
    y = tmp_y;
    x = tmp_x;
    
    while((0 < y) && (x < n-1))
        {y--;x++;}
    while((0 <= y && y < n) && (0 <= x && x < n))
        N[y++][x--]-=1;
 
    y = tmp_y;
    x = tmp_x;
 
    while((0 < y))
        y--;
    while((0 <= y && y < n) && (0 <= x && x < n))
        N[y++][x]-=1;
    
    y = tmp_y;
    x = tmp_x;
    
    while((0 < x))
        x--;
    while((0 <= y && y < n) && (0 <= x && x < n))
        N[y][x++]-=1;
 
    N[tmp_y][tmp_x]+=3;
}
 
void check(int k)
{
    if(k == n)
    {
        cnt++;
        return;
    }
 
    for(int i = 0; i < n; ++i)
    {
        if(N[k][i] == 0)
        {
            fill(k, i);
            check(k+1);
            clear(k, i);
        }
    }
}

'알고리즘 > Backjoon' 카테고리의 다른 글

[Python3] 2661 - 좋은수열  (0) 2020.01.07
[Python3] 3009 - 네 번째 점  (0) 2020.01.07
[Python3] 16212 - 정열적인 정렬  (0) 2020.01.05
[Python3] 2355 - 시그마  (0) 2020.01.05
[C++] 10451 - 순열 사이클  (0) 2020.01.05
Comments