HDU 2102 A计划(三维BFS)
这题太欢乐了......虽然wa了几次,但是想到骑士在两幅图的传送门中传来传去就觉得这骑士太坑了
#include <cstdio> #include <iostream> #include <cmath> #include <cstring> using namespace std; int n,m,cost,head,tail,ans; char map[2][15][15]; int sum[2][15][15]; struct node { int z,x,y; } q[11111]; node st, end; int dirx[4] = {1,-1,0,0}; int diry[4] = {0,0,1,-1}; void init() { memset(sum,-1,sizeof(sum)); ans = 0; } bool go(int z,int x,int y) { if(x < 0 || x >= n || y < 0 || y >= m) return false; if(map[z][x][y] == '*') return false; if(sum[z][x][y] != -1) return false; // return true; } int bfs() { head = 0; tail = 0; q[head++] = st; sum[st.z][st.x][st.y] = 0; while(head != tail) { node t = q[tail++]; node tt; if(end.z == t.z && end.x == t.x && end.y == t.y) { if(cost >= sum[t.z][t.x][t.y]) { return sum[t.z][t.x][t.y]; } else return -1; } for(int i=0; i<4; i++) { tt.z = t.z; tt.x = t.x + dirx[i]; tt.y = t.y + diry[i]; if(go(tt.z,tt.x,tt.y)) { //cout << tt.z << ' ' << tt.x << ' ' << tt.y << endl; if(map[tt.z][tt.x][tt.y] == '.' || map[tt.z][tt.x][tt.y] == 'P') { sum[tt.z][tt.x][tt.y] = sum[t.z][t.x][t.y] + 1; q[head++] = tt; } if(map[tt.z][tt.x][tt.y] == '#' && map[1 - tt.z][tt.x][tt.y] != '*' && map[1 - tt.z][tt.x][tt.y] != '#'){ sum[tt.z][tt.x][tt.y] = sum[t.z][t.x][t.y] + 1; sum[1 - tt.z][tt.x][tt.y] = sum[tt.z][tt.x][tt.y]; tt.z = 1 - tt.z; q[head++] = tt; } } } } return -1; } int main() { int T; cin >> T; while(T --) { init(); cin >> n >> m >> cost; for(int z=0; z<2; z++) for(int i=0; i<n; i++) for(int j=0; j<m; j++) { cin >> map[z][i][j]; if(map[z][i][j] == 'S') { st.z = z; st.x = i; st.y = j; } if(map[z][i][j] == 'P') { end.z = z; end.x = i; end.y = j; } } if(bfs() == -1) printf("NO\n"); else printf("YES\n"); } return 0; }