个人理解八皇后问题

54 篇文章 0 订阅
20 篇文章 0 订阅

程序源文件下载地址在文章后面。

---------------------------------------------------------------------

八皇后问题,网上的解法很多,在此我说一下自己的

理解,权当是学习笔记了。

注:四皇后解法总共有2种,八皇后解法总共有92种,16皇后有很多种解法,我运行程序几分钟结果都没出来。

-------------------------------------------------------------------------------------------------------------------------------------------------

解题思路:跟穷举查不多,我们依次在每一行放一个皇后,而一行有八列,所以我们在放置一个皇后时,就要

马上判断皇后放置的列是否符合条件,如果符合,我们就放置下一个皇后,如果不符合,我们就尝试着将皇后

放置在下一列,接着像刚才一样判断皇后放置的列是否合法...如此循环,直到最后一列为止。

------------------------------------------------------------------------------------------------------------------------------------------------

因为有八列,所以每行皇后的位置就有八种可能,我们在程序中用一个循环来控制即可。

------------------------------------------------------------------------------------------------------------------------------------------------

//程序源码

#include <iostream>
#include <Windows.h>
using namespace std;

#define  N 8	   //这是解的是8皇后问题,如果要解4、16皇后等,只须将8改掉即可,方便吧。
int queen[N]={0}; //数组queen[N]的下标表示皇后所在的行,queen[N]其值表示皇后所在的列

//判断皇后是否能够放在第row行
bool CanPlace(int row)
{
		for(int j=0;j<row;j++)//将第row行皇后与前面所有行的皇后进行比较
		{
			if (queen[row]==queen[j]||row+queen[row]==j+queen[j]||row-queen[row]==j-queen[j])
				return false;//不能放皇后
		}
		return true; //可以放皇后

}
//输出皇后的位置
void PrintfQueen()
{
	for(int i=0;i<N;i++)
	{
		for (int j=0;j<N;j++)
		{
			if(queen[i]==j)
				cout<<"■ ";//输出皇后
			else
				cout<<"□ ";
		}
		cout<<endl;
	}
	cout<<endl<<endl;
}

int Sum=0;
void Queen(int row)
{
	if (row>=N)//皇后已经全部放完时
	{
		PrintfQueen();//输出皇后的位置
		Sum++;
		return;
	} 
	else
	{
		for (int i=0;i<N;i++) //总共有N列,一列一列的试探放置皇后
		{
			queen[row]=i; //将第row行皇后放在第i列上面

			if(CanPlace(row))//判断皇后的位置是否正确
				Queen(row+1);  //放下一个皇后
		}
	}
}


void main(void)
{
	Queen(0);
	cout<<"总的摆法,Sum="<<Sum;
	cin.get();
}


 

--------------------------------------------------------------------------------------------------------------------------------------------------

//效果截图

 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

源码下载地址:八皇后问题解法.zip    //免积分

备份地址:八皇后问题解法.zip     //需积分

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
C++面向对象编程八皇后问题 BOOL CMyqueenApp::InitInstance() { AfxEnableControlContainer(); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. #ifdef _AFXDLL Enable3dControls(); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic(); // Call this when linking to MFC statically #endif // Change the registry key under which our settings are stored. // TODO: You should modify this string to be something appropriate // such as the name of your company or organization. SetRegistryKey(_T("Local AppWizard-Generated Applications")); LoadStdProfileSettings(); // Load standard INI file options (including MRU) // Register the application's document templates. Document templates // serve as the connection between documents, frame windows and views. CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CMyqueenDoc), RUNTIME_CLASS(CMainFrame), // main SDI frame window RUNTIME_CLASS(CMyqueenView)); AddDocTemplate(pDocTemplate); // Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line if (!ProcessShellCommand(cmdInfo)) return FALSE; VERIFY( 1 == InitSkinMagicLib( AfxGetInstanceHandle(), "Demo" , NULL, NULL ) ); VERIFY( 1 == LoadSkinFromResource( AfxGetInstanceHandle() , "DEFAULT" ,"DEFAULT") ); VERIFY( 1 == SetWindowSkin( m_pMainWnd->m_hWnd , "MainFrame" )); VERIFY( 1 == SetDialogSkin( "Dialog" ) ); //((CMainFrame*)m_pMainWnd)->m_bSkinned = TRUE; //((CMainFrame*)m_pMainWnd)->m_nIndex = 0; m_pMainWnd->SetWindowText("八皇后问题演示"); // The one and only window has been initialized, so show and update it. m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow();

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

friendan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值