c로 미로찾기구현중인데 난수생성이 잘 안되는것같네요 뭐가 문제일까요?

gong0의 이미지

요점은 미로를 탐색할때 탐색방향을 랜덤으로 설정하고싶은데 계속 같은 길만 나오네요ㅠㅠ
srand((unsigned int)time(NULL));도 사용했는데 뭐가 문제일까요?

#include <stdio.h>
#include<time.h>
#include<stdlib.h>
 
#define numRow 10
#define numCol 10
#define MAX_STACK_SIZE 100		//스택의 최대 크기
#define TRUE 1		//TRUE의 초기화
#define FALSE 0		//FALSE의 초기화
#define MARK 2		//가본 길 기록
#define EXIT_ROW 8
#define EXIT_COL 7
short int maze[numRow + 2][numCol + 2];
int top = -1;
 
typedef struct {
	short int vert;
	short int horiz;
}offsets;
 
offsets move[8] = { { -1, 0 },        
	{ -1,1 },
	{ 0,1 },
	{ 1,1 },       
	{ 1,0 },
	{ 1,-1 },
	{ 0,-1 },
	{ -1,-1 }        
};
 
typedef struct {                        
	short int row;
	short int col;
//	short int dir;
}element;
element stack[MAX_STACK_SIZE];		//지나온 경로와 방향을 저장할 stack
 
 
void setmaze();
void path();
void push(element);
element pop();
 
void main() {
	setmaze();
	for (int i = 0; i < numRow + 2; i++) {
		for (int j = 0; j < numCol + 2; j++)
			printf("%2d", maze[i][j]);
		printf("\n");
 
 
	}
 
	path();
	for (int i = 0; i < numRow + 2; i++) {
		for (int j = 0; j < numCol + 2; j++)
			printf("%2d", maze[i][j]);
		printf("\n");
 
	}
 
}
void setmaze() {
	short int maze0[numRow][numCol] = { 
		{ 0,0,1,1,1,0,1,0,1,0 },
		{ 1,0,0,1,1,1,0,1,0,1 },
		{ 1,1,0,1,1,0,1,0,1,1 },
		{ 0,0,1,1,0,0,1,0,0,0 },
		{ 1,1,0,1,1,0,1,0,1,0 },
		{ 1,0,1,1,0,1,1,0,0,1 },
		{ 1,1,0,1,0,1,0,0,1,1 },
		{ 1,0,1,0,1,0,1,0,0,0 },
		{ 0,1,0,1,1,1,0,1,1,0 },
		{ 1,0,0,1,1,1,0,0,0,0 } 
	};
 
 
	for (int i = 0; i < numCol + 2; i++) {
		maze[0][i]=	maze[numRow + 1][i] = maze[i][0] = maze[i][numCol + 1] = 1;
	}
	for (int i = 1;i < numRow + 1 ; i++) {
		for (int j = 1; j < numCol + 1; j++) {
			maze[i][j] = maze0[i - 1][j - 1];
		}
	}
 
 
}
void path() {      //미로탐색
	int row, col, nextRow, nextCol, dir, found = FALSE; element position;
	srand((unsigned int)time(NULL));
	maze[1][1] = MARK; top = 0;
	stack[0].row = 1;
	stack[0].col = 1;
	stack[0].dir = rand() % 8;
	while (top > -1 && !found) {
		position = pop();
		row = position.row;
		col = position.col;
		dir = rand() % 8;
		while (dir < 8 && !found) {
			nextRow = row + move[dir].vert;
			nextCol = col + move[dir].horiz;
			if (!maze[nextRow][nextCol] && (EXIT_ROW - 1 <= nextRow &&  nextRow <= EXIT_ROW + 1 ) && (EXIT_COL - 1 <= nextCol && nextCol <= EXIT_COL + 1))
				found = TRUE;
			else if (maze[nextRow][nextCol] == FALSE) {
				maze[nextRow][nextCol] = MARK;
				position.row = row;
				position.col = col;
				//position.dir = ++dir;
				push(position);
				row = nextRow;
				col = nextCol;
 
			}
 
			else dir=rand()%8;
		}
	}
	if (found) {
		printf("The path is:\n\n");
		printf("row  col\n");
		for (int i = 0; i <= top; i++)
			printf("%2d%5d\n", stack[i].row, stack[i].col);
		printf("%2d%5d\n", row, col);
		printf("%2d%5d\n", nextRow, nextCol);
		printf("%2d%5d\n", EXIT_ROW, EXIT_COL);
	}
	else printf("The maze does not have a path.\n");
}
void push(element it) {
	if (top >= MAX_STACK_SIZE - 1)
		printf("stack if Full");
	stack[++top] = it;
}
element pop() {
	if (top == -1)
		printf("stack is Empty");
	return stack[top--];
}
shint의 이미지


TimeOut 나길래 확인해보니.
길이 막혀서 그런거 같습니다.??
count갯수를 초과하면. 통과하도록 변경했습니다.

rand()값은 잘 적용되는것 같습니다.

http://codepad.org/Sz1BrF8T

#include <stdio.h>
#include<time.h>
#include<stdlib.h>
 
#define numRow 10
#define numCol 10
#define MAX_STACK_SIZE 100		//스택의 최대 크기
#define TRUE 1		//TRUE의 초기화
#define FALSE 0		//FALSE의 초기화
#define MARK 2		//가본 길 기록
#define EXIT_ROW 8
#define EXIT_COL 7
short int maze[numRow + 2][numCol + 2];
int top = -1;
 
typedef struct {
	short int vert;
	short int horiz;
}offsets;
 
offsets move[8] = { { -1, 0 },        
	{ -1,1 },
	{ 0,1 },
	{ 1,1 },       
	{ 1,0 },
	{ 1,-1 },
	{ 0,-1 },
	{ -1,-1 }        
};
 
typedef struct {                        
	short int row;
	short int col;
	short int dir;
}element1;
element1 stack1[MAX_STACK_SIZE];		//지나온 경로와 방향을 저장할 stack
 
 
void setmaze();
void path();
void push(element1);
element1 pop();
 
int main() {
	setmaze();
	for (int i = 0; i < numRow + 2; i++) {
		for (int j = 0; j < numCol + 2; j++)
			printf("%2d", maze[i][j]);
		printf("\n"); 
	}
 
	path();
 
	for (int i = 0; i < numRow + 2; i++) {
		for (int j = 0; j < numCol + 2; j++)
			printf("%2d", maze[i][j]);
		printf("\n");
 
	}
	return 0;
}
void setmaze() {
	short int maze0[numRow][numCol] = { 
		{ 0,0,1,1,1,0,1,0,1,0 },
		{ 1,0,0,1,1,1,0,1,0,1 },
		{ 1,1,0,1,1,0,1,0,1,1 },
		{ 0,0,1,1,0,0,1,0,0,0 },
		{ 1,1,0,1,1,0,1,0,1,0 },
		{ 1,0,1,1,0,1,1,0,0,1 },
		{ 1,1,0,1,0,1,0,0,1,1 },
		{ 1,0,1,0,1,0,1,0,0,0 },
		{ 0,1,0,1,1,1,0,1,1,0 },
		{ 1,0,0,1,1,1,0,0,0,0 } 
	};
 
 
	for (int i = 0; i < numCol + 2; i++) {
		maze[0][i]=	maze[numRow + 1][i] = maze[i][0] = maze[i][numCol + 1] = 1;
	}
	for (int i = 1;i < numRow + 1 ; i++) {
		for (int j = 1; j < numCol + 1; j++) {
			maze[i][j] = maze0[i - 1][j - 1];
		}
	}
 
 
}
void path() {      //미로탐색
	int row = 0;
        int col = 0;
        int nextRow = 0;
        int nextCol = 0;
        int dir = 0;
        int found = FALSE;
        element1 position;
 
	srand((unsigned int)time(NULL));
	maze[1][1] = MARK; top = 0;
	stack1[0].row = 1;
	stack1[0].col = 1;
	stack1[0].dir = rand() % 8;
 
 
        int count = 0;
 
	while (top > -1 && !found) 
        {
		position = pop();
 
		row = position.row;
		col = position.col;
		dir = rand() % 8;
		while (dir < 8 && !found) 
                {
			nextRow = row + move[dir].vert;
			nextCol = col + move[dir].horiz;
			if (!maze[nextRow][nextCol] && (EXIT_ROW - 1 <= nextRow &&  nextRow <= EXIT_ROW + 1 ) && (EXIT_COL - 1 <= nextCol && nextCol <= EXIT_COL + 1))
                        {
				found = TRUE;
                        }
			else if (maze[nextRow][nextCol] == FALSE) 
                        {
				maze[nextRow][nextCol] = MARK;
				position.row = row;
				position.col = col;
				//position.dir = ++dir;
 
				push(position);
 
				row = nextRow;
				col = nextCol;
 			}
 			else
                        {
                            dir = rand()%8;
printf("%d ", rand() % 8);
 
                        }
 
                    if(count >= 100)
                    {
                        found = TRUE;
                        count = 0;
                    }
                    count++;
		}
	}
 
	if (found) {
		printf("The path is:\n\n");
		printf("row  col\n");
		for (int i = 0; i <= top; i++)
			printf("%2d%5d\n", stack1[i].row, stack1[i].col);
		printf("%2d%5d\n", row, col);
		printf("%2d%5d\n", nextRow, nextCol);
		printf("%2d%5d\n", EXIT_ROW, EXIT_COL);
	}
	else printf("The maze does not have a path.\n");
}
void push(element1 it) {
	if (top >= MAX_STACK_SIZE - 1)
		printf("stack if Full");
	stack1[++top] = it;
}
element1 pop() {
	if (top == -1)
		printf("stack is Empty");
	return stack1[top--];
}

//출력 결과
1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 1 1 1 0 1 0 1 0 1
1 1 0 0 1 1 1 0 1 0 1 1
1 1 1 0 1 1 0 1 0 1 1 1
1 0 0 1 1 0 0 1 0 0 0 1
1 1 1 0 1 1 0 1 0 1 0 1
1 1 0 1 1 0 1 1 0 0 1 1
1 1 1 0 1 0 1 0 0 1 1 1
1 1 0 1 0 1 0 1 0 0 0 1
1 0 1 0 1 1 1 0 1 1 0 1
1 1 0 0 1 1 1 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1
2 3 6 5 7 1 4 1 4 1 4 4 0 3 1 1 0 7 4 4 4 4 2 5 7 7 6 5 2 3 6 6 0 4 1 1 6 6 2 0 2 5 2 6 2 7 6 7 3 3 5 2 1 4 4 6 3 6 6 2 5 1 0 2 5 0 2 5 3 7 6 5 1 0 7 7 4 0 2 0 0 1 3 5 0 6 2 1 7 2 3 4 3 4 7 4 5 The path is:

row col
1 1
2 2
3 3
2 3
1 2
2 2
8 7
1 1 1 1 1 1 1 1 1 1 1 1
1 2 2 1 1 1 0 1 0 1 0 1
1 1 2 2 1 1 1 0 1 0 1 1
1 1 1 2 1 1 0 1 0 1 1 1
1 0 0 1 1 0 0 1 0 0 0 1
1 1 1 0 1 1 0 1 0 1 0 1
1 1 0 1 1 0 1 1 0 0 1 1
1 1 1 0 1 0 1 0 0 1 1 1
1 1 0 1 0 1 0 1 0 0 0 1
1 0 1 0 1 1 1 0 1 1 0 1
1 1 0 0 1 1 1 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

gong0의 이미지

아 길이 막힌거였군요ㅠㅠ!!!감사합니다!

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.