就这个广搜爽
2024-05-21 13:17:17
发布于:广东
24阅读
0回复
0点赞
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
struct node{
int a[9] = {0}, ct = 0;
bool vis[9] = {0};
};
bool check(node n){
for(int i = 1; i < n.ct; i++){
if(n.a[i] + i == n.a[n.ct] + n.ct || n.a[i] - i == n.a[n.ct] - n.ct) return 0;
}return 1;
}int ct;
void bfs(){
node n;
queue <node> q;
q.push(n);
while(!q.empty()){
node head = q.front();
q.pop();
if(head.ct == 8){
printf("sum=%d\n", ++ct);
for(int i = 1; i <= 8; i++) printf("%2d", head.a[i]);
cout << endl;
continue;
}for(int i = 1; i <= 8; i++){
if(!head.vis[i]){
node xx = head;
xx.a[++xx.ct] = i;
if(check(xx)){
xx.vis[i] = 1;
q.push(xx);
}
}
}
}
}
int main(){
bfs();
return 0;
}
这里空空如也
有帮助,赞一个