문제는 디버깅하며 한라인 한라인 실행하면 문제가 없는데...
그냥 런하면 첫번째 돌고 에러난다... 왜그런건지 아는 프갤러 있음?
아래는 소스
/*
* 파일명 : Robot.c
* 작성일 : 2013.01.21.MON.
* 작성자 : M
* 내 용 : 미로 찾는 생쥐
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#define MAZE_SIZE 19
#define ROBOT 15
#define UP 1
#define RIGHT 2
#define DOWN 4
#define LEFT 8
// 얼굴 모양의 ASCII CODE
int maze[MAZE_SIZE][MAZE_SIZE]=
{{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1},
{1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}};
int sx=MAZE_SIZE-1, sy=MAZE_SIZE-2; // 생쥐의 출발 위치
int * rec; // 최단 경로 계산을 위해 생쥐가 움직인 경로를 저장
void gotoxy(int x,int y)
{
COORD pos={x,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
int get_shape(int m[][MAZE_SIZE], int x, int y)
{
static shape[]=
{32, 5, 6, 3, 5, 5, 1, 25, 6, 4, 6,
21, 2, 23, 22, 16};
//{32, 179, 196, 192, 179, 179, 218, 195, 196, 217, 196,
// 193, 191, 180, 194, 197};
// 5=|,
int s=0;
if(m[y][x])
{
if(y>0 && m[y-1][x])
s |= UP; // y>0이 윗쪽 경계를 넘지않나 확인
if(y<MAZE_SIZE-2 && m[y+1][x])
s |= DOWN; // y<MAZE_SIZE-2 가 아랫쪽 경계를 넘지 않나 확인
if(x>0 && m[y][x-1])
s |= LEFT; // x>0이 왼쪽 경계를 넘지 않나 확인
if(x<MAZE_SIZE-2 && m[y][x+1])
s |= RIGHT; // x<MAZE_SIZE-2가 오른쪽 경계를 넘지 않나 확인
}
return shape[s];
}
// 미로를 화면에 그려줌
void draw_maze(int m[][MAZE_SIZE])
{
int i, j;
for(j=0; j<MAZE_SIZE; j++)
{
for(i=0; i<MAZE_SIZE; i++)
{
gotoxy(i+1, j+1); // gotoxy()는 화면 좌측상단이(0,0)이 아니라 (1,1)이기 때문에 1을 더함
putch(get_shape(m, i, j));
}
}
}
// 생쥐의 이동 경로를 저장
void record(int x, int y)
{
static int index=0; // 내부 정적 변수
rec[index++]=x;
rec[index++]=y;
}
// 생쥐를 한칸 앞으로 이동
void forward(int *x, int *y, int dir)
{
gotoxy(*x+1, *y+1); // 생쥐를 지운다.
putch(' ');
*x=(dir==LEFT) ? --(*x) :
(dir==RIGHT) ? ++(*x) : *x;
*y=(dir==UP) ? --(*y) :
(dir==DOWN) ? ++(*y) : *y;
record(*x, *y); // 최단 경로를 찾기 위해 경로를 저장
gotoxy(*x+1, *y+1); // 생쥐를 그린다.
putch(ROBOT);
}
// 생쥐의 방향을 시계 방향으로
void right(int *dir)
{
*dir<<=1;
*dir=(*dir>LEFT) ? UP : *dir; // LEFT를 시계 방향으로 튼 경우
}
// 생쥐의 방향을 반시계 방향으로
void left(int *dir)
{
*dir>>=1;
*dir=(*dir==0) ? LEFT : *dir; // UP을 반시계 방향으로 튼 경우
}
// 아직 미로에 들어 있는가?
int still_in_maze(int x, int y)
{
if(x>0 && x<MAZE_SIZE-1 && y>0 && y<MAZE_SIZE-1)
return 1;
else
return 0;
}
// 벽이 앞에 있는가?
int wall_ahead(int m[][MAZE_SIZE], int x, int y, int dir)
{
x=(dir==LEFT) ? --x : (dir==RIGHT) ? ++x : x;
y=(dir==UP) ? --y : (dir==DOWN) ? ++y : y;
return m[y][x];
}
// 우선법 알고리즘
void right_hand(int m[][MAZE_SIZE], int x, int y, int dir)
{
gotoxy(x+1, y+1); // 처음 생쥐의 모습을 그려줌
putch(ROBOT);
record(x, y); // 최단 경로를 찾기 위해 처음의 좌표를 저장
forward(&x, &y, dir); // 앞으로 간다
while(still_in_maze(x, y)) // 아직 미로 안이면
{
Sleep(100); // 이동 속도가 너무 빠르므로 지연 시간을 넣음
right(&dir); // 시계 방향으로 튼다
while(wall_ahead(m, x, y, dir)) // 앞에 벽이 있으면
left(&dir); // 왼쪽으로 튼다
forward(&x, &y, dir); // 앞으로 간다.
}
record(-1, -1); // 경로의 끝을 나타냄
}
// 중복된 경로의 삭제
int del_path(int i, int j)
{
int k=i;
while(rec[j]>=0) // 삭제
rec[i++]=rec[j++];
rec[i]=-1;
return k; // k는 삭제되고 난 뒤의 j가 가져야 할 값
}
// 중복된 경로 제거 알고리즘
void shortest_path(void)
{
int i=0;
int x, y;
int j;
int x1, y1;
while(rec[i]>=0) // 최단경로 찾기
{
x=rec[i];
y=rec[i+1];
j=i+2; // 2를 더한 이유? 한 좌표가 두 정수이므로 다음 좌표는 +2이다.
while(rec[j]>=0)
{
x1=rec[j];
y1=rec[j+1];
if(x==x1 && y==y1) // 같은 좌표가 발견되면
j=del_path(i, j); // 중복되는 경로 삭제
j++;
j++;
}
i++;
i++;
}
i=0; // 최단 경로를 움직이는 생쥐 애니메이션
while(rec[i]>=0)
{
x=rec[i++];
y=rec[i++];
gotoxy(x+1, y+1);
putch(ROBOT); // 생쥐를 그림
Sleep(100); // 지연 시간
gotoxy(x+1, y+1);
putch(' '); // 생쥐를 지움
}
}
int main(void)
{
int i;
rec=(int*)malloc(MAZE_SIZE*MAZE_SIZE); // 생쥐의 움직임 저장
if(rec==NULL) // 메모리 할당 에러
{
printf("\n Memory allocation error! \n");
exit(1);
}
system("cls");
/*for(i=0; i<200; i++)
{
putch(i);
printf(" %d\n", i);
}*/
draw_maze(maze);
gotoxy(40, 5);
printf("Simulation of Mrio Mouse");
gotoxy(40, 10);
printf("Press any key to start");
getchar();
//bioskey(0);
right_hand(maze, sx, sy, LEFT); // 생쥐 1차 시도
/* 위에 right_hand 실행하면 에러남... 왜그럼?
* 한줄씩 디버깅하면 괜찮은데... 그냥 런하면 에러남..
*
*/
gotoxy(40, 10);
printf("Press any key to see shortest path...");
//bioskey(0);
getchar();
shortest_path(); // 생쥐 2차 시도, 최단 경로
gotoxy(40, 10);
printf("Press any key to end program... ");
//bioskey(0);
getchar();
return 0;
}
댓글 영역
획득법
① NFT 발행
작성한 게시물을 NFT로 발행하면 일주일 동안 사용할 수 있습니다. (최초 1회)
② NFT 구매
다른 이용자의 NFT를 구매하면 한 달 동안 사용할 수 있습니다. (구매 시마다 갱신)
사용법
디시콘에서지갑연결시 바로 사용 가능합니다.