#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_STACK_SIZE 100
#define ROW_SIZE 3
#define COL_SIZE 4
#define TRUE 1
#define FALSE 0
typedef struct // 4방향의 좌표를 가질 구조체
{
int row;
int col;
int dir;
}element;
element stack[MAX_STACK_SIZE];
typedef struct
{
int vert;
int horiz;
}offsets;
offsets move[4];
int top = -1;
char mark[ROW_SIZE+2][COL_SIZE+2];
int EXIT_ROW = ROW_SIZE, EXIT_COL = COL_SIZE;
int maze[ROW_SIZE+2][COL_SIZE+2] = { // 미로를 구성하는 배열 (바깥테두리는 모두 1로 표시)
{1,1,1,1,1,1},
{1,0,0,0,0,1},
{1,0,0,0,0,1},
{1,0,0,0,0,1},
{1,1,1,1,1,1},
};
void path(void);
void add(int *top, element item);
element del(int *top);
void stack_full();
element stack_empty();
int main(void)
{
int i, j;
for(i=0;i<=ROW_SIZE+1; i++)
{
for(j=0; j<=COL_SIZE+1; j++)
{
if (maze[i][j] == 0) mark[i][j] = \'0\';
if (maze[i][j] == 1) mark[i][j] = \'1\';
}
}
move[0].vert = 1; move[0].horiz = 0; /* 동쪽 */
move[1].vert = -1; move[1].horiz = 0; /* 서쪽 */
move[2].vert = 0; move[2].horiz = 1; /* 남쪽 */
move[3].vert = 0; move[3].horiz = -1; /* 북쪽 */
path();
for(i=0;i<=ROW_SIZE+1; i++)
{
for(j=0; j<=COL_SIZE+1; j++)
{
printf("%c", mark[i][j]);
}
printf("\\n");
}
return 0;
}
void path(void)
{
int i, j, row, col, next_row, next_col, dir, found = FALSE;
element position;
maze[1][1] = 1;
top =0;
stack[0].row = 1;
stack[0].col = 1;
stack[0].dir = 0;
while(top > -1 && !found)
{
position = del(&top);
row=position.row;
col=position.col;
dir=position.dir;
while(dir < 4 && !found)
{
next_row = row + move[dir].vert;
next_col = col + move[dir].horiz;
if(next_row == EXIT_ROW && next_col == EXIT_COL)
{
found = TRUE;
}
else if(!maze[next_row][next_col])
{
maze[next_row][next_col] = 1;
position.row = row;
position.col = col;
position.dir = ++dir;
add(&top, position);
row = next_row;
col = next_col;
dir = 0;
}
else ++dir;
}
}
if(found)
{
printf("The path is:\\n");
printf(" row col\\n");
for(i = 0; i<=top; i++)
{
printf(" <%3d,%3d> ", stack[i].row, stack[i].col);
for (j=0; j<4; j++)
{
if (stack[i+1].row - stack[i].row == move[j].horiz && stack[i+1].col - stack[i].col == move[j].vert)
{
mark[stack[i].row ][stack[i].col ] = j + 2 + \'0\';
}
}
if((i+1)%6 == 0) printf("\\n");
}
printf(" <%3d,%3d> ", row, col);
printf(" <%3d,%3d>\\n", EXIT_ROW, EXIT_COL);
for (j=0; j<4; j++)
{
if (row - stack[i-1].row == move[j].horiz && col - stack[i-1].col == move[j].vert)
{
mark[stack[top].row ][stack[top].col ] = j + 2+ \'0\';
}
if (EXIT_ROW - row == move[j].horiz && EXIT_COL - col == move[j].vert)
{
mark[row][col] = j + 2+ \'0\';
}
}
for(i=0; i<ROW_SIZE+1; i++)
{
for (j=0; j<COL_SIZE+1; j++)
{
switch (mark[i][j])
{
case \'2\':
mark[i][j] = \'>\';
break;
case \'3\':
mark[i][j] = \'<\';
break;
case \'4\':
mark[i][j] = \'V\';
break;
case \'5\':
mark[i][j] = \'^\';
break;
}
}
}
mark[EXIT_ROW][EXIT_COL] = \'F\';
}
else printf(" The maze does not have a path\\n");
}
void add(int *top, element item)
{
if(*top >= MAX_STACK_SIZE-1)
{
stack_full();
return;
}
stack[++(*top)] = item;
}
element del(int *top)
{
if(*top == -1)
return stack_empty();
return stack[(*top)--];
}
void stack_full()
{
fprintf(stderr, "Stack is full !! \\n");
}
element stack_empty()
{
element item;
item.col = -1;
item.dir = -1;
item.row = -1;
fprintf(stderr,"Stack is empty !! \\n");
return item;
}
댓글 영역
획득법
① NFT 발행
작성한 게시물을 NFT로 발행하면 일주일 동안 사용할 수 있습니다. (최초 1회)
② NFT 구매
다른 이용자의 NFT를 구매하면 한 달 동안 사용할 수 있습니다. (구매 시마다 갱신)
사용법
디시콘에서지갑연결시 바로 사용 가능합니다.