U E D R , A S I H C RSS

Eight Queen Problem/최태호소스


문제를 본 순간부터 시간� 체�..--;;

누구든 코멘트해� �관없다는..^^;;

~cpp 
//12:50
//1:25
# include <stdio.h>
# include <conio.h>
# define SIZE	8
char P[SIZE][SIZE];
char Q[SIZE];
int c;

int Check(int in)
{
	int re=0;
	for(int i=c;i>0;i--){
		if((in-i)>=0 && P[c-i][in-i] ==1){//왼쪽으로..
			return 0;
		}
		if( (in+i)<SIZE && P[c-i][in+i]==1 ){//오른쪽으로..
			return 0;
		}
	}
	if (i==0) return 1;
	return 0;
}

int EQ()
{
	int re;
	for(int i=0;i<SIZE;i++){
		if(Q[i]==1)	continue;// 그 줄� queen �있는것�므로 다�칸으로..
		//없으면..그�리� 놓아서 걸리는지 체�..
		if(Check(i)>0 ){//I � 놓�수 있�때..
			Q[i]=1;
			P[c][i]=1;
			c++;
			if(c==SIZE){//�때는 찾� 것임..
				return 1;
			}
			re=EQ();
			if (re==1) return 1;
			c--;
			Q[i]=0;
			P[c][i]=0;

		}

	}
	return 0;

}

void Init()
{
	for (int i=0;i<SIZE;i++){
		for(int j=0;j<SIZE;j++) P[i][j]=0;
		Q[i]=0;
	}
	c=0;
}

void RESULT()
{
	for(int i=0;i<SIZE;i++){
		for(int j=0;j<SIZE;j++)printf("%d ",P[i][j]);
		printf("\n");
	}
}
void main(void)
{
	Init();
	EQ();
	RESULT();
	getch();
	return;
}

아래것� �번째 문제��..
EQ() 함수�서 return 1 하는 대신 계� 풀�� 하고..
마지막ì—� ë�„ì°©í–ˆì�„때만 결과를 ìœë ¥í•˜ë�„ë¡� 수정하였다.
그리고 ë©”ì�¸í•¨ìˆ˜ì—�서 ê²°ê³¼ ìœë ¥í•˜ëŠ” 루틴ì�€ 제거 하였다.

~cpp 
//12:50
//1:25
# include <stdio.h>
# include <conio.h>
# define SIZE	8
char P[SIZE][SIZE];
char Q[SIZE];
int c;

void RESULT()
{
	static int count=0;
	count++;
	printf("solv %d\n",count);
	for(int i=0;i<SIZE;i++){
		for(int j=0;j<SIZE;j++)printf("%d ",P[i][j]);
		printf("\n");
	}
	printf("\n");
}

int Check(int in)
{
	int re=0;
	for(int i=c;i>0;i--){
		if((in-i)>=0 && P[c-i][in-i] ==1){//왼쪽으로..
			return 0;
		}
		if( (in+i)<SIZE && P[c-i][in+i]==1 ){//오른쪽으로..
			return 0;
		}
	}
	if (i==0) return 1;
	return 0;
}

int EQ()
{
	int re;
	for(int i=0;i<SIZE;i++){
		if(Q[i]==1)	continue;// 그 줄� queen �있는것�므로 다�칸으로..
		//없으면..그�리� 놓아서 걸리는지 체�..
		if(Check(i)>0 ){//I � 놓�수 있�때..
			Q[i]=1;
			P[c][i]=1;
			c++;
			if(c==SIZE){//�때는 찾� 것임..
//				return 1;
				RESULT();
			}
			re=EQ();
//			if (re==1) return 1;
			c--;
			Q[i]=0;
			P[c][i]=0;

		}

	}
	return 0;

}

void Init()
{
	for (int i=0;i<SIZE;i++){
		for(int j=0;j<SIZE;j++) P[i][j]=0;
		Q[i]=0;
	}
	c=0;
}

void main(void)
{
	Init();
	EQ();
//	RESULT();
	getch();
	return;
}

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:13
Processing time 0.1906 sec