标签:des style blog http io color os 使用 sp
Description
Input
Output
Sample Input
Sample Output
#include<stdio.h> #include<queue> using namespace std; int space[55][55][55];//存位置: int vis[55][55][55];//标记是否走过 struct castle//记录过程 { int x,y,z,time_used; }; struct change//进行坐标改变 { int x,y,z; }move[6]={{1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1}}; //上楼 向后 向右 下楼 向前 向右 int A,B,C,T;//A:楼的层数,B:楼的长度,C:楼的宽度 int BFS() { queue<castle> q;//定义房间的队列 castle now,next;//记录过程 now.x=A-1; now.y=0; now.z=0;//初始时在底楼 now.time_used=0;//记录用时 vis[A-1][0][0]=1;//走过的路标记 q.push(now);//将起始位置加入队列 while(!q.empty()){ now=q.front();//把队首取出来 q.pop();//删除队首 if(now.time_used>T)//判断是否超时 return -1; else if((now.x==0)&&(now.y==B-1)&&(now.z==C-1))//判断是否到达目的地 return now.time_used; for(int i=0;i<6;i++){//分别将下一步能到达的地方找到 next.x=now.x+move[i].x; next.y=now.y+move[i].y; next.z=now.z+move[i].z; if(space[next.x][next.y][next.z]==1 ||next.x<0||next.y<0||next.z<0//判断是否出楼或者 ||next.x>A-1||next.y>B-1||next.z>C-1 ||vis[next.x][next.y][next.z]==1)//其他违反规则的事件 continue; else{ next.time_used=now.time_used+1;//下一步的时间计算 vis[next.x][next.y][next.z]=1;//标记走过的路 q.push(next);//将next加入队尾 } } } return -1; } int main() { int N; scanf("%d",&N); while(N--){ scanf("%d%d%d%d",&A,&B,&C,&T); int i,j,k; for(i=A-1;i>=0;i--) for(j=0;j<B;j++) for(k=0;k<C;k++) scanf("%d",&space[i][j][k]); int step=BFS(); printf("%d\n",step); //for( i=0;i<6;i++) // printf("%d %d %d\n",move[i].x,move[i].y,move[i].z); } return 0; }
代码测试数据能过。
标签:des style blog http io color os 使用 sp
原文地址:http://www.cnblogs.com/yanglingwell/p/4095613.html