HDU 1072 (BFS) has a bomb

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=1072

The main idea is on a NXM map, 0 means the wall, 1 for the Open space, 2 for the person, 3 for the destination, and 4 for the time bomb reboot.

Time bombs are 6, and the time required to take one step is 1. You can move up, down, left, and right one cell at a time.

When the person goes to 4 o'clock if the bomb time is not 0, you can reset the bomb time to 6. If a man goes to 3 and the bomb is not 0 o'clock,

Success out. Begging for the shortest time from 2 to 3.

A typical search, using the BFS, the structure of a step to indicate the time required, it is equivalent to the number of steps, a time to represent the bomb,

Note that here in fact each point can be repeated access, in order to reach the minimum time, 4 of the location if you have to go, actually only walk once

A bomb point if go many times, although set the maximum time limit but the round-trip consumption is not worth the candle, think about here

Code

1#include <cstdio>2#include <queue>3#include <cstring>4 using namespacestd;5 structPoint {6     intx, y;7     inttime,step;//bomb time and number of steps8 };9 intdx[]={1,-1,0,0};Ten intdy[]={0,0,1,-1}; One intn,m; A intvisit[Ten][Ten],map[Ten][Ten]; - intBFsintSxintSy) - { the     inti; -memset (Visit,0,sizeof(visit)); -Queue<point>Q; - Point now,next; +now.x=sx;now.y=Sy; -Now.time=6; +now.step=0; Avisit[now.x][now.y]=1; at Q.push (now); -      while(!q.empty ()) -     { -now=Q.front (); - Q.pop (); -         if(map[now.x][now.y]==3) in             returnNow.step; -         if(now.time<=1)Continue; to          for(i=0;i<4; i++) +         { -next.x=now.x+Dx[i]; thenext.y=now.y+Dy[i]; *             if(next.x<1|| next.x>n| | next.y<1|| NEXT.Y&GT;M)Continue; $             if(map[next.x][next.y]==0)Continue;Panax Notoginseng             if(visit[next.x][next.y]==1)Continue; -next.step=now.step+1; thenext.time=now.time-1; +             if(map[next.x][next.y]==4) A             { theNext.time=6; +visit[next.x][next.y]=1;//Make sure to go only once, Mark -             } $ Q.push (next); $         } -     } -     return-1; the } - intMain ()Wuyi { the     intT,i,j,sx,sy; -      while(~SCANF ("%d",&t)) { Wu      while(t--) -     { Aboutscanf"%d%d",&n,&m); $          for(i=1; i<=n;i++) -         { -              for(j=1; j<=m;j++) -             { Ascanf"%d",&map[i][j]); +                 if(map[i][j]==2) the                 { -sx=i; $sy=J; the                 } the             } the         } theprintf"%d\n", BFS (Sx,sy)); -     } in     } the     return 0; the}

HDU 1072 (BFS) has a bomb

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.