HDU 1010 Tempter of the Bone

Source: Internet
Author: User

The problem is a deep search of the typical problem, in time t accurate to reach the end, processing bad either WA or TLE. There are two very important pruning: first, the odd and even pruning, must be in advance to determine the good ' S ' and ' D ' between the Manhattan distance and time t is the same parity, otherwise it will TLE, and the second is to judge '. ' Whether the number is greater than or equal to t-1, this can make the program much faster, but not necessary, I tried to remove it after or can It's just a few more times.

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5typedefConst int&cintr;6 intN,m,t,ei,ej;7 8InlineintDist (cintr x, cintr y, cintr x2, Cintr y2) {9     returnABS (X-X2) +abs (yy2);Ten } One  A BOOLFlag; - Charmap[Ten][Ten]; - intdir[2][4]= {{-1,0,0,1},{0,-1,1,0}}; the  - voidDfsintIintJintTime ) { -     if(flag)return ; -     if(I==ei && J==ej && time==t) { +flag=true; -         return ; +     } A     inttmp=Dist (I,J,EI,EJ); at     //no more odd-and-even pruning judgment, in the main function has been judged good, here only to make time to judge -     if(Time+tmp > t/*|| (tmp+t-time) &1*/)return ; -      for(intk=0; k<4; ++k) { -Cintr di= i+dir[0][k], dj= j+dir[1][k]; -         CharCh=MAP[DI][DJ]; -         if(map[di][dj]!='X'){ inmap[di][dj]='X'; -DFS (di,dj,time+1); tomap[di][dj]=ch; +         } -     } the } *  $ intMain () {Panax Notoginseng     intI,J,SI,SJ; -      while(~SCANF ("%d%d%d",&n,&m,&t), N) { thememset (Map,'X',sizeof(map)); +flag=false; A         intPoint=0; the GetChar (); +          for(i=1; i<=n; ++I,getchar ()) -              for(j=1; j<=m; ++j) $                 if((map[i][j]= getchar ()) = ='S') si= I, sj=J; $                 Else if(map[i][j]=='D') ei= I, ej=J; -                 Else if(map[i][j]=='.')      ++Point ; -         inttmp=Dist (SI,SJ,EI,EJ); the         //two important pruning, using point to make the first judgment (greatly improving the efficiency), -         //and then the odd-even pruning, just one cut.Wuyi         if(Point < T1|| (tmp+t) &1) { thePuts"NO"); -             Continue; Wu         } -map[si][sj]='X'; AboutDFS (SI,SJ,0); $Puts (flag?)"YES":"NO"); -     } -     return 0; -}

HDU 1010 Tempter of the Bone

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.