題目連結: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 0x7fffffffusing 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;}