U E D R , A S I H C RSS

8queen/문원명

미완성입니다..중괄호 관리도 안된것 같고, 각 변수의 값 조정 변화에 오류가 있는 것 같다.

~cpp 
#include <iostream>
using namespace std;

void main()
{
	int board[8][8];
	int firstBoard[8][8];
	int y2nd, x2nd, setx = 0;
	int y3rd, same = 0;
	int impossible = 0;
	int y4th, x4th;
	int y5th, x5th;
	int y6th, x6th;
	int y7th, x7th;
	int findY, findX;
	int originX, firstFind,firstAnswer = 1;
	int endFind = 0, count = 0;
	for(int y1st = 0 ; y1st < 8 ; y1st++)
	{
		for(int x1st = 0 ; x1st < 8 ; x1st++)
		{
			board[x1st][y1st] = 0;
			firstBoard[x1st][y1st] = 0;
		}
	}
	
	do					//맨 처음답과 같을때까지 찾기 반복
	{
		impossible = 0;
		for(y2nd = 0 ; y2nd < 8 ; y2nd++)	//행단위		
		{
			setx = 0;
			firstFind = 1;
			do								//열바꿔가며 가능한 자리 찾기
			{
				same = 0;			
				x2nd = setx;
				if(firstFind == 1) 
				{
					originX = x2nd;
					firstFind = 0;
				}
				board[y2nd][x2nd] = 1;
				for(y3rd = 0 ; y3rd < 8 ; y3rd)			//같은 열에 있는지 찾기  //수직검색
				{
					if (board[y3rd][x2nd] == board[y2nd][x2nd]) same = 1; 
				}
				if (same == 0) //왼쪽위 대각선 검색
				{
					findY = y2nd - 1;
					findX = x2nd - 1;
					while( (findY != -1) && (findX != -1))
					{
						if(board[findY][findX] == 1) same = 1;
						findY -= 1;
						findX -= 1;
					}
				}
				if (same == 0) //왼쪽아래 대각선 검색
				{
					findY = y2nd + 1;
					findX = x2nd - 1;
					while( (findY != 8) && (findX != -1))
					{
						if(board[findY][findX] == 1) same = 1;
						findY += 1;
						findX -= 1;
					}
				}
				if (same == 0) //오른쪽위 대각선 검색
				{
					findY = y2nd - 1;
					findX = x2nd + 1;
					while( (findY != -1) && (findX != 8))
					{
						if(board[findY][findX] == 1) same = 1;
						findY -= 1;
						findX += 1;
					}
				}
				if (same == 0) //오른쪽아래 대각선 검색
				{
					findY = y2nd + 1;
					findX = x2nd + 1;
					while( (findY != 8) && (findX != 8))
					{
						if(board[findY][findX] == 1) same = 1;
						findY += 1;
						findX += 1;
					}
				}

				
				if (same == 1)				//공격가능한 것이 있으면 초기화후 열바꾸기
				{
					board[y2nd][x2nd] = 0;
					setx++;
				}
				if (setx == 8) setx = 0;

				if (originX == setx )	impossible = 1;				//모든열이 불가능하면 나오기	
				if (impossible == 1) break;
			} while (same == 1);
			if (impossible == 1) break;
		}
		
		if (firstAnswer == 0)	//처음 답과 같은지 조사	
		{
			for(y7th = 0 ; y7th < 8 ; y7th++)
			{
				for(x7th = 0 ; x7th < 8 ; x7th++)
				{
					if( firstBoard[y7th][x7th] != board[y7th][x7th]) count++; 
				}
				
			}
			
		}
		if (count == 0) endFind = 1; //종료조건
		else count = 0;
		
		if (impossible == 0)
		{
			if (firstAnswer == 1) //처음답 저장
			{
				for(y6th = 0 ; y6th < 8 ; y6th++)
				{
					for(x6th = 0 ; x6th < 8 ; x6th++)
					{
						firstBoard[y6th][x6th] = board[y6th][x6th];
					}
				
				}
				firstAnswer = 0;
			}
		}
		
		if(endFind == 0)
		{
			if (impossible == 0)
			{
				for(y4th = 0 ; y4th < 8 ; y4th++) //답 출력
				{
					for(x4th = 0 ; x4th < 8 ; x4th++)
					{
						cout<<"board[y3th][x4th]\t";
					}
					cout<<endl;
				}
				cout<<endl<<endl;
			}
			for(y5th = 0 ; y5th < 8 ; y5th++) //초기화
			{
				for(x5th = 0 ; x5th < 8 ; x5th++)
				{
					board[x5th][y5th] = 0;
				}
			}
		}
	}while(endFind == 0);
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:19
Processing time 0.0117 sec