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
| #include<iostream> #include<algorithm> #include<string> #include<cstdio> #include<cstring> #include<queue> #include<unordered_map> #include<map>
using namespace std;
const int N = 110;
char s[N][N]; bool st[N][N]; int ans; int xs, ys, xe, ye; int row, col, k; int dx[4] = { 1,-1,0,0 }, dy[4] = { 0,0,1,-1 };
void dfs(int x, int y, int time) { if (x == xe && y == ye) { ans = time; return; } int xx = 0, yy = 0; for (int i = 0; i <= 3; i++) { int xx = x + dx[i], yy = y + dy[i]; if(!(x <= 0 || y <= 0 || x > row || y > col || st[xx][yy])&&!(time+1 >= ans)&&!(s[xx][yy] == '#' && (time+1) % k != 0)) { st[xx][yy] = true;
dfs(xx, yy, time + 1);
st[xx][yy] = false; } }
}
int main() { int t; cin >> t;
while (t--) { cin >> row >> col >> k;
ans = 0x3f3f3f3f; memset(s, '#', sizeof s);
for (int i = 1; i <= row; i++) for (int j = 1; j <= col; j++) { cin >> s[i][j]; if (s[i][j] == 'S')xs = i, ys = j; if (s[i][j] == 'E')xe = i, ye = j; }
dfs(xs, ys, 0); if (ans == 0x3f3f3f3f)cout << "Oop!" << endl; else cout<<ans<<"\n";
}
return 0; }
|