Introfor

Recursion. 미로찾기 본문

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;
}

ref. 

'Doing > C&C++' 카테고리의 다른 글

Byte padding  (0) 2020.09.07
[Recursion] Counting Cells in a Blob  (0) 2020.07.30
[data_structure] Linked List  (0) 2020.07.26
[Algorithm] sequential Search  (0) 2020.07.01
[C++] Reference  (0) 2020.06.29
Comments