HDU1010 dfs+ Pruning

Source: Internet
Author: User

Tempter of the Bone

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 110290 Accepted Submission (s): 29967


Problem DescriptionThe Doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone is a trap, and he tried desperately to get out of this maze.

The maze is a rectangle with sizes N by M. There is a door in the maze. At the beginning, the door is closed and it would open at the t-th second for a short period of time (less than 1 second) . Therefore the doggie had to arrive in the door on exactly the t-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once He entered a block, the ground of this block would start-to-sink and disappear in the next second. He could not stay on one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.

Inputthe input consists of multiple test cases. The first line of all test case contains three integers n, m, and T (1 < N, m < 7; 0 < T <), which denote The sizes of the maze and the time at which the door would open, respectively. The next N lines give the maze layout, with all line containing M characters. A character is one of the following:

' X ': a block of wall, which the doggie cannot enter;
' S ': The start point of the doggie;
' D ': the Door; Or
'. ': an empty block.

The input is terminated with three 0 ' s. This test is a not-to-be processed.

Outputfor each test case, print on one line "YES" if the doggie can survive, or "NO" otherwise.

Sample Input4 4 5S. X... X... Xd.... 3 4 5S. X... X.... D0 0 0

Sample Outputnoyes

Authorzhang, Zheng

SOURCE ZJCPC2004 Test Instructions: Whether the S point in the map passes the T time to reach the D point exactly; code:
1 //obviously a Dfs, but the normal DFS will time out, read the puzzle to understand the original can be the odd and even pruning. The distance from the starting point to the end point if it is odd t is also odd to arrive,2 //the distance from the starting point to the end point can be reached if the even T is an even number. The shortest distance from the starting point to the end point is the difference between the coordinates of two points, and if we do not take this short circuit, only the number of steps is even.3 //steps to reach the end point. So many things can be ruled out. You can draw your own picture to see. 4 //If you can walk less than T on the map. 5#include <iostream>6#include <cstdio>7#include <cstring>8#include <cmath>9 using namespacestd;Ten intn,m,t; One intAns,tim; A intdir[4][2]={1,0,-1,0,0,1,0,-1}; - BOOLvis[Ten][Ten]; - Charmap[Ten][Ten]; the voidDfsintSxintSy) - { -     if(tim>t) -     return; +     if(map[sx][sy]=='D') -     { +         if(tim==t) Aans=1; at         return; -     } -      for(intI=0;i<4; i++) -     { -         intx=sx+dir[i][0],y=sy+dir[i][1]; -         if(x<0|| x>=n| | y<0|| Y&GT;=M)Continue; in         if(Vis[x][y])Continue; -         if(map[x][y]=='X')Continue; tovis[x][y]=1; +tim++; - dfs (x, y); thevis[x][y]=0; *tim--; $         if(ans==1)return;Panax Notoginseng     } - } the intMain () + { A     intSx,sy,ex,ey; the      while(SCANF ("%d%d%d", &n,&m,&t)! =EOF) +     { -         if(n==0&&m==0&&t==0) Break; $         intCnt=0; $          for(intI=0; i<n;i++) -         { -scanf"%s", Map[i]); the              for(intj=0; j<m;j++){ -             if(map[i][j]=='S')Wuyi             { thesx=i;sy=J; -             } Wu             if(map[i][j]=='D') -             { Aboutex=i;ey=J; $             } -             if(map[i][j]=='.') -cnt++; -             } A         } +         intTem1=fabs (sx+sy-ex-ey); theans=0; -         if(tem1%2==t%2&&cnt+1>=t) { $memset (Vis,0,sizeof(Vis)); thevis[sx][sy]=1; thetim=0; the DFS (sx,sy); the         } -         if(ans==1) printf ("yes\n"); in         Elseprintf"no\n"); the     } the     return 0; About}

HDU1010 dfs+ Pruning

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.