1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| #include <stdio.h> #include <stdbool.h>
#define ANS_X 2 #define ANS_Y 2 #define BOUNDARY_X 3 #define BOUNDARY_Y 3 #define MAX 10
int used[BOUNDARY_X][BOUNDARY_Y] = { 0 }; int dir[][2] = { {0, 1}, {1, 0}, {0, -1}, {-1, 0} }; int best[MAX][MAX];
bool DFS(int used[BOUNDARY_X][BOUNDARY_Y], int x, int y);
int main(void) { int i, j;
DFS(used, 0, 0);
for ( i = 0; i < MAX; i++ ) { for ( j = 0; j < MAX; j++ ) { if ( i == 0 && j == 0 ) printf("#"); else if ( best[i][j] ) printf("*"); else printf(" "); } printf("\n"); }
return 0; }
bool DFS(int used[BOUNDARY_X][BOUNDARY_Y], int x, int y) { if ( (x == ANS_X) && (y == ANS_Y) ) { return true; }
for ( int i = 0; i <= 4; i++ ) { int new_x = x + dir[i][0], new_y = y + dir[i][1]; if ( new_x >= 0 && new_x < BOUNDARY_X && new_y >= 0 && new_y < BOUNDARY_X && used[new_x][new_y] == 0 ) {
used[new_x][new_y] = 1;
if ( DFS(used, new_x, new_y) ) { printf("(%d,%d)\n", new_x, new_y); best[new_x][new_y] = 1; return true; }
used[new_y][new_x] = 0; } }
return false; }
|