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
| #include <iostream> #include <queue> using namespace std; int r, c, cx, cy, xx, yy, gs, wss, dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0}, ans; char arr[52][52]; queue<int> wx, wy, gx, gy; int main() { cin >> r >> c; for(int i = 0; i < r; i++) { scanf("%s", &arr[i]); for(int j = 0; j < c; j++) if(arr[i][j] == 'S') gx.push(j), gy.push(i); else if(arr[i][j] == '*') wx.push(j), wy.push(i); } while(!gx.empty()) { wss = wx.size(); gs = gx.size(); while(wss--) { cx = wx.front(); wx.pop(); cy = wy.front(); wy.pop(); for(int i = 0; i < 4; i++) { xx = cx + dx[i]; yy = cy + dy[i]; if(xx >= c || yy >= r || xx < 0 || yy < 0 || arr[yy][xx] != '.') continue; wx.push(xx), wy.push(yy); arr[yy][xx] = '*'; } } while(gs--) { cx = gx.front(); gx.pop(); cy = gy.front(); gy.pop(); if(arr[cy][cx] == 'D') { cout << ans; return 0; } for(int i = 0; i < 4; i++) { xx = cx + dx[i]; yy = cy + dy[i]; if(xx >= c || yy >= r || xx < 0 || yy < 0 || arr[yy][xx] == 'S' || arr[yy][xx] == '*' || arr[yy][xx] == 'X') continue; gx.push(xx), gy.push(yy); if(arr[yy][xx] != 'D') arr[yy][xx] = 'S'; } } ans++; } cout << "KAKTUS"; }
|