A计划
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9891 Accepted Submission(s): 2383
Problem Description
可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。
现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。
现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。
Input
输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小N*M(1 <= N,M <=10)。T如上所意。接下去的前N*M表示迷宫的第一层的布置情况,后N*M表示迷宫第二层的布置情况。
Output
如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。
Sample Input
1 5 5 14 S*#*. .#... ..... ****. ...#. ..*.P #.*.. ***.. ...*. *.#..
Sample Output
YES
Source
Recommend
——分割线——
一题纯SPFA,但还有一些坑点的~好久不写了这个算法了,感觉小错不断,好坑!
代码:
/*Author:WNJXYK*/
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<set>
#include<queue>
using namespace std;
#define LL long long
int m,n;
int T;
int map[2][15][15];
struct localN{
int k,x,y,t;
localN(){}
localN(int a,int b,int c,int d){
k=a;
x=b;
y=c;
t=d;
}
};
int Dx[]={0,1,0,-1,0};
int Dy[]={0,0,1,0,-1};
queue<localN> que;
bool inque[2][15][15];
inline int BFS(int sk,int sx,int sy){
while(!que.empty()) que.pop();
memset(inque,false,sizeof(inque));
que.push(localN(sk,sx,sy,0));
inque[sk][sx][sy]=true;
while(!que.empty()){
localN point=que.front();
que.pop();
int lt=point.t;
int lk=point.k;
int lx=point.x;
int ly=point.y;
if (lt>T) continue;
if (map[lk][lx][ly]==3) return lt;
for (int p=1;p<=4;p++){
int x=Dx[p]+lx;
int y=Dy[p]+ly;
if (1<=x && x<=m && 1<=y && y<=n && inque[lk][x][y]==false){
inque[lk][x][y]=true;
if (map[lk][x][y]==0 || map[lk][x][y]==3){
que.push(localN(lk,x,y,lt+1));
}
if (map[lk][x][y]==2 && map[1-lk][x][y]!=1 && map[1-lk][x][y]!=2){
que.push(localN(1-lk,x,y,lt+1));
}
}
}
}
return -1;
}
inline void getChar(char &x){
scanf("%c",&x);
while(x!='.' && x!='*' && x!='#' && x!='P' && x!='S') scanf("%c",&x);
}
inline void SolveProblem(){
scanf("%d%d%d",&m,&n,&T);
char x;
int sk,sx,sy;
for (int k=0;k<=1;k++)
for (int i=1;i<=m;i++){
for (int j=1;j<=n;j++){
getChar(x);
if (x=='.') map[k][i][j]=0;
if (x=='*') map[k][i][j]=1;
if (x=='#') map[k][i][j]=2;
if (x=='P') map[k][i][j]=3;
if (x=='S'){
map[k][i][j]=0;
sk=k;sx=i;sy=j;
}
}
}
if (BFS(sk,sx,sy)!=-1){
printf("YES\n");
}else{
printf("NO\n");
}
}
int main(){
int C;
scanf("%d\n",&C);
for (;C--;) SolveProblem();
return 0;
}