标签:
Description
Input
Output
Sample Input
Sample Output
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n,m,l,t,ans; int v[55][55][55]; int map[55][55][55]; int yi[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}}; void dfs(int x,int y,int z,int time) { if (time>t||time>=ans) return ; if (m+n+l-x-y-z>t-time) return ; if (x==n-1&&y==m-1&&z==l-1) {ans=time;return ;} int rx,ry,rz,i; for (i=0;i<6;i++) { rx=x+yi[i][0]; ry=y+yi[i][1]; rz=z+yi[i][2]; if (rx>=0&&rx<n&&ry>=0&&ry<m&&rz>=0&&rz<l&&map[rx][ry][rz]!=1&&(v[rx][ry][rz]==0||time<v[rx][ry][rz])) { v[rx][ry][rz]=time; map[rx][ry][rz]=1; dfs(rx,ry,rz,time+1); map[rx][ry][rz]=0; } } } int main() { int c,i,j,k; scanf("%d",&c); while (c--) { scanf("%d%d%d%d",&n,&m,&l,&t); for (i=0;i<n;i++) for (j=0;j<m;j++) for (k=0;k<l;k++) scanf("%d",&map[i][j][k]); ans=1001; memset(v,0,sizeof(v)); dfs(0,0,0,0); if (ans==1001) printf("-1\n"); else printf("%d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/pblr/p/4698841.html