Untitled
1 week ago in Plain Text
//
// main.c
// N-Queens M-Rooks Problem N-Queens M-Rooks Problem N-Queens M-Rooks Problem N-Queens M-Rooks Problem N-Queens M-Rooks Problem N-Queens M-Rooks Problem N-Queens M-Rooks Problem
//
// Created by Angela on 2021/2/3.
//

/*#include<stdio.h>
void place(int row, int n, int m);
int valid(int row, int col, char r);
void display(void);
int position[15];
int role[15];
int N, M, total, count=0;
int main(void)
{
while(scanf("%d %d", &N, &M)!=EOF)
{
count=0;
total=N+M;
place(0, N, M);
printf("%d\n", count);
}

return 0;
}
void place(int row, int n, int m)
{
int i;

if(row==total)
{
display();
}
else
{
for(i=0; i<total; i++)
{
if(valid(row, i, 'Q') && n>0)
{
position[row]=i;
role[row]='Q';
place(row+1, n-1, m);
}
if(valid(row, i, 'R') && m>0)
{
position[row]=i;
role[row]='R';
place(row+1, n, m-1);
}
}
}
}
int valid(int row, int col, char r)
{
int i;

if(r=='Q')
{
for(i=0; i<=row-1; i++)
{
if(position[i]==col || position[i]==col-row+i || position[i]==col+row-i)
return 0;
}
}
if(r=='R')
{
for(i=0; i<=row-1; i++)
{
if(role[i]=='Q')
{
if(position[i]==col || position[i]==col-row+i || position[i]==col+row-i)
return 0;
}
if(role[i]=='R')
{
if(position[i]==col)
return 0;
}
}
}

return 1;
}
void display(void)
{
count++;
// int i;
// for(i=0; i<total; i++)
// {
// printf("%d ", position[i]);
// printf("%c\n", role[i]);
// }
return;
}*/
#include<stdio.h>
void place(int row, int n, int m);
int valid(int row, int col, char r);
void display(void);
int column[10]={0};
int Qright[20]={0};
int Qleft[20]={0};
int Rright[20]={0};
int Rleft[20]={0};
int N, M, total, count=0;
int main(void)
{
while(scanf("%d %d", &N, &M)!=EOF)
{
count=0;
total=N+M;
place(0, N, M);
printf("%d\n", count);
}

return 0;
}
void place(int row, int n, int m)
{
int i;

if(row==total)
{
display();
}
else
{
for(i=0; i<total; i++)
{
if(valid(row, i, 'Q') && n>0)
{
column[i]++;
Qright[row+i]++;
Qleft[i-row+total-1]++;
Rright[row+i]++;
Rleft[i-row+total-1]++;
place(row+1, n-1, m);
column[i]--;
Qright[row+i]--;
Qleft[i-row+total-1]--;
Rright[row+i]--;
Rleft[i-row+total-1]--;
}
if(valid(row, i, 'R') && m>0)
{
column[i]++;
Qright[row+i]++;
Qleft[i-row+total-1]++;
place(row+1, n, m-1);
column[i]--;
Qright[row+i]--;
Qleft[i-row+total-1]--;
}
}
}
}
int valid(int row, int col, char r)
{
if(r=='Q')
{
if(column[col]>0 || Qright[row+col]>0 || Qleft[col-row+total-1]>0)
return 0;
}
if(r=='R')
{
if(column[col]>0 || Rright[row+col]>0 || Rleft[col-row+total-1]>0)
return 0;
}
return 1;
}
void display(void)
{
count++;

return;
}
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176