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.0127 sec