Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

EightQueenProblem/lasy0901: Difference between revisions

From ZeroWiki
imported>Unknown
No edit summary
 
(Repair batch-0002 pages from live compare)
 
Line 6: Line 6:
== 하나의 해만 구하는 프로그램 ==
== 하나의 해만 구하는 프로그램 ==
  #include <stdio.h>
  #include <stdio.h>
  int t[8],check(int),is_finished=0;
  int t[8],check(int),is_finished=0;
  void make(int);
  void make(int);
  int check(int pos)
  int check(int pos)
Line 13: Line 13:
  for (i=0;i<=pos;i++)
  for (i=0;i<=pos;i++)
  for (j=0;j<=pos;j++)
  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))
  if ((i!=j)&&(t[i]==t[j]||t[i]+i==t[j]+j||t[i]-i==t[j]-j))
  return 0;
  return 0;
  return 1;
  return 1;
Line 23: Line 23:
  if (pos<8)
  if (pos<8)
  {for (i=0;i<8;i++)
  {for (i=0;i<8;i++)
  {t[pos]=i;
  {t[pos]=i;
  if (check(pos)) make(pos+1);}}
  if (check(pos)) make(pos+1);}}
  else
  else
  {is_finished=1; // 하나의 해만 찾음 <- 이 부분이 없으면 여러가지 해 출력
  {is_finished=1; // 하나의 해만 찾음 <- 이 부분이 없으면 여러가지 해 출력
  for (i=0;i<8;i++)
  for (i=0;i<8;i++)
  printf("%3d",t[i]);}
  printf("%3d",t[i]);}
  }
  }
  int main(int argc, char* argv[])
  int main(int argc, char* argv[])
  {
  {
  make(0);
  make(0);
Line 39: Line 39:
  #include <stdio.h>
  #include <stdio.h>
  #include <conio.h>
  #include <conio.h>
  int t[8],check(int),count=0;
  int t[8],check(int),count=0;
  void make(int);
  void make(int);
  int check(int pos)
  int check(int pos)
Line 46: Line 46:
  for (i=0;i<=pos;i++)
  for (i=0;i<=pos;i++)
  for (j=0;j<=pos;j++)
  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))
  if ((i!=j)&&(t[i]==t[j]||t[i]+i==t[j]+j||t[i]-i==t[j]-j))
  return 0;
  return 0;
  return 1;
  return 1;
Line 57: Line 57:
  for (i=0;i<8;i++)
  for (i=0;i<8;i++)
  {
  {
  t[pos]=i;
  t[pos]=i;
  if (check(pos))
  if (check(pos))
  make(pos+1);
  make(pos+1);
Line 65: Line 65:
  {
  {
  count++;
  count++;
  printf("[%d] : ",count);
  printf("[%d] : ",count);
  for (i=0;i<8;i++)
  for (i=0;i<8;i++)
  printf("%3d",t[i]);
  printf("%3d",t[i]);
  printf("\n");
  printf("\n");
  }
  }
  }
  }
   
   
  int main(int argc, char* argv[])
  int main(int argc, char* argv[])
  {
  {
  make(0);
  make(0);
  return 0;
  return 0;
  }
  }

Latest revision as of 00:16, 27 March 2026

같은 대각선에 있다면 x좌표와 y좌표의 합이나 차가 같다는 사실을 이용해서 프로그래밍했습니다. 좀 어거지로 소스 코드 크기를 줄였습니다. -_-a

두번째 프로그램은 ... 이상하게 컴파일이 안되더군요.. 알고보니 #include <stdafx.h> 을 안 넣어서 (VC6.0) 낭패-_-a

하나의 해만 구하는 프로그램

#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;
}

모든 해를 구하는 프로그램

#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;
}