您的位置:首页 > 其它

HDU 2102 A计划 BFS求解

2015-04-11 18:26 453 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102

题意简单,救公主,但是许多细节需要处理,看着数据比较小,又不是求最小时间量,第一反应就是DFS,因为DFS的代码量一般比较小,可惜TLE了,技术不够啊!

1,当两边都是#时,不能过去,当对面是*时,不能过去

2,不能认为只有这边是#,对面是 . 是才能过去,因为存在对面为P是的特殊情况,所以这告诉我们,不要任歪曲题意,还是跟着题意走,WA了好多次

附上一个有测试数据的链接:http://blog.csdn.net/liuqiyao_01/article/details/8858182

代码:

#include<iostream>
#include<sstream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<string>
#define LL __int64
#define INF 0x7fffffff

using namespace std;
struct node{
int z,x,y,step;
};

char Map[2][12][12];
int vis[2][12][12],N,M,T,P[3];

int d[][2]={1,0,-1,0,0,1,0,-1};
void bfs(){
memset(vis,0,sizeof(vis));
queue<node> Q;
Q.push((node){0,0,0,0});vis[0][0][0]=1;
while(!Q.empty()){
node tp=Q.front();
if(tp.z==P[0] && tp.x==P[1] && tp.y==P[2]){cout<<"YES\n";return;}
Q.pop();
if(tp.step>=T) continue;
for(int p=0;p<4;p++){
int tx=tp.x+d[p][0],ty=tp.y+d[p][1];
if(tx<0 || ty<0 || tx>=N || ty>=M) continue;
if(vis[tp.z][tx][ty] || Map[tp.z][tx][ty]=='*') continue;
if(Map[tp.z][tx][ty]=='#'){
if(Map[1-tp.z][tx][ty]!='#' && Map[1-tp.z][tx][ty]!='*' && !vis[1-tp.z][tx][ty])
vis[1-tp.z][tx][ty]=1,Q.push((node){1-tp.z,tx,ty,tp.step+1});
vis[tp.z][tx][ty]=1;
continue;
}
Q.push((node){tp.z,tx,ty,tp.step+1});vis[tp.z][tx][ty]=1;
}
}
cout<<"NO\n";
}

int main(){
//freopen("D:\\in.txt","r",stdin);
int C;cin>>C;
while(C--){
cin>>N>>M>>T;
for(int i=0;i<2;i++) for(int j=0;j<N;j++) for(int k=0;k<M;k++){
cin>>Map[i][j][k];
if(Map[i][j][k]=='P') P[0]=i,P[1]=j,P[2]=k;
}
bfs();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: