应用C++面向对象思维解K皇后问题

应用C++面向对象思维解K皇后问题

题目描述

小Z最近捡到了一个棋盘,他想在棋盘上摆放K个皇后。他想知道在他摆完这K个皇后之后,棋盘上还有多少了格子是不会被攻击到的。
(Ps:一个皇后会攻击到这个皇后所在的那一行,那一列,以及两条对角线)

在这里插入图片描述

两个类

1.棋盘格子类

class cell {
public:
	//构造函数
	cell() {
		isH = false; //没有皇后
		isDanger = false; //不是危险区
	}
	//析构函数
	virtual ~cell() {}
	//设置函数(1:放置皇后 2:设置危险区)
	void setCell(int _n) { _n == 1 ? isH = true : isDanger = true; }
	//获取函数
	char getCell() {
		if (isH) return 'H'; //如果有皇后,返回“H”
		else if (isDanger) return 'x'; //如果危险,返回“x”
		else return '-'; //否则返回“-”,表示安全区
	}
	//输出格子
	void printCell() {
		if (isH) cout << "H";
		else if (isDanger) cout << "x";
		else cout << "-";
	}
private:
	bool isH; //格子上是否有皇后
	bool isDanger; //是否危险区(皇后攻击范围)
};

2.棋盘类

class Chessboard {
public:
	Chessboard(int _row, int _line) {
		row = _row; line = _line;
		cell_null = row * line;
		cellArr = new cell*[row]; //new一个二维数组,表示棋盘中所有格子
		for (int i = 0; i < row; i++) cellArr[i] = new cell[line];
	}
	virtual ~Chessboard() {
		for (int i = 0; i < row; i++) {
			delete[]cellArr[i]; cellArr[i] = NULL;
		}
	}
	//放置皇后(_row:行坐标 _line:列坐标)
	bool inputK(int _row, int _line) {
		if (cell_null == 0) return false; //如果空格数为0,则放置皇后失败
		else {
			cell_null--; //空格数-1
			//危险区设置:行列方向
			for (int i = 0; i < row; i++) cellArr[i][_line - 1].setCell(2); //皇后所在行设为危险区
			for (int i = 0; i < line; i++) cellArr[_row - 1][i].setCell(2); //皇后所在列设为危险区
			//危险区设置:对角线方向
			for (int r = _row - 2, l = _line - 2; r >= 0 && l >= 0; r--, l--) cellArr[r][l].setCell(2); //左上
			for (int r = _row, l = _line - 2; r < row && l >= 0; r++, l--) cellArr[r][l].setCell(2); //左下
			for (int r = _row - 2, l = _line; r >= 0 && l < line; r--, l++) cellArr[r][l].setCell(2); //右上
			for (int r = _row, l = _line; r < row && l < line; r++, l++) cellArr[r][l].setCell(2); //右下
			//放置皇后
			cellArr[_row - 1][_line - 1].setCell(1);
			return true;
		}
	}
	//返回安全格子的个数
	int getSafe() {
		int safe = 0;
		//遍历所有格子
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < line; j++) {
				if (cellArr[i][j].getCell() == '-') safe++;
			}
		}
		return safe;
	}
	//打印棋盘所有格子
	void printChessboard() {
		cout << "棋盘(H:皇后,x:危险区,-:安全区):" << endl;
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < line; j++) {
				cellArr[i][j].printCell(); cout << " ";
			}
			cout << endl;
		}
	}
private:
	int row, //棋盘行数
		line, //棋盘列数
		cell_null, //表示还能放棋子的格子个数(空格个数)
		cell_danger; //危险格子格子
	cell **cellArr; //格子二维数组
};

主函数

int main() {
	int row, line, k;
	cout << "棋盘规格(行,列):"; cin >> row >> line; cout << endl;
	cout << "皇后数量:"; cin >> k; cout << endl;
	//建立棋盘
	Chessboard c(row, line);
	
	int temp_row, temp_line;
	for (int i = 0; i < k; i++) {
		cout << "皇后" << i + 1 << "位置:";
		cin >> temp_row >> temp_line;
		c.inputK(temp_row, temp_line);
	}
	cout << endl;

	c.printChessboard();
	cout << endl;
	cout << "SageNum:" << c.getSafe() << endl;
	system("pause");
	return 0;
}

完整代码

#include <iostream>
using namespace std;

class cell {
public:
	//构造函数
	cell() {
		isH = false; //没有皇后
		isDanger = false; //不是危险区
	}
	//析构函数
	virtual ~cell() {}
	//设置函数(1:放置皇后 2:设置危险区)
	void setCell(int _n) { _n == 1 ? isH = true : isDanger = true; }
	//获取函数
	char getCell() {
		if (isH) return 'H'; //如果有皇后,返回“H”
		else if (isDanger) return 'x'; //如果危险,返回“x”
		else return '-'; //否则返回“-”,表示安全区
	}
	//输出格子
	void printCell() {
		if (isH) cout << "H";
		else if (isDanger) cout << "x";
		else cout << "-";
	}
private:
	bool isH; //格子上是否有皇后
	bool isDanger; //是否危险区(皇后攻击范围)
};

class Chessboard {
public:
	Chessboard(int _row, int _line) {
		row = _row; line = _line;
		cell_null = row * line;
		cellArr = new cell*[row]; //new一个二维数组,表示棋盘中所有格子
		for (int i = 0; i < row; i++) cellArr[i] = new cell[line];
	}
	virtual ~Chessboard() {
		for (int i = 0; i < row; i++) {
			delete[]cellArr[i]; cellArr[i] = NULL;
		}
	}
	//放置皇后(_row:行坐标 _line:列坐标)
	bool inputK(int _row, int _line) {
		if (cell_null == 0) return false; //如果空格数为0,则放置皇后失败
		else {
			cell_null--; //空格数-1
			//危险区设置:行列方向
			for (int i = 0; i < row; i++) cellArr[i][_line - 1].setCell(2); //皇后所在行设为危险区
			for (int i = 0; i < line; i++) cellArr[_row - 1][i].setCell(2); //皇后所在列设为危险区
			//危险区设置:对角线方向
			for (int r = _row - 2, l = _line - 2; r >= 0 && l >= 0; r--, l--) cellArr[r][l].setCell(2); //左上
			for (int r = _row, l = _line - 2; r < row && l >= 0; r++, l--) cellArr[r][l].setCell(2); //左下
			for (int r = _row - 2, l = _line; r >= 0 && l < line; r--, l++) cellArr[r][l].setCell(2); //右上
			for (int r = _row, l = _line; r < row && l < line; r++, l++) cellArr[r][l].setCell(2); //右下
			//放置皇后
			cellArr[_row - 1][_line - 1].setCell(1);
			return true;
		}
	}
	//返回安全格子的个数
	int getSafe() {
		int safe = 0;
		//遍历所有格子
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < line; j++) {
				if (cellArr[i][j].getCell() == '-') safe++;
			}
		}
		return safe;
	}
	//打印棋盘所有格子
	void printChessboard() {
		cout << "棋盘(H:皇后,x:危险区,-:安全区):" << endl;
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < line; j++) {
				cellArr[i][j].printCell(); cout << " ";
			}
			cout << endl;
		}
	}
private:
	int row, //棋盘行数
		line, //棋盘列数
		cell_null, //表示还能放棋子的格子个数(空格个数)
		cell_danger; //危险格子格子
	cell **cellArr; //格子二维数组
};

int main() {
	int row, line, k;
	cout << "棋盘规格(行,列):"; cin >> row >> line; cout << endl;
	cout << "皇后数量:"; cin >> k; cout << endl;
	//建立棋盘
	Chessboard c(row, line);

	int temp_row, temp_line;
	for (int i = 0; i < k; i++) {
		cout << "皇后" << i + 1 << "位置:";
		cin >> temp_row >> temp_line;
		c.inputK(temp_row, temp_line);
	}
	cout << endl;

	c.printChessboard();
	cout << endl;
	cout << "SageNum:" << c.getSafe() << endl;
	system("pause");
	return 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();
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值