U E D R , A S I H C RSS

Eight Queen Problem/김준엽

No older revisions available

No older revisions available



1번문제를 제대로 이해 못해서 실수로 모든 경우를 구해버렸습니다. -0-;;;

~cpp 
#include <iostream>

class ChessBoard
{
	char m_cells[8][8];

public:
	ChessBoard()
	{
		for (int i=0; i<8; ++i)
			for (int j=0; j<8; ++j)
				m_cells[i][j] = 0;
	}
	
	void applyQueen(int x, int y)
	{
		for (int i=0; i<8; ++i)
			for (int j=0; j<8; ++j)
			{
				if (i==x | j==y | (y-j)==(x-i) | (y-j)==-(x-i))
					m_cells[i][j] = -1;
			}

		m_cells[x][y] = 1;
	}
	
	bool isEmptyCell(int x, int y)
	{
		return (m_cells[x][y] == 0);
	}

	void display()
	{
		for (int i=0; i<8; ++i)
		{
			for (int j=0; j<8; ++j)
			{
				if (m_cells[i][j] == 1)
					std::cout << " O";
				else if (m_cells[i][j] == -1)
					std::cout << " X";
				else 
					std::cout << "  ";
			}
			std::cout << std::endl;
		}
		std::cout << std::endl;
		
	}
};

void applyCell(ChessBoard cboard, int x, int y);

void find8Queen(ChessBoard& cboard, int y)
{
	if (y >= 8)
	{
		cboard.display();
	 	return;
	}
	
	for (int x=0; x<8; ++x)
		if ( cboard.isEmptyCell(x, y) )
			applyCell(cboard, x, y);
	
}

void applyCell(ChessBoard cboard, int x, int y)
{
	cboard.applyQueen(x, y);
	find8Queen(cboard, y+1);
}

int main()
{
	ChessBoard cboard;

	find8Queen(cboard, 0);
	
	return 0;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:11
Processing time 0.0180 sec