HDU 1253 Victory Grand Escape (BFS)

Source: Internet
Author: User
Tags define local

Topic links

problem DescriptionIgnatius 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.

InputThe 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 + +. OutputFor 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 Input13 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

The answer: looks very complex, in fact, is a maze, because asked is the minimum time required, so use BFS. A three-dimensional array is no different from a two-dimensional array, except that the dir array is open slightly differently.

#include <cstdio>#include<iostream>#include<string>#include<cstring>#include<stack>#include<queue>#include<algorithm>#include<cmath>#include<map>using namespacestd;//#define LOCALintmp[ -][ -][ -];BOOLvis[ -][ -][ -];intn,a,b,c,t;structnode{Node (intAintBintCintd) {x=a,y=b,z=c,time=D;} intx, Y, Z intTime ;};intdir[6][3]={{1,0,0},{-1,0,0},{0,0,1},{0,0,-1},{0,1,0},{0,-1,0}};BOOLCheckintXintYintz) {    if(x>=0&&y>=0&&z>=0&&x<a&&y<b&&z<c&&mp[x][y][z]==0)    return 1; return 0;}intBFsintt) {Node St (0,0,0,0), Ed (0,0,0,0); Queue<node>Q;  while(!q.empty ()) Q.pop (); memset (Vis,0,sizeofvis); vis[0][0][0]=1;    Q.push (ST);  while(!Q.empty ()) {St=Q.front ();        Q.pop (); if(st.time>t)return-1; if(st.x==a-1&&st.y==b-1&&st.z==c-1&AMP;&AMP;ST.TIME&LT;=T)returnSt.time;  for(intI=0;i<6; i++) {ed.x=st.x+dir[i][0]; Ed.y=st.y+dir[i][1]; Ed.z=st.z+dir[i][2]; if(Check (ED.X,ED.Y,ED.Z) &&!Vis[ed.x][ed.y][ed.z]) {Vis[ed.x][ed.y][ed.z]=true; Ed.time=st.time+1; if(ABS (ed.x-a+1) +abs (ed.y-b+1) +abs (ed.z-c+1) +ed.time>t)Continue;            Q.push (ed); }        }    }    return-1;}intMain () {#ifdef LOCAL freopen ("In.txt","R", stdin);#endif //LOCAL    //Startscanf"%d",&N);  while(n--) {scanf ("%d%d%d%d",&a,&b,&c,&T); Memset (MP,0,sizeofMP);  for(intI=0; i<a; i++)         for(intj=0; j<b; J + +)         for(intk=0; k<c; k++) scanf ("%d",&Mp[i][j][k]); printf ("%d\n", BFS (T)); }    return 0;}

HDU 1253 Victory Grand Escape (BFS)

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.