hdu1253 Victory Grand Escape

Source: Internet
Author: User

The Great Escape of victory
Time limit:4000/2000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 29463 Accepted Submission (s): 11101


Problem Description
Ignatius was taken by the Devil, one day the devil on business trip, this is Ignatius escape good chance.

The Devil lives in a castle, the castle is a a*b*c cube, can be expressed as a b*c matrix, just beginning Ignatius was locked in (0,0,0) position, leaving the castle door in (a-1,b-1,c-1) position, now know that the devil will be in T minutes back to the castle, Ignatius can walk from one coordinate to one of the six adjacent coordinates per minute. Now give you the map of the castle, please calculate the Ignatius can leave the castle before the Devil return (as long as the exit to leave the castle, if go to the exit when the devil just came back also counted escape success), If you can please output how many minutes to leave, if not then output-1.

Input
The first line of the input data is a positive integer k, indicating the number of test data. The first line of the test data for each group is four positive integers a,b,c and T (1<=a,b,c<=50,1<=t<=1000), They represent the size of the castle and the time of the Devil's return. Then a block of input data (first NO. 0 block, then 1th block, 2nd block ...), each input data has B line, each line has a C positive integer, representing the layout of the maze, of which 0 represents the road, 1 represents the wall. (If the input description is not clear, you can refer to the Maze description in sample input, which represents the maze in)

Special note: The test data is very large, please use scanf input, I can not guarantee that using CIN can not time out. On this OJ, submit using Visual C + +.

Output
For each set of test data, if Ignatius can leave the castle before the Devil returns, then output-1 if the minimum number of minutes is required.

Sample Input
1
3 3 4 20
0 1 1 1
0 0 1 1
0 1 1 1
1 1 1 1
1 0 0 1
0 1 1 1
0 0 0 0
0 1 1 0
0 1 1 0

Sample Output
11

Author
Ignatius.l

Recommend
IGNATIUS.L | We have carefully selected several similar problems for you:1372 1072 1026 1548 1180

Deep Search Dfs
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;intN,m,l,t,ans;intv[ -][ -][ -];intmap[ -][ -][ -];intyi[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};voidDfsintXintYintZintTime ) { if(time>t)return;//Pruning if(M+n+l-x-y-z>t-time)return;//Pruning if(x==n-1&&y==m-1&&z==l-1) {ans=time;return ;} intNx,ny,nz; for(intI=0; i<6; i++) {NX=x+yi[i][0]; NY=y+yi[i][1]; NZ=z+yi[i][2]; if(nx>=0&&nx<n&&ny>=0&&ny<m&&nz>=0&&nz<l&&map[nx][ny][nz]==0&& (v[nx][ny][nz]==0|| TIME&LT;V[NX][NY][NZ]))//range must be written out { //cout<<nx<< "" <<ny<< "" <<nz<<endl;v[nx][ny][nz]=Time ; MAP[NX][NY][NZ]=1; DFS (Nx,ny,nz,time+1); //if (flag) return; //cout<<nx<< "<<ny<<" "<<nz<<" AAA "<<endl;map[nx][ny][nz]=0; } } /*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; Pruning map[rx][ry][rz]=1; DFS (RX,RY,RZ,TIME+1); map[rx][ry][rz]=0; } } */}intMain () {intc,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=-1; memset (V,0,sizeof(v)); DFS (0,0,0,0); if(ans==-1) printf ("-1\n"); Elseprintf"%d\n", ans); } return 0;}

Wide Search Better Write
#include <iostream> #include <cstdio> #include <cstring> #include <queue>using namespace std; int A,b,c,t;int Map[55][55][55];int dir[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};struct nodes{ int i,j,k,m;}; Nodes Node1,node2;int BFs () {queue<nodes> q; while (!q.empty ()) Q.pop (); node1.i=0; node1.j=0; node1.k=0; node1.m=0; Q.push (Node1); while (!q.empty ()) {Node1=q.front (); Q.pop (); if (node1.i==a-1&&node1.j==b-1&&node1.k==c-1) {//cout<<node1.m; return node1.m; } for (int i=0;i<6;i++) {node2.i=node1.i+dir[i][0]; NODE2.J=NODE1.J+DIR[I][1]; NODE2.K=NODE1.K+DIR[I][2]; if (node2.i>=0&&node2.i<a&&node2.j>=0&&node2.j<b&&node2.k>=0 &AMP;&AMP;NODE2.K&LT;C&AMP;&AMP;!MAP[NODE2.I][NODE2.J][NODE2.K]) { node2.m=node1.m+1; Map[node2.i][node2.j][node2.k]=1; Q.push (Node2); }}} return-1;} int main () {int n; scanf ("%d", &n); while (n--) {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", &map[i][j][k]); }}} Map[0][0][0]=1; int Ans=bfs (); COUT&LT;&LT;BFS () <<endl; if (ans>=0&&ans<=t) printf ("%d\n", ans); else printf (" -1\n"); }}

hdu1253 Victory Grand Escape

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.