八皇后问题

八皇后问题是经典的回溯问题。

问题表述

在 8×8 格的国际象棋上摆放8个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行、同一列或同一斜线上,问有多少种摆法。
在这里插入图片描述

当我们选择了第一个皇后的位置之后,与其处于同行同列同斜线的位置便都无法被选择,第二个皇后只能放在未被第一个皇后所辐射到的位置上。

接着放置第三个皇后,同样不能放在被前两个皇后辐射到的位置上,若此时已经没有未被辐射的位置能够被选择,也就意味着这种摆法是不可行的,我们需要回退到上一步,给第二个皇后重新选择一个未被第一个皇后辐射的位置,再来看是否有第三个皇后可以摆放的位置。

若还是没有适合第三个皇后的位置,则再次回退至选择第二个皇后的位置这一步操作,若第二个皇后也没有更多的选择则回退到第一个皇后,重新进行位置的选择。这种通过试探回退的思路就是回溯法

回溯法基本思路

回溯法常常使用DFS(深度优先搜索)来进行对解空间的答案搜索。

首先从根部节点出发搜索解空间,当搜索至解空间的某一节点时,先利用约束或限界剪枝函数来判断该节点是否是问题的解。若不是,则跳过对该节点为根的子树的搜索,逐层向其祖先节点回溯;否则,进入该子树,继续按深度优先策略搜索。

回溯法的基本行为是优雅的暴力搜索,搜索过程进行剪枝来为了避免无效冗余的搜索。

例题分析

下面看一道八皇后题型的例题。

题目题干:
                  Checker Challenge

  Examine the 6x6 checkerboard below and note that the six checkers are arranged on the board so that one and only one is placed in each row and each column, and there is never more than one in any diagonal. (Diagonals run from southeast to northwest and southwest to northeast and include all diagonals, not just the major two.)
在这里插入图片描述
  The solution shown above is described by the sequence 2 4 6 1 3 5, which gives the column positions of the checkers for each row from 1 to 6:

      ROW 1 2 3 4 5 6
      COLUMN 2 4 6 1 3 5

  This is one solution to the checker challenge. Write a program that finds all unique solution sequences to the Checker Challenge (with ever growing values of N). Print the solutions using the column notation described above. Print the the first three solutions in numerical order, as if the checker positions form the digits of a large number, and then a line with the total number of solutions.

  Special note: the larger values of N require your program to be especially efficient. Do not precalculate the value and print it (or even find a formula for it); that’s cheating. Work on your program until it can solve the problem properly. If you insist on cheating, your login to the USACO training pages will be removed and you will be disqualified from all USACO competitions. YOU HAVE BEEN WARNED.

INPUT FORMAT

A single line that contains a single integer N (6 <= N <= 13) that is the dimension of the N x N checkerboard.
SAMPLE INPUT (file checker.in)
6

OUTPUT FORMAT

The first three lines show the first three solutions found, presented as N numbers with a single space between them. The fourth line shows the total number of solutions found.
SAMPLE OUTPUT (file checker.out)
2 4 6 1 3 5
3 6 2 5 1 4
4 1 5 2 6 3
4

题目大意:

一个 6×6 的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行、每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子。

我们需要找出所有棋子放置的解,并把它们以上面的序列方法输出,解按字典顺序排列(输出前三种组合即可)。

解题思路:

通过观察可知,对于一条从右上到左下的对角线,其上的棋子坐标应满足 x+y 为一定值。

对于一条从左上到右下的对角线,其上的棋子坐标应满足 x-y 为一定值,为了避免产生负数,代码中用 x-y+d 来储存数字。

因此只需满足:

if(check[0][i] != 1)
	if(check[1][t+i] != 1)
		if(check[2][t-i+d] != 1)

只要满足这三个数字均使用过,则在 temp[t]=i 处放置棋子,并将check数组中的相应数值标记为已使用,然后对下一行进行搜索。

AC代码:

#include<bits/stdc++.h>
using namespace std;

int d, ans, cnt, temp[100], check[100][100]; // d是棋盘的维度 

void Eight_Queen(int t)
{
    if(t > d)
    {
        cnt++;
        if(cnt > 3)
            return ;
        for(int i=1; i<=d; i++)
            cout<<temp[i]<<" ";
        cout<<endl;
        return ;
    }
    
    for(int i=1; i<=d; i++)
    {
        if(check[0][i] != 1)
        {
            if(check[1][t+i] != 1)
            {
                if(check[2][t-i+d] != 1)
                {
                    temp[t] = i;
                    check[0][i] = check[1][t+i] = check[2][t-i+d] = 1;
                    Eight_Queen(t+1); 
                    check[0][i] = check[1][t+i] = check[2][t-i+d] = 0;
                }
            }
        }
    }
}

int main()
{
    cin>>d;
    Eight_Queen(1);
    cout<<cnt;
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值