回溯法_八皇后问题

题目描述

八皇后问题是一个经典的排列问题,最早由国际象棋棋手马克斯·贝瑟尔(Max Bezzel)于1848年提出。问题的目标是在一个8×8的棋盘上放置八个皇后,使得它们互相之间不能互相攻击,即任意两个皇后不能位于同一行、同一列或同一对角线上。

思考

解决八皇后问题可以使用回溯算法或者递归算法。基本思想是逐行放置皇后,每一行只能放置一个皇后,并且保证当前行放置的皇后不与之前行放置的皇后相互攻击。如果在某一行无法找到合适的位置放置皇后,则回溯到上一行重新选择位置。

方法

1 回溯法

(1)确定函数参数和返回值
void solve_queen(int StartIndex,int n,int* arr,vector<vector<int>>& Ret)
(2)确定终止条件
if(!Check(n,arr)){
return;
}
//剪枝
if(StartIndex==n && Check(n,arr)){
arr==>Ret;
return;
}
(3)确定单层搜素逻辑
for(int i=StartIndex*n;i<StartIndex*n+n;++i){
arr[i]=true;
solve_queen(StartIndex+1,n,arr,Ret);
arr[i]=false;
}

代码实现

1 回溯法

#include<iostream>
#include<graphics.h>
#include<easyx.h>
#include<vector>
#include <windows.h>//sleep()函数

//初始化棋盘
void initialChessboard(int n, bool*& arr) {
	arr = (bool*)malloc(sizeof(bool) * (n * n));
	for (int i = 0; i < (n * n); ++i) {
		arr[i] = false;
	}
}
//展示棋盘
void ShowChessboard(int n,bool* arr) {
	for (int i = 1; i <= n + 1; ++i) {
		line(50, i * 50, 50 * (n + 1), i * 50);
		line(i * 50, 50, i * 50, 50 * (n + 1));
	}
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (arr[i * n + j]) {
				setfillcolor(GREEN);
				fillcircle((j + 1) * 50 + 25, (i + 1) * 50 + 25, 25);
				setfillcolor(WHITE);

			}
		}
	}
}
//标记棋子
void Sign(int n, int num) {
	int line = num / n;
	int row = num % n;
	setfillcolor(RED);
	fillcircle((row + 1) * 50 + 25, (line + 1) * 50 + 25, 25);
	setfillcolor(WHITE);
}
//检查是否合法
bool Check(int n, bool* arr) {
	//逐行检测
	for (int i = 0; i < n; ++i) {
		int flag = -1;//标志在一行中是否存在过棋子
		for (int j = 0; j < n; ++j) {
			if (arr[i * n + j]) {
				if (flag!=-1) {
					Sign(n, flag);
					Sign(n, i * n + j);
					return false;
				}
				flag = i*n+j;
			}
		}
	}
	//逐列检测
	for (int i = 0; i < n; ++i) {
		int flag = -1;//标志在一列中是否存在过棋子
		for (int j = 0; j < n; ++j) {
			if (arr[i + j * n]) {
				if (flag!=-1) {
					Sign(n, flag);
					Sign(n, i + j * n);
					return false;
				}
				flag = i + j * n;
			}
		}
	}
	//逐对角线检查
	//左上到右下
	for (int i = 0; i < n - 1; ++i) {
		int flag = -1;
		int j = i;
		while (j%n || j==0) {
			if (arr[j]) {
				if (flag!=-1) {
					Sign(n, flag);
					Sign(n, j);
					return false;
				}
				flag = j;
			}
			j += (n + 1);
		}
	}
	for (int i = n; i < n*(n-1); i=i+n) {
		int flag = -1;
		int j = i;
		while (j < n* n) {
			if (arr[j]) {
				if (flag!=-1) {
					Sign(n, flag);
					Sign(n, j);
					return false;
				}
				flag = j;
			}
			j += (n + 1);
		}
	}
	//右上到左下
	for (int i = n-1; i >0; --i) {
		int flag = -1;
		int j = i;
		while ((j+1)%n || j==n-1) {
			if (arr[j]) {
				if (flag!=-1) {
					Sign(n, flag);
					Sign(n, j);
					return false;
				}
				flag = j;
			}
			j += (n - 1);
		}
	}
	for (int i = 2*n-1; i <= n * (n - 1); i = i + n) {
		int flag = -1;
		int j = i;
		while (j < n * n) {
			if (arr[j]) {
				if (flag!=-1) {
					Sign(n, flag);
					Sign(n, j);
					return false;
				}
				flag = j;
			}
			j += (n - 1);
		}
	}
	return true;
}
//在控制台展现棋盘状态
void ShowChessboard_cmd(int n, bool* arr) {
	for (int i = 0; i < n*n; ++i) {
		if (i % n == 0) std::cout << std::endl;
		std::cout << arr[i] << " ";
	}
	std::cout << std::endl;
}
void solve_queen(int StartIndex, int n, bool* arr,std::vector<std::vector<bool>>& Ret) {

	if (!Check(n, arr)) {
		std::cout << "错误 "<<std::endl;
		return;
	}
	//剪枝
	if (StartIndex == n && Check(n, arr)) {
		std::vector<bool> v;
		for (int i = 0; i < n * n; ++i) {
			v.push_back(arr[i]);
		}
		Ret.push_back(v);
		return;
	}

	for (int i = StartIndex * n; i < StartIndex * n + n; ++i) {
		arr[i] = true;

		//展示状态
		cleardevice();
		BeginBatchDraw();
		ShowChessboard(n, arr);
		ShowChessboard_cmd(n, arr);
		Check(n, arr);
		FlushBatchDraw();
		//Sleep(1);

		solve_queen(StartIndex + 1, n, arr, Ret);
		arr[i] = false;
	}
}

void Show_Ret(std::vector<std::vector<bool>> Ret, int n) {
	int num = Ret.size();
	bool* arr = (bool*)malloc(sizeof(bool) * (n * n));
	int i = 0;
	for (std::vector<std::vector<bool>>::iterator it=Ret.begin();it<Ret.end();++it,++i) {
		std::cout << "一共有 " << num << " 种答案" << std::endl;
		std::cout << "这是第 " << i + 1 << " 个答案" << std::endl;
		std::cout << "任意键切换下一个" << std::endl;
		int j = 0;
		for (std::vector<bool>::iterator it_2 = (*it).begin(); it_2 < (*it).end(); ++it_2,++j) {
			arr[j] = *it_2;
		}
		cleardevice();
		BeginBatchDraw();
		ShowChessboard(n,arr);
		ShowChessboard_cmd(n, arr);
		FlushBatchDraw();
		system("pause");
	}
	system("pause");
}

int main() {
	initgraph(1000, 1000);
	bool* arr = nullptr;
	const int n = 8;
	initialChessboard(n, arr);
	std::vector<std::vector<bool>> Ret;
	solve_queen(0, n, arr, Ret);
	free(arr);
	Show_Ret(Ret, n);
	return 0;
}

运行图片:
红色代表出现错误的棋子
运行中:
运行图片
运行结果:
结果图片

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值