⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 8queen.h

📁 经典8皇后问题程序,在vc6.0运行通过
💻 H
字号:
#include<iostream>
using namespace std;

bool Check_Row(char ChessBoard[][8],int row,int col)			//检查行
{
	for(int i = 0;i<8;i++)
	{
		if(ChessBoard[row][i] != '*')
		{
			return false;
		}
	}
	return true;
}

bool Check_Col(char ChessBoard[][8],int row,int col)			//检查列
{
	for(int i = 0;i<8;i++)
	{
		if(ChessBoard[i][col] != '*')
		{
			return false;
		}
	}
	return true;
}

bool Check_bias(char ChessBoard[][8],int row,int col)		//检查对角线
{
	int i = row;
	int j = col;
	if(ChessBoard[i][j] != '*')
	{
		return false;
	}
	while((i>0)&&(j>0))
	{
		i--;
		j--;
		if(ChessBoard[i][j] != '*')
			return false;
	}

	i = row;
	j = col;
	while((i<7)&&(j>0))
	{
		i++;
		j--;
		if(ChessBoard[i][j] != '*')
			return false;
	}

	i = row;
	j = col;
	while((i<7)&&(j<7))
	{
		i++;
		j++;
		if(ChessBoard[i][j] != '*')
			return false;
	}
	
	i = row;
	j = col;
	while((i>0)&&(j<7))
	{
		i--;
		j++;
		if(ChessBoard[i][j] != '*')
			return false;
	}
	return true;
}

void output(char chess[][8])
{
	for(int i = 0;i<8;i++)
	{
		cout<<"("<<i<<")";
		for(int j = 0;j<8;j++)
		{
			cout<<chess[i][j]<<" ";
		}
		cout<<endl;
	}
}


void Queen(char Chess_Board[][8],int row,int col,int &x)
{
	if(row == 8)
	{
		cout<<"< "<<x<<" >"<<"**************************************"<<endl;
		output(Chess_Board);
		x++;
	}
	else
	{	
		int a,b;
		a = row;
		b = col;
		if(Check_Row(Chess_Board,a,b)&&Check_Col(Chess_Board,a,b)&&Check_bias(Chess_Board,a,b))
		{
			Chess_Board[a][b] = '@';
			Queen(Chess_Board,a+1,0,x);
			Chess_Board[a][b] = '*';
			if(b<7)
			{
				Queen(Chess_Board,a,b+1,x);
			}
		}
		else
		{
			if(b<7)
			{
				Queen(Chess_Board,a,b+1,x);
			}
		}	
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -