U E D R , A S I H C RSS

Eight Queen Problem/정수민



-_-;;

C++ -_-; 해 포함 =0=

96 + 21 + 15 -_-!

;;

- ;; 행하 -_-;; ................. . ㅠㅠ

EightQueenProblem.h

~cpp
#ifndef EIGHTQUEENPROBLEM_H_
#define EIGHTQUEENPROBLEM_H_

class EightQueenProblem
{
private:
    int chess[8][8];			//판
	int x,y;					
	bool ContraryQueen();		// 
	bool SearchMap();			// 0()  
	void SketchMap();			// 1()2() 
	void reset();				// 0   화

public:
	EightQueenProblem();		
	void MakeQueen();			//  
	void PrintMap();			// 

};

#endif

EightQueenProblem.cpp

~cpp
//////////////////////////////////////////////////////////////////////////
//																		//
//	EightQueenProblem   						//
//	 2006/04/17 7:17 ~ 2006/04/17 10:24								//
//	  2006/04/07										//
//	 : 														//
//																		/
////////////////////////////////////////////////////////////////////////


#include <iostream>

using namespace std;

#include "EightQueenProblem.h"


EightQueenProblem::EightQueenProblem () {
	reset ();

}

void EightQueenProblem::MakeQueen () {
	x=rand()%8;
	y=rand()%8;
	while ( 1 ) {
		if ( ContraryQueen () == 0 ) {		//0  1 
			break;
		}
		reset ();
	}

}

bool EightQueenProblem::ContraryQueen () {
	for (int i = 0 ; i < 8 ; i++ ) {
		if ( SearchMap () == 1 ) {			//   
			return 1;
		}
		while ( 1 ) {
			x = rand()%8;
			y = rand()%8;
			if ( chess[x][y] == 0 ) {
				break;
			}
		}
		chess[x][y] = 2;					//   .
		SketchMap ();
	}
	return 0;

}

void EightQueenProblem::SketchMap () {
	int i,j;
	for (i = 0 ; i < 8 ; i++ ) {
		if (chess[x][i] != 2) {
			chess[x][i] = 1;
		}
		if (chess[i][y] != 2) {
			chess[i][y] = 1;
		}
	}
	for (i = 0 ; i < 8 ; i++ ) {
		for (j = 0 ; j < 8 ; j++ ) {
			if ( (x-y) == (i-j) && chess[i][j] != 2) {
				chess[i][j] = 1;
			}
			if ( (x+y) == (i+j) && chess[i][j] != 2) {
				chess[i][j] = 1;
			}
		}
	}

}

bool EightQueenProblem::SearchMap () {
	for (int i = 0 ; i < 8 ; i++ ) {
		for (int j = 0 ; j < 8 ; j++ ) {
			if ( chess[i][j] == 0 ) {
				return 0;
			}
		}
	}
	return 1;

}

void EightQueenProblem::reset () {
	for (int i = 0 ; i < 8 ; i++ ) {
		for (int j = 0 ; j < 8 ; j++ ) {
			chess[i][j] = 0;
		}
	}

}

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

tast.cpp

~cpp
#include "EightQueenProblem.h"
#include <iostream>
#include <time.h>


int main()
{
	srand((unsigned)time(NULL)); 

    EightQueenProblem test;
	test.MakeQueen();
	test.PrintMap();
    return 0;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:12
Processing time 0.0106 sec