U E D R , A S I H C RSS

Eight Queen Problem/lasy0901

๊ฐ™์€ ๋Œ€๊ฐ์„ ์— ์žˆ๋‹ค๋ฉด x์ขŒํ‘œ์™€ y์ขŒํ‘œ์˜ ํ•ฉ์ด๋‚˜ ์ฐจ๊ฐ€ ๊ฐ™๋‹ค๋Š” ์‚ฌ์‹ค์„ ์ด์šฉํ•ด์„œ ํ”„๋กœ๊ทธ๋ž˜๋ฐํ–ˆ์Šต๋‹ˆ๋‹ค.
์ข€ ์–ด๊ฑฐ์ง€๋กœ ์†Œ์Šค ์ฝ”๋“œ ํฌ๊ธฐ๋ฅผ ์ค„์˜€์Šต๋‹ˆ๋‹ค. -_-a

๋‘๋ฒˆ์งธ ํ”„๋กœ๊ทธ๋žจ์€ ... ์ด์ƒํ•˜๊ฒŒ ์ปดํŒŒ์ผ์ด ์•ˆ๋˜๋”๊ตฐ์š”.. ์•Œ๊ณ ๋ณด๋‹ˆ #include <stdafx.h> ์„ ์•ˆ ๋„ฃ์–ด์„œ (VC6.0) ๋‚ญํŒจ-_-a

ํ•˜๋‚˜์˜ ํ•ด๋งŒ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ

~cpp 
#include <stdio.h>
int t[8],check(int),is_finished=0;
void make(int);
int check(int pos)
{
	int i,j;
	for (i=0;i<=pos;i++)
		for (j=0;j<=pos;j++)
			if ((i!=j)&&(t[i]==t[j]||t[i]+i==t[j]+j||t[i]-i==t[j]-j))
				return 0;
	return 1;
}
void make(int pos)
{
	int i;
	if (is_finished==0)
		if (pos<8)
		{for (i=0;i<8;i++)
				{t[pos]=i;
				if (check(pos)) make(pos+1);}}
		else
			{is_finished=1; // ํ•˜๋‚˜์˜ ํ•ด๋งŒ ์ฐพ์Œ <- ์ด ๋ถ€๋ถ„์ด ์—†์œผ๋ฉด ์—ฌ๋Ÿฌ๊ฐ€์ง€ ํ•ด ์ถœ๋ ฅ
			for (i=0;i<8;i++)
				printf("%3d",t[i]);}
}
int main(int argc, char* argv[])
{
	make(0);
	return 0;
}

๋ชจ๋“  ํ•ด๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ

~cpp 
#include <stdio.h>
#include <conio.h>
int t[8],check(int),count=0;
void make(int);
int check(int pos)
{
	int i,j;
	for (i=0;i<=pos;i++)
		for (j=0;j<=pos;j++)
			if ((i!=j)&&(t[i]==t[j]||t[i]+i==t[j]+j||t[i]-i==t[j]-j))
				return 0;
	return 1;
}
void make(int pos)
{
	int i;
	if (pos<8)
	{
		for (i=0;i<8;i++)
		{
			t[pos]=i;
			if (check(pos))
				make(pos+1);
		}
	}
	else
	{
		count++;
		printf("[%d] : ",count);
		for (i=0;i<8;i++)
			printf("%3d",t[i]);
		printf("\n");
	}
}

int main(int argc, char* argv[])
{
	make(0);
	return 0;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:12
Processing time 0.0069 sec