|
| 1 | +#include<iostream> |
| 2 | +#include<cstring> |
| 3 | +#include<cstdio> |
| 4 | +#include<cassert> |
| 5 | +using namespace::std; |
| 6 | + |
| 7 | +const int max_rc = 50; |
| 8 | +char area[max_rc+5][max_rc+5], reach[max_rc+5][max_rc+5], stuck[max_rc+5][max_rc+5]; |
| 9 | +int start_x, start_y, end_x, end_y; |
| 10 | +int r, c; |
| 11 | + |
| 12 | +void dfs_forward(int x, int y) |
| 13 | +{ |
| 14 | + reach[x][y] = 1; |
| 15 | +// printf("Reach at x = %d, y = %d\n", x, y); |
| 16 | + if(area[x][y] == '+' || area[x][y] == 'S' || area[x][y] == 'T'){ |
| 17 | + if(x > 0 && !reach[x-1][y] && area[x-1][y] != '#') |
| 18 | + dfs_forward(x-1, y); |
| 19 | + if(x < r-1 && !reach[x+1][y] && area[x+1][y] != '#') |
| 20 | + dfs_forward(x+1, y); |
| 21 | + if(y > 0 && !reach[x][y-1] && area[x][y-1] != '#') |
| 22 | + dfs_forward(x, y-1); |
| 23 | + if(y < c-1 && !reach[x][y+1] && area[x][y+1] != '#') |
| 24 | + dfs_forward(x, y+1); |
| 25 | + } |
| 26 | + else if(area[x][y] == '-'){ |
| 27 | + if(y > 0 && !reach[x][y-1] && area[x][y-1] != '#') |
| 28 | + dfs_forward(x, y-1); |
| 29 | + if(y < c-1 && !reach[x][y+1] && area[x][y+1] != '#') |
| 30 | + dfs_forward(x, y+1); |
| 31 | + } |
| 32 | + else if(area[x][y] == '|'){ |
| 33 | + if(x > 0 && !reach[x-1][y] && area[x-1][y] != '#') |
| 34 | + dfs_forward(x-1, y); |
| 35 | + if(x < r-1 && !reach[x+1][y] && area[x+1][y] != '#') |
| 36 | + dfs_forward(x+1, y); |
| 37 | + } |
| 38 | + else if(area[x][y] == '.'){ |
| 39 | + if(x < r-1 && !reach[x+1][y] && area[x+1][y] != '#') |
| 40 | + dfs_forward(x+1, y); |
| 41 | + } |
| 42 | + else{ |
| 43 | + cout << "Error!" << endl; |
| 44 | + printf("Error at x = %d, y = %d\n", x, y); |
| 45 | + } |
| 46 | + return; |
| 47 | +} |
| 48 | + |
| 49 | +void dfs_back(int x, int y) |
| 50 | +{ |
| 51 | + assert(y < c); |
| 52 | +// printf("Reach at x = %d, y = %d\n", x, y); |
| 53 | + stuck[x][y] = 1; //1±íʾÕâÀïÄܹ»µ½´ït |
| 54 | + if(x > 0 && !stuck[x-1][y] && area[x-1][y] != '-' && area[x-1][y] != '#') |
| 55 | + dfs_back(x-1, y); |
| 56 | + if(x < r-1 && !stuck[x+1][y] && area[x+1][y] != '-' && area[x+1][y] != '#' && area[x+1][y] != '.') |
| 57 | + dfs_back(x+1, y); |
| 58 | + if(y > 0 && !stuck[x][y-1] && area[x][y-1] != '|' && area[x][y-1] != '#' && area[x][y-1] != '.') |
| 59 | + dfs_back(x, y-1); |
| 60 | + if(y < c-1 && !stuck[x][y+1] && area[x][y+1] != '|' && area[x][y+1] != '#' && area[x][y+1] != '.') |
| 61 | + dfs_back(x, y+1); |
| 62 | + return; |
| 63 | +} |
| 64 | + |
| 65 | +void print_reach(void) |
| 66 | +{ |
| 67 | + for(int i=0;i<r;i++){ |
| 68 | + for(int j=0;j<c;j++){ |
| 69 | + printf("%d ", reach[i][j]); |
| 70 | + } |
| 71 | + cout << endl; |
| 72 | + } |
| 73 | +} |
| 74 | +void print_stuck(void) |
| 75 | +{ |
| 76 | + for(int i=0;i<r;i++){ |
| 77 | + for(int j=0;j<c;j++){ |
| 78 | + printf("%d ", stuck[i][j]); |
| 79 | + } |
| 80 | + cout << endl; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +int main() |
| 85 | +{ |
| 86 | + cin >> r >> c; |
| 87 | + getchar(); |
| 88 | + memset(reach, 0, sizeof(reach)); |
| 89 | + memset(stuck, 0, sizeof(stuck)); |
| 90 | + for(int i=0;i<r;i++){ |
| 91 | + for(int j=0;j<c;j++){ |
| 92 | + area[i][j] = getchar(); |
| 93 | + if(area[i][j] == 'S'){ |
| 94 | + start_x = i; |
| 95 | + start_y = j; |
| 96 | + } |
| 97 | + else if(area[i][j] == 'T'){ |
| 98 | + end_x = i; |
| 99 | + end_y = j; |
| 100 | + } |
| 101 | + } |
| 102 | + getchar(); |
| 103 | + } |
| 104 | + dfs_forward(start_x, start_y); |
| 105 | +// printf("Finish forward dfs!\n"); |
| 106 | +// printf("end_x = %d, end_y = %d, reach[end_x][end_y] = %d\n", end_x, end_y, reach[end_x][end_y]); |
| 107 | + if(!reach[end_x][end_y]) |
| 108 | + cout << "I'm stuck!" << endl; |
| 109 | + else{ |
| 110 | + dfs_back(end_x, end_y); |
| 111 | + int cnt = 0; |
| 112 | + for(int i=0;i<r;i++){ |
| 113 | + for(int j=0;j<c;j++){ |
| 114 | + if(reach[i][j] && !stuck[i][j]) |
| 115 | + cnt++; |
| 116 | + } |
| 117 | + } |
| 118 | + cout << cnt << endl; |
| 119 | + } |
| 120 | +// print_reach(); |
| 121 | +// cout << endl; |
| 122 | +// print_stuck(); |
| 123 | + return 0; |
| 124 | +} |
0 commit comments