게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
스택 미로 관력해서 질문이 있습니다
게시물ID : programmer_19485짧은주소 복사하기
작성자 : Athalwolf
추천 : 0
조회수 : 397회
댓글수 : 2개
등록시간 : 2017/01/03 23:33:42
옵션
  • 본인삭제금지
#include <stdio.h>
#define COLS 17
#define ROWS 13
#define START_ROW 1
#define START_COL 1
#define EXIT_ROW 11
#define EXIT_COL 16
#define MAX 50

int maze[ROWS][COLS] = {
{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
{ 1,0,1,0,0,0,1,1,0,0,0,1,1,1,1,1,1 },
{ 1,1,0,0,0,1,1,0,1,1,1,0,0,1,1,1,1 },
{ 1,0,1,1,0,0,0,0,1,1,1,1,0,0,1,1,1 },
{ 1,1,1,0,1,1,1,1,0,1,1,0,1,1,0,0,1 },
{ 1,1,1,0,1,0,0,1,0,1,1,1,1,1,1,1,1 },
{ 1,0,0,1,1,0,1,1,1,0,1,0,0,1,0,1,1 },
{ 1,0,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1 },
{ 1,0,0,1,1,0,1,1,0,1,1,1,1,1,0,1,1 },
{ 1,1,1,0,0,0,1,1,0,1,1,0,0,0,0,0,1 },
{ 1,0,0,1,1,1,1,1,0,0,0,1,1,1,1,0,1 },
{ 1,0,1,0,0,1,1,1,1,1,0,1,1,1,1,4,1 },
{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 } };

typedef struct _coord {
int  x;
int  y;
}coord;

int top = 0;
coord stack[MAX];
void push(int row,int col) {
top ++;
stack[top].x = row;
stack[top].y = col;
}

coord pop(coord*stack) {
int row;
int col;

row = stack[top].x;
col = stack[top].y;
maze[row][col] = 2;
return stack[top--];
}

void Fprint()
{
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
if (maze[i][j] == 1)
printf("■");
else
printf("  ");
}
printf("\n");
}
}

void Lprint()
{
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
if (maze[i][j] == 1)
printf("■");
else if (maze[i][j] == 2)
printf("□");
else if (maze[i][j] == 3)
printf("◎");
else if (maze[i][j] == 4)
printf("★");
else
printf("  ");
}
printf("\n");
}
}
void check(coord*stack)
{
int i;
int j;
coord temp;
int row = stack[top].x, col = stack[top].y;
for (i = -1; i < 2; i++) {
for (j = -1; j < 2; j++) {
if (maze[row + i][col + j] == 0) {
row = row + i; col = col + j;
push(row,col);
return;
}
}
}

temp = pop(stack);
stack[top].x = temp.x;
stack[top].y = temp.y;
}
int main()
{
Fprint();
int i;
stack[top].x = START_ROW;
stack[top].y = START_COL;
printf("\n\n");
while (1) {
check(stack);
if (stack[top].x == (EXIT_ROW - 1) && stack[top].y == (EXIT_COL - 1))
break;
}
//Lprint();
printf("%3d %3d", stack[top].x, stack[top].y);
return 0;
}

수정하다가 코드가 누더기가 되긴 했는데

스택 0번 방에 출발 지점을 지정해줘서 탑을 0부터 시작했습니다
미로 길 찾는데 0이 길이라 0이면 푸쉬에 넣는 걸로 했는데
1,1에서 무한루프를 돌더라고요
call by address로 값 계속 바꿔 줘보려고 했는데 에러만 나서 못했고


사실 어디가 어떻게 문제인지를 1시간 째 감도 못 잡고 있어서 
이렇게 글올려요..............

하나짜리 스택은 어떻게 다루겠는데
좌표 다루려니까 어려워요 ㅜㅜㅜ

도와주세요
꼬릿말 보기
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호