Created
September 5, 2022 19:17
-
-
Save huytd/629b4381e2de95e7c96e5dcdfd9bd20a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
const int N = 5; | |
int solve(int map[N][N], int x, int y, int ret[N][N]) { | |
if (map[y][x] == 2) { | |
return 1; | |
} else { | |
if (y - 1 >= 0 && map[y - 1][x] != 0 && ret[y - 1][x] == 0) { | |
ret[y - 1][x] = 1; | |
if (solve(map, x, y - 1, ret)) { | |
return 1; | |
} else { | |
ret[y - 1][x] = 0; | |
} | |
} | |
if (x + 1 < N && map[y][x + 1] != 0 && ret[y][x + 1] == 0) { | |
ret[y][x + 1] = 1; | |
if (solve(map, x + 1, y, ret)) { | |
return 1; | |
} else { | |
ret[y][x + 1] = 0; | |
} | |
} | |
if (y + 1 < N && map[y + 1][x] != 0 && ret[y + 1][x] == 0) { | |
ret[y + 1][x] = 1; | |
if (solve(map, x, y + 1, ret)) { | |
return 1; | |
} else { | |
ret[y + 1][x] = 0; | |
} | |
} | |
if (x - 1 >= 0 && map[y][x - 1] != 0 && ret[y][x - 1] == 0) { | |
ret[y][x - 1] = 1; | |
if (solve(map, x - 1, y, ret)) { | |
return 1; | |
} else { | |
ret[y][x - 1] = 0; | |
} | |
} | |
return 0; | |
} | |
} | |
int main() { | |
int map[N][N] = { | |
{1, 0, 0, 0, 2}, | |
{1, 1, 1, 0, 1}, | |
{1, 1, 1, 1, 1}, | |
{0, 0, 1, 0, 1}, | |
{0, 0, 1, 1, 1}, | |
}; | |
int ret[N][N] = { | |
{1, 0, 0, 0, 0}, | |
{0, 0, 0, 0, 0}, | |
{0, 0, 0, 0, 0}, | |
{0, 0, 0, 0, 0}, | |
{0, 0, 0, 0, 0}, | |
}; | |
solve(map, 0, 0, ret); | |
for (int i = 0; i < N; i++) { | |
for (int j = 0; j < N; j++) { | |
printf("%d ", ret[i][j]); | |
} | |
printf("\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment