Doing/C&C++
Recursion. 미로찾기
YongArtist
2020. 7. 28. 11:32
#include <stdio.h>
int size = 8;
int maze[8][8] = {
{0, 0, 0, 0, 0, 0, 0, 1},
{0, 1, 1, 0, 1, 1, 0, 1},
{0, 0, 0, 1, 0, 0, 0, 1},
{0, 1, 0, 0, 1, 1, 0, 0},
{0, 1, 1, 1, 0, 0, 1, 1},
{0, 1, 0, 0, 0, 1, 0, 1},
{0, 0, 0, 1, 0, 0, 0, 1},
{0, 1, 1, 1, 0, 1, 0, 0},
};
int pathWay = 0;
int wall = 1;
int blocked = 2;
int path = 3;
int find_path(int x, int y){
if (x<0 || y<0 || x>=size || y>=size)
return 0;
else if (maze[x][y] != pathWay)
return 0;
else if (x==size-1 && y==size-1){
maze[0][0] = path;
return 1;
}
else{
maze[x][y] = path;
if (find_path(x+1, y) || find_path(x, y+1) || find_path(x-1, y) || find_path(x, y-1))
return 1;
maze[x][y] = blocked;
return 1;
}
}
int main(){
if (find_path(0,0))
printf("Found");
else
printf("Not Found");
return 0;
}