나름대로 열심히 풀어보았으나
��처럼 깔�하게 나오진 않는군요 -_-;;
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;
}










