-
Notifications
You must be signed in to change notification settings - Fork 0
/
PD.cpp
80 lines (56 loc) · 844 Bytes
/
PD.cpp
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
#include <bits/stdc++.h>
using namespace std;
int queen[15];
// n row;
// queen[n] col;
int ans;
int r,c;
int isConflict(int row,int col)
{
for(int t=1;t<=9;t++)
{
if( queen[row]!=0 || col==queen[t] || (queen[t]!=0 && ( abs(col-queen[t])==abs(row-t) ) ) )
return 1;
}
return 0;
}
int find(int curRow,int queenNum)
{
if(queenNum==9)
{
ans++;
return 0;
}
if(curRow==10) return 0;
if(curRow == r )
{
find(curRow+1,queenNum);
}
for(int t=1;t<=9;t++)
{
if(!isConflict(curRow,t))
{
queen[curRow]=t;
find(curRow+1,queenNum+1);
queen[curRow]=0;
}
}
return 0;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int t=0;t<n;t++)
{
memset(queen,0,sizeof(queen));
ans=0;
scanf("%d %d",&r,&c);
queen[r]=c;
find(1,1);
printf("%d\n",ans);
}
}
return 0;
}