标签:des blog http ar io os 使用 sp for
Description

Input
Output
Sample Input
Sample Output
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<vector>
#include<set>
using namespace std;
#define INF 1<<30
int t,a,b,c,T,aa[55][55][55],ans;
int dic[6][3]={{0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0}};
bool vis[55][55][55],flag;
struct line
{
int x,y,z,step;
}e,st;
queue<line> q;
bool check(int x,int y,int z)
{
if(x<0||x>=a||y<0||y>=b||z<0||z>=c||aa[x][y][z]||vis[x][y][z])
return false;
return true;
}
int bfs()
{
while(!q.empty())
q.pop();
int x,y,z,step;
e.x=0,e.y=0,e.z=0,e.step=0;
q.push(e);
while(!q.empty())
{
st=q.front();
q.pop();
for(int i=0;i<6;i++)
{
e.x=st.x+dic[i][0];
e.y=st.y+dic[i][1];
e.z=st.z+dic[i][2];
if(check(e.x,e.y,e.z))
{
e.step=st.step+1;
if(abs(e.x-a+1)+abs(e.y-b+1)+abs(e.z-c+1)+e.step>T)
continue;
if(e.x==a-1&&e.y==b-1&&e.z==c-1)
return e.step;
vis[e.x][e.y][e.z]=1;
q.push(e);
}
}
}
return -1;
}
int main()
{
scanf("%d",&t);
while(t--)
{
ans=INF,flag=false;
memset(vis,0,sizeof(vis));
scanf("%d%d%d%d",&a,&b,&c,&T);
for(int i=0;i<a;i++)
for(int j=0;j<b;j++)
for(int k=0;k<c;k++)
scanf("%d",&aa[i][j][k]);
vis[0][0][0]=1;
printf("%d\n",bfs());
}
return 0;
}
标签:des blog http ar io os 使用 sp for
原文地址:http://www.cnblogs.com/a972290869/p/4166240.html