HDU2102---A計劃(附測試資料)
阿新 • • 發佈:2019-02-15
#include<stdio.h> #include<iostream> #include<string.h> #include<queue> using namespace std; char maze[2][35][35]; int n,m,limit; bool vis[2][35][35],flag; struct node{int x,y,z,step;}; int dir[4][2]={0,1,0,-1,1,0,-1,0}; queue<node> q; node ft,now; bool judge(){ if(ft.x<0||ft.x>=n||ft.y<0||ft.y>=m) return 0; else if(vis[ft.z][ft.x][ft.y]==true) return 0; else if((maze[ft.z][ft.x][ft.y]=='*')) return 0; else if(maze[ft.z][ft.x][ft.y]=='#'&&maze[!ft.z][ft.x][ft.y]=='*') return 0; else if(maze[ft.z][ft.x][ft.y]=='#'&&maze[!ft.z][ft.x][ft.y]=='#') return 0; else return 1; } void bfs(){ ft.x=0,ft.y=0,ft.z=0,ft.step=0; while(!q.empty()) q.pop(); q.push(ft); vis[0][0][0]=true; while(!q.empty()){ now=q.front(); q.pop(); if(now.step>limit) return ; if(maze[now.z][now.x][now.y]=='P'){ printf("YES\n"); //printf("%d",now.step); flag=true; return; } for(int i=0;i<4;i++){ ft.x=now.x+dir[i][0]; ft.y=now.y+dir[i][1]; ft.z=now.z; if(judge()){ if(maze[ft.z][ft.x][ft.y]=='#') {vis[ft.z][ft.x][ft.y]=true; ft.z=!ft.z; } ft.step=now.step+1; vis[ft.z][ft.x][ft.y]=true; q.push(ft); } } } } int main() { //freopen("in.txt","r",stdin); int t ; cin>>t; while(t--){ cin>>n>>m>>limit; memset(vis,false,sizeof vis); memset(maze,'*',sizeof maze); flag=false; for(int i=0;i<n;i++){ scanf("%s",&maze[0][i]); } for(int i=0;i<n;i++){ scanf("%s",&maze[1][i]); } bfs(); if(!flag) printf("NO\n"); } }
最後就是我精心處理的測試資料辣