13 3 4 200 1 1 10 0 1 10 1 1 11 1 1 11 0 0 10 1 1 10 0 0 00 1 1 00 1 1 0 Sample Output11用 bfs 搜算算法 即可 用到STL 里的queue 队列
#include<iostream>#include<cstring>#include<queue> using namespace std;struct Node{ int x,y,z; int time;};int map[55][55][55];//当做地图 又起 vis的作用 int dir[6][3]={0,0,1,0,0,-1,1,0,0,-1,0,0,0,1,0,0,-1,0};int A,B,C,T;int bfs(int i,int j,int k){ queue<Node>q; Node next,v; v.x=i; v.y=j; v.z=k; v.time=0; q.push(v); map[i][j][k]=1;//状态变化 while(!q.empty()) { v=q.front();//取头 q.pop();//释放 // printf("999/n");// //放在外面的话 v=q.front() 相当于下次取出来后 判断 用v // if(v.x==A&&v.y==B&&v.z==C&&v.time<=T)//找到出口 // {//用v // return v.time;//返回时间 // } for(int i=0;i<6;i++)//六个方向 { next.x=v.x+dir[i][0]; //next 作为下一个 方向 next.y=v.y+dir[i][1]; next.z=v.z+dir[i][2]; if(next.x>=1&&next.x<=A&&next.y>=1&&next.y<=B&&next.z>=1&&next.z<=C&&next.time<=T) { // 符合 条件 if(!map[next.x][next.y][next.z])// 道路可通 没被访问 { next.time=v.time+1;//时间变化 //在里面的话 就用这次 来判断 if(next.x==A&&next.y==B&&next.z==C&&next.time<=T)//找到出口 {//用next // next.time=v.time+1; return next.time;//返回时间 } map[next.x][next.y][next.z]=1;//状态变化 q.push(next);//压入队列 } } } } return -1; } int main(){ int t; int i,j,l; cin>>t; while(t--) { cin>>A>>B>>C>>T; for(i=1;i<=A;i++) for(j=1;j<=B;j++) for(l=1;l<=C;l++) scanf("%d",&map[i][j][l]); int sum=bfs(1,1,1); printf("%d/n",sum); }}/*13 3 4 200 1 1 10 0 1 10 1 1 11 1 1 11 0 0 10 1 1 10 0 0 00 1 1 00 1 1 0*/这里说明一下 判断是否 到大终点 得到if 判断 可以放在外面也可以放在 里面的但是 判断的 的数值要不同
当在外面时 :
当在里面时:
这个地方 要注意一下
新闻热点
疑难解答