HDU 1242 Rescue (BFS)

Source: Internet
Author: User
Tags define local

Topic links

problem DescriptionAngel was caught by the moligpy! He was put into prison by Moligpy. The prison is described as a n * M (n, M <=) matrix. There is WALLs, ROADs, and guards in the prison.

Angel ' s friends want to save Angel. Their task Is:approach Angel. We assume that "approach Angel" are to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or his?) to move into the grid. We assume that we moving up, down, right, left takes US 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

You had to calculate the minimal time to approach Angel. (We can move only up, down, left and right, to the neighbor grid within bound, of course.) InputFirst line contains-integers stand for N and M.

Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "R" stands for each of the Angel ' s friend.

Process to the end of the file. OutputFor each test case, your program should output a single integer, standing for the minimal time needed. If Such a number does no exist, you should output a line containing "Poor ANGEL have to stay in the prison all he life." Sample Input7 8#.#####.#.a#. r.#. #x ..... #.##...##...#.............. Sample Output13: A is the target location, R is the starting position, through x takes more than 1s time, if not go to the output "Poor ANGEL have to stay in the prison all he life."
#include <cstdio>#include<iostream>#include<string>#include<sstream>#include<cstring>#include<stack>#include<queue>#include<algorithm>#include<cmath>#include<map>#definePI ACOs (-1.0)#defineMS (a) memset (A,0,sizeof (a))#defineMSP Memset (Mp,0,sizeof (MP))#defineMSV memset (vis,0,sizeof (VIS))using namespacestd;//#define LOCALintn,m;Charmp[ About][ About];intdir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};intEx,ey;intvis[ About][ About];structnode{intx, y; intTime ;} CP;intBFs () {Queue<Node>Q;  while(!q.empty ()) Q.pop ();    Q.push (CP);  while(!Q.empty ()) {CP=Q.front (), Q.pop (); if(Cp.x==ex&&cp.y==ey)returnCp.time; VIS[CP.X][CP.Y]=1;  for(intI=0; i<4; i++) {Node np; Np.x=cp.x+dir[i][0]; NP.Y=cp.y+dir[i][1]; Np.time=cp.time+1; if(mp[np.x][np.y]=='#')Continue; if(Vis[np.x][np.y])Continue; if(np.x<0|| np.y<0|| np.x>=n| | NP.Y&GT;=M)Continue; if(mp[np.x][np.y]=='x') np.time++;        Q.push (NP); }    }    return-1;}intMain () {#ifdef LOCAL freopen ("In.txt","R", stdin);#endif //LOCALIos::sync_with_stdio (false);  while(cin>>n>>m) {msp,msv;  for(intI=0; i<n; i++) cin>>Mp[i];  for(intI=0; i<n; i++)             for(intj=0; j<m; J + +)            {                if(mp[i][j]=='a') ex=i,ey=J; Else if(mp[i][j]=='R') cp.x=i,cp.y=J; } cp.time=0; VIS[CP.X][CP.Y]=1; intans=BFS (); if(ans==-1) printf ("Poor ANGEL have to stay in the prison all his life.\n"); Elseprintf"%d\n", ans); }    return 0;}

HDU 1242 Rescue (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.