๊ฐ์ ๋๊ฐ์ ์ ์๋ค๋ฉด x์ขํ์ y์ขํ์ ํฉ์ด๋ ์ฐจ๊ฐ ๊ฐ๋ค๋ ์ฌ์ค์ ์ด์ฉํด์ ํ๋ก๊ทธ๋๋ฐํ์ต๋๋ค.
์ข ์ด๊ฑฐ์ง๋ก ์์ค ์ฝ๋ ํฌ๊ธฐ๋ฅผ ์ค์์ต๋๋ค. -_-a
์ข ์ด๊ฑฐ์ง๋ก ์์ค ์ฝ๋ ํฌ๊ธฐ๋ฅผ ์ค์์ต๋๋ค. -_-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;
}










