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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| #include<iostream> using namespace std;
int n ,m, start_x, start_y, end_x, end_y, start_dir, end_dir, nx, ny, n_dir; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, 1, 0, -1}; char arr[55][55]; bool used[55][55], out=false, found;
void DFS(int x, int y, int dir){ if(found) return; if(x==end_x && y==end_y){ found=true; for(int i=0 ; i<n ; i++){ for(int j=0 ; j<m ; j++){ cout<<arr[i][j]; } cout<<"\n"; } return; } if(arr[x][y] == '.'){ nx = x + dx[dir]; ny = y + dy[dir]; DFS(nx, ny, dir); } else if(arr[x][y] == '*'){ return; } else{ if(arr[x][y] == '/'){ bool tmp=used[x][y]; used[x][y] = true; n_dir = dir^1; nx = x + dx[n_dir]; ny = y + dy[n_dir]; DFS(nx, ny, n_dir); used[x][y] = tmp; if(!used[x][y]){ used[x][y] = true; arr[x][y] = '\\'; n_dir = 3-dir; nx = x + dx[n_dir]; ny = y + dy[n_dir]; DFS(nx, ny, n_dir); arr[x][y] = '/'; used[x][y] = false; } } else{ bool tmp=used[x][y]; used[x][y] = true; n_dir = 3-dir; nx = x + dx[n_dir]; ny = y + dy[n_dir]; DFS(nx, ny, n_dir); used[x][y] = tmp; if(!used[x][y]){ used[x][y] = true; arr[x][y] = '/'; n_dir = dir^1; nx = x + dx[n_dir]; ny = y + dy[n_dir]; DFS(nx, ny, n_dir); arr[x][y] = '\\'; used[x][y] = false; } } } }
int main(){ while(cin>>m>>n && (m!=-1 && n!=-1)){ if(out) cout<<"\n"; else out=true; found=false; start_x = start_y = end_x = end_y = -1; for(int i=0 ; i<n ; i++){ for(int j=0 ; j<m ; j++){ used[i][j] = false; cin>>arr[i][j]; if((i==0 || i==n-1 || j==0 || j==m-1) && arr[i][j]=='.'){ if(start_x == -1){ start_x = i; start_y = j; if(i==0) start_dir = 2; else if(i==n-1) start_dir = 0; else if(j==0) start_dir = 1; else start_dir = 3; } else{ end_x = i; end_y = j; } } } } DFS(start_x, start_y, start_dir); } return 0; }
|