몬테카를로 n-queen문제좀 도와주세요 ㅠ

123abab의 이미지

#include
#include
#include
#include

int n;
int *queen;
FILE *fn, *ft;

int promising(int m)
{
int i=0, promising;
promising = 1;
while(i if(abs( queen[i]-queen[m]) == abs(i-m) )
promising = 0;
if(queen[i]==queen[m])
promising = 0;
i++;
}
return promising;
}

void fillRnd(int m)
{
int i;
srand((unsigned)time(NULL));

for(i = 0; i < m; i++) {
queen[i] = rand()%n;
while(promising(i)==0)
queen[i] = rand()%n;
}
}

int promisingRnd(int m)
{
int i, j;

for(i=0; i {
for(j=i+1; j {
if(abs( queen[i]-queen[j]) == abs(i-j) )
return 0;
if(queen[i]==queen[j])
return 0;
}
}

return 1;
}

void queens(int i)
{
int j;

if(promising(i)) {
if(i==n-1) {
for(j=0; j fprintf(ft, "%d\n", queen[j]);
exit(-1);
}

else {
for(j=0; j queen[i+1] = j;
queens(i+1);
}
}
}
}

void main()
{
int i, j;
char input[100];

printf("input file name? ");
scanf("%s", input);

fn = fopen(input, "r");
ft = fopen("output.txt", "w");

fscanf(fn, "%d", &n);

queen = (int *)malloc (sizeof(int)*(n));

while(1) {
fillRnd(n-5);
//if(promisingRnd(n-5)==1)
queens(n-6);
}
}

정도로 코드 짜봤는데.. 자꾸 무한루프도네여

밑에서 fillRnd하면서 겹치는지 안겹치는지 확인해준게 문젠가..
promisingRnd는 만약 그냥 fillRnd해서 채워넣은다음 채워넣은것들을 확인하는 함수로 만들어봤습니다..
도와주세요..

123abab의 이미지

#include
#include
#include
#include

int n;
int *queen;
FILE *fn, *ft;

int promising(int m)
{
int i=0, promising;
promising = 1;
while(i if(abs( queen[i]-queen[m]) == abs(i-m) )
promising = 0;
if(queen[i]==queen[m])
promising = 0;
i++;
}
return promising;
}

void fillRnd(int m)
{
int i;
srand((unsigned)time(NULL));

for(i = 0; i < m; i++) {
queen[i] = rand()%n;
while(promising(i)==0)
queen[i] = rand()%n;
}
}

int promisingRnd(int m)
{
int i, j;

for(i=0; i {
for(j=i+1; j {
if(abs( queen[i]-queen[j]) == abs(i-j) )
return 0;
if(queen[i]==queen[j])
return 0;
}
}

return 1;
}

void queens(int i)
{
int j;

if(promising(i)) {
if(i==n-1) {
for(j=0; j fprintf(ft, "%d\n", queen[j]);
exit(-1);
}

else {
for(j=0; j queen[i+1] = j;
queens(i+1);
}
}
}
}

void main()
{
int i, j;
char input[100];

printf("input file name? ");
scanf("%s", input);

fn = fopen(input, "r");
ft = fopen("output.txt", "w");

fscanf(fn, "%d", &n);

queen = (int *)malloc (sizeof(int)*(n));

while(1) {
fillRnd(n-5);
//if(promisingRnd(n-5)==1)
queens(n-6);
}
}

lithium81의 이미지

< 와 > 사이에 code

#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char* argv[])
{
    return 0;
}

- 어제보다 나은 오늘, 오늘보다 나은 내일.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.