A计划(bfs)
2019-08-02 20:57 木木王韦 阅读(112) 评论(0) 收藏 举报A计划
可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。
现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。
Input
输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小NM(1 <= N,M <=10)。T如上所意。接下去的前NM表示迷宫的第一层的布置情况,后NM表示迷宫第二层的布置情况。
Output
如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。
Sample Input
1
5 5 14
S*#*.
.#…
…
****.
…#.
….P
#.…
**…
….
*.#…
Sample Output
YES
ac代码:
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
struct node{
int x,y,z,step;
friend bool operator<(node a,node b){
return a.step>b.step;
}
};
int d[4][2]={0,1,1,0,0,-1,-1,0};
int vis[11][11][3];
char s[11][11][3];
int sx,sy,sz,ex,ey,ez;
int flag;
int m,n,tt,t;
void bfs(int x,int y,int z,int step){
memset(vis,0,sizeof(vis));
node e1,e2;
e1.x =x;
e1.y =y;
e1.z =z;
e1.step =0;
vis[x][y][z]=1;
priority_queue<node> que;
que.push(e1);
while(!que.empty()){
e1=que.top();
que.pop();
if(s[e1.x ][e1.y ][e1.z ]=='P'){
if(e1.step <=t){
flag=1;
}
break;
}
for(int i=0;i<4;i++){
e2.x =e1.x +d[i][0];
e2.y =e1.y +d[i][1];
e2.z=e1.z;
if(e2.x>=0&&e2.x<n&&e2.y>=0&&e2.y<m&&!vis[e2.x][e2.y][e2.z]&&s[e2.x][e2.y][e2.z]!='*' ){
//cout<<"e2:"<<e2.x<<" "<<e2.y<<" "<<e2.z<<endl;
if(s[e2.x][e2.y][e2.z]!='#'){
vis[e2.x][e2.y][e2.z]=1;
e2.step =e1.step +1;
que.push(e2);
}
else{
vis[e2.x][e2.y][e2.z]=1;
if(e1.z ==0){
e2.z =1;
if(s[e2.x][e2.y][e2.z]!='*'&&s[e2.x][e2.y][e2.z]!='#'){
e2.step =e1.step ;
vis[e2.x][e2.y][e2.z]=1;
que.push(e2);
}
}
else{
e2.z =0;
if(s[e2.x][e2.y][e2.z]!='*'&&s[e2.x][e2.y][e2.z]!='#'){
e2.step =e1.step ;
vis[e2.x][e2.y][e2.z]=1;
que.push(e2);
}
}
}
}
}
}
}
int main(){
cin>>tt;
while(tt--){
flag=0;
cin>>m>>n>>t;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>s[i][j][0];
if(s[i][j][0]=='S'){
sx=i;
sy=j;
sz=0;
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>s[i][j][1];
if(s[i][j][1]=='S'){
sx=i;
sy=j;
sz=1;
}
}
}
//cout<<sx<<sy<<sz<<endl;
bfs(sx,sy,sz,0);
if(flag) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
浙公网安备 33010602011771号