</pre></h2></center><div style="color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:13px; line-height:18px"><h2 style="margin:0px; font-family:inherit; color:blue; font-size:24px; line-height:36px"></h2><h2 style="margin:0px; font-family:inherit; color:blue; font-size:24px; line-height:36px">Description</h2><div class="content" style="background-color:rgb(228,240,248); font-family:'Courier New',Courier,'Times New Roman',monospace; font-size:20px; line-height:24px; height:auto; margin:0px; padding:0px 20px">检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行、每列只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子。 列号 1 2 3 4 5 6 ------------------------- 1 | | O | | | | | ------------------------- 2 | | | | O | | | ------------------------- 3 | | | | | | O | ------------------------- 4 | O | | | | | | ------------------------- 5 | | | O | | | | ------------------------- 6 | | | | | O | | ------------------------- 上面的布局可以用序列2 4 6 1 3 5来描述,第i个数字表示在第i行的相应位置有一个棋子,如下: 行号 1 2 3 4 5 6 列号 2 4 6 1 3 5 这只是跳棋放置的一个解。请编一个程序找出所有跳棋放置的解。并把它们以上面的序列方法输出。解按字典顺序排列。请输出前3个解。最后一行是解的总个数。 </div><h2 style="margin:0px; font-family:inherit; color:blue; font-size:24px; line-height:36px">Input</h2><div class="content" style="background-color:rgb(228,240,248); font-family:'Courier New',Courier,'Times New Roman',monospace; font-size:20px; line-height:24px; height:auto; margin:0px; padding:0px 20px">一个数字N (6 <= N <= 13) 表示棋盘是N x N大小的。 </div><h2 style="margin:0px; font-family:inherit; color:blue; font-size:24px; line-height:36px">Output</h2><div class="content" style="background-color:rgb(228,240,248); font-family:'Courier New',Courier,'Times New Roman',monospace; font-size:20px; line-height:24px; height:auto; margin:0px; padding:0px 20px">前三行为前三个解,每个解的两个数字之间用一个空格隔开。第四行只有一个数字,表示解的总数。 </div><h2 style="margin:0px; font-family:inherit; color:blue; font-size:24px; line-height:36px">Sample Input</h2><div class="content" style="background-color:rgb(228,240,248); font-family:'Courier New',Courier,'Times New Roman',monospace; font-size:20px; line-height:24px; height:auto; margin:0px; padding:0px 20px">6</div><h2 style="margin:0px; font-family:inherit; color:blue; font-size:24px; line-height:36px">Sample Output</h2><div class="content" style="background-color:rgb(228,240,248); font-family:'Courier New',Courier,'Times New Roman',monospace; font-size:20px; line-height:24px; height:auto; margin:0px; padding:0px 20px">2 4 6 1 3 5 3 6 2 5 1 4 4 1 5 2 6 3 4</div><h2 style="margin:0px; font-family:inherit; color:blue; font-size:24px; line-height:36px"></h2><div>N 皇后为简单的搜索问题,剪枝有很多种方法, 可以用for循环来判断是否可行, 也可以直接用数组标记一个if就可已了,直接上代码</div><div><pre code_snippet_id="140398" snippet_file_name="blog_20140103_1_3332749" name="code" class="cpp">#include<stdio.h>
int a[15] = {0}, col[15] = {0}, ldia[30] = {0}, rdia[30] = {0};
int n, count = 0;
void queen(int m){
int i;
if(m == n+1){
count++;
if(count <= 3){
for(i = 1; i < n; i++)
printf("%d ", a[i]);
printf("%d", a[n]);
printf("\n");
}
}
else{
for(i = 1; i <= n; i++){
if(col[i] == 0 && ldia[i + m] == 0 && rdia[i - m + 8] == 0){
a[m] = i;
col[i] = 1;
ldia[i + m] = 1;
rdia[i - m + 8] = 1;
queen(m+1);
col[i] = 0;
ldia[i + m] = 0;
rdia[i - m + 8] = 0;
}
}
}
}
int main(){
scanf("%d", &n);
queen(1);
printf("%d\n", count);
return 0;
}
</pre></h2></center><div style="color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; font-size:13px; line-height:18px"><h2 style="margin:0px; font-family:inherit; color:blue; font-size:24px; line-height:36px"></h2><h2 style="margin:0px; font-family:inherit; color:blue; font-size:24px; line-height:36px">Description</h2><div class="content" style="background-color:rgb(228,240,248); font-family:'Courier New',Courier,'Times New Roman',monospace; font-size:20px; line-height:24px; height:auto; margin:0px; padding:0px 20px">检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行、每列只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子。 列号 1 2 3 4 5 6 ------------------------- 1 | | O | | | | | ------------------------- 2 | | | | O | | | ------------------------- 3 | | | | | | O | ------------------------- 4 | O | | | | | | ------------------------- 5 | | | O | | | | ------------------------- 6 | | | | | O | | ------------------------- 上面的布局可以用序列2 4 6 1 3 5来描述,第i个数字表示在第i行的相应位置有一个棋子,如下: 行号 1 2 3 4 5 6 列号 2 4 6 1 3 5 这只是跳棋放置的一个解。请编一个程序找出所有跳棋放置的解。并把它们以上面的序列方法输出。解按字典顺序排列。请输出前3个解。最后一行是解的总个数。 </div><h2 style="margin:0px; font-family:inherit; color:blue; font-size:24px; line-height:36px">Input</h2><div class="content" style="background-color:rgb(228,240,248); font-family:'Courier New',Courier,'Times New Roman',monospace; font-size:20px; line-height:24px; height:auto; margin:0px; padding:0px 20px">一个数字N (6 <= N <= 13) 表示棋盘是N x N大小的。 </div><h2 style="margin:0px; font-family:inherit; color:blue; font-size:24px; line-height:36px">Output</h2><div class="content" style="background-color:rgb(228,240,248); font-family:'Courier New',Courier,'Times New Roman',monospace; font-size:20px; line-height:24px; height:auto; margin:0px; padding:0px 20px">前三行为前三个解,每个解的两个数字之间用一个空格隔开。第四行只有一个数字,表示解的总数。 </div><h2 style="margin:0px; font-family:inherit; color:blue; font-size:24px; line-height:36px">Sample Input</h2><div class="content" style="background-color:rgb(228,240,248); font-family:'Courier New',Courier,'Times New Roman',monospace; font-size:20px; line-height:24px; height:auto; margin:0px; padding:0px 20px">6</div><h2 style="margin:0px; font-family:inherit; color:blue; font-size:24px; line-height:36px">Sample Output</h2><div class="content" style="background-color:rgb(228,240,248); font-family:'Courier New',Courier,'Times New Roman',monospace; font-size:20px; line-height:24px; height:auto; margin:0px; padding:0px 20px">2 4 6 1 3 5 3 6 2 5 1 4 4 1 5 2 6 3 4</div><h2 style="margin:0px; font-family:inherit; color:blue; font-size:24px; line-height:36px"></h2><div>N 皇后为简单的搜索问题,剪枝有很多种方法, 可以用for循环来判断是否可行, 也可以直接用数组标记一个if就可已了,直接上代码</div><div><pre code_snippet_id="140398" snippet_file_name="blog_20140103_1_3332749" name="code" class="cpp">#include<stdio.h>
int a[15] = {0}, col[15] = {0}, ldia[30] = {0}, rdia[30] = {0};
int n, count = 0;
void queen(int m){
int i;
if(m == n+1){
count++;
if(count <= 3){
for(i = 1; i < n; i++)
printf("%d ", a[i]);
printf("%d", a[n]);
printf("\n");
}
}
else{
for(i = 1; i <= n; i++){
if(col[i] == 0 && ldia[i + m] == 0 && rdia[i - m + 8] == 0){
a[m] = i;
col[i] = 1;
ldia[i + m] = 1;
rdia[i - m + 8] = 1;
queen(m+1);
col[i] = 0;
ldia[i + m] = 0;
rdia[i - m + 8] = 0;
}
}
}
}
int main(){
scanf("%d", &n);
queen(1);
printf("%d\n", count);
return 0;
}