面向对象思想中的八皇后问题

一直都是用深搜求八皇后问题,昨天面向对象老师讲了一下用面向对象思想来写,感到质疑,回来写了一下,感觉恶心。

这是人类的思想吗?


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<cmath>
#include<string>
using namespace std;
#define N 8
class Queen
{
	const int col;
	int row;
	bool testAttack(int testrow, int testcol)
	{
		if(testrow == row)return true;
		if(abs(testrow-row) == abs(testcol - col))return true;
		return false;
	}
public:
	Queen(int c, int r, Queen *n):col(c), row(r), neighbor(n){};
	void show()
	{
		printf("row = %d col = %d\n", row, col);
		if(neighbor) neighbor->show();
	}
	Queen *neighbor;
	bool findsolution();
	bool canAttack(int , int);
	bool advance();
};

bool Queen::findsolution()
{
	while(neighbor && neighbor->canAttack(row, col))
	{
		if(!advance()) 
			return false;
	}
	return true;
}

bool Queen::advance()
{
	while(row < N)
	{
		row ++;
		return findsolution();
	}
	if(neighbor && !neighbor->advance())
		return false;
	if(row == N && col == 1)return false;
	row = 1;
	return findsolution();
}

bool Queen::canAttack(int testrow, int testcol)
{
	if(testAttack(testrow, testcol))
		return true;
	return neighbor && neighbor->canAttack(testrow, testcol);
}

int main()
{
	Queen *lastqueen = 0;
	for(int i = 1; i <= N; i++)
	{
		lastqueen = new Queen(i, 1, lastqueen);
		if(!lastqueen->findsolution())printf("nosolution\n");
		else if(i == N)
		{
			lastqueen->show();
			while(lastqueen->advance())lastqueen->show();
		}
	}
	return 0;
}



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值