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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int C,M,N,T;
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
struct Node{
int f,x,y,time;
}point[2];
char loc[3][12][12];
int vis[3][12][12];
bool judge(Node p){
if(p.f<1||p.f>2||p.x>N||p.x<1||p.y<1||p.y>M||loc[p.f][p.x][p.y]=='*'||p.time>T)
return false;
else return true;
}
int bfs(){
queue<Node>Q;
int t;
Node loca,next;
Q.push(point[0]);
while(Q.size()!=0){
loca=Q.front();
Q.pop();
vis[loca.f][loca.x][loca.y]=1;
for(int i=0;i<4;i++){
next.f=loca.f;
next.x=loca.x+dx[i];
next.y=loca.y+dy[i];
next.time=loca.time+1;
if(vis[next.f][next.x][next.y]) continue;
else if(loc[next.f][next.x][next.y]=='.'){
vis[next.f][next.x][next.y]=1;
}
else if(loc[next.f][next.x][next.y]=='#'){
vis[next.f][next.x][next.y]=1;
next.f=3-next.f;
vis[next.f][next.x][next.y]=1;
if(loc[next.f][next.x][next.y]=='#') continue;
}
if(judge(next)) {
if(loc[next.f][next.x][next.y]=='P'){
return next.time;
}
Q.push(next);
}
}
}
return -1;
}
int main(){
int T1;
cin>>C;
while(C--){
cin>>N>>M>>T;
for(int flo=1;flo<=2;flo++){
for(int i=1;i<=N;i++){
for(int j=1;j<=M;j++){
cin>>loc[flo][i][j];
if(loc[flo][i][j]=='S') {
point[0].f=flo;
point[0].x=i;
point[0].y=j;
point[0].time=0;
}
}
}
}
memset(vis,0,sizeof(vis));
T1=bfs();
//cout<<T1<<endl;
if(T1>T||T1==-1) printf("NO\n");
else printf("YES\n");
}
return 0;
}
&spm=1001.2101.3001.5002&articleId=86771376&d=1&t=3&u=9c1405cf8a5747b6ac73c51a12631713)
1万+

被折叠的 条评论
为什么被折叠?



