HDU 1253 victory

Source: Internet
Author: User

HDU 1253 victory
VictoryTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 27587 Accepted Submission (s): 10508


Problem DescriptionIgnatius was taken away by the devil. One day the devil went on a business trip, which was a good opportunity for Ignatius to escape.

The Devil lives in A castle. The castle is A cube of A * B * C, which can be expressed as A matrix of B * C. Ignatius was shut down at the beginning (0, 0, 0) location, exit the castle door in (A-1, B-1, C-1) location, now know that the devil will return to the castle in T minutes, ignatius can go from one coordinate to one of the six adjacent coordinates every minute. now, let's show you the map of the castle. Can you calculate whether Ignatius can leave the castle before the devil returns, if the devil just came back at the exit, it would be a success). If you can, please output how many minutes to leave. If not, output-1.



The first line of Input data is a positive integer K, indicating the number of test data. the first row of each group of test data 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 when the devil came back. then there is A piece of input data (first 0th, then 1st, 2nd ......), each input data has B rows, and each row has C positive integers, representing the layout of the maze. 0 represents the path, and 1 represents the wall. (if you are not clear about the Input description, refer to the maze description in Sample Input, which indicates the maze)

Note: The test data in this question is very large. Use scanf to input the data. I cannot guarantee that the use of cin will not time out. Use Visual C ++ to submit the data on this OJ.

Output for each group of test data, if Ignatius can leave the castle before the devil returns, please Output how many minutes it takes; otherwise, Output-1.

Sample Input
13 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 Output
11

AuthorIgnatius. L

A 3D wide search question. At the beginning, I wrote a DFS and decided not to time out .. Later, the anger was changed to BFS. C ++ AC... G ++ still times out...
# Include
 
  
# Include
  
   
# Include
   
    
# Include using namespace std; int vis [52] [52] [52]; int map [52] [52] [52]; int dir [6] [3] = {0, 1}, {0,-1}, {0, 0}, {0, 0 },{-, 0 }}; // three-dimensional direction, int k, n, m; int ans; struct node {int z1, x1, y1; int time ;}; bool check (int z1, int x1, int y1) {if (z1> = k | z1 <0 | x1> = n | x1 <0 | y1> = m | y1 <0 | vis [z1] [x1] [y1] | map [z1] [x1] [y1] = 1) // return 0; return 1;} int bfs () {int I; queue
    
     
Q; node st, ed; st. x1 = 0; st. y1 = 0; st. z1 = 0; st. time = 0; q. push (st); while (! Q. empty () {st = q. front (); q. pop (); if (st. x1 = n-1 & st. y1 = M-1 & st. z1 = k-1) return st. time; // escape for (I = 0; I <6; I ++) {ed. x1 = st. x1 + dir [I] [2]; ed. y1 = st. y1 + dir [I] [1]; ed. z1 = st. z1 + dir [I] [0]; if (! Check (ed. z1, ed. x1, ed. y1) continue; ed. time = st. time + 1; vis [ed. z1] [ed. x1] [ed. y1] = 1; q. push (ed) ;}} return 0 ;}int main () {int t, Time; int z, x, y; scanf ("% d", & t ); while (t --) {memset (vis, 0, sizeof (vis); scanf ("% d", & k, & n, & m, & Time); for (z = 0; z
     
      

Code for DFS Timeout:
#include 
       
        #include 
        
         #include using namespace std;int vis[52][52][52];int map[52][52][52];int dir[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};int k,n,m;int z,x,y;int ans;bool check(int z1,int x1,int y1){    if(z1>=k ||z1<0 ||x1>=n ||x1<0 ||y1>=m ||y1<0 ||vis[z1][x1][y1] ||map[z1][x1][y1]==1)         return 0;    return 1;}void dfs(int z,int x,int y,int time){    int sz,sx,sy,i;    if(z==k-1 &&x==n-1 &&y==m-1)    {        ans=time;        return;    }    for(i=0;i<6;i++)    {        sz=z+dir[i][0];        sx=x+dir[i][2];        sy=y+dir[i][1];        if(!check(sz,sx,sy))            continue;        vis[sz][sx][sy]=1;        time+=1;        dfs(sz,sx,sy,time);        vis[sz][sx][sy]=0;    }    return;}int main(){    int t,Time;    scanf("%d",&t);    while(t--)    {        memset(vis,0,sizeof(vis));        scanf("%d%d%d%d",&k,&n,&m,&Time);        for(z=0;z
         
          

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.