hdu1242 Rescue (BFS)

Source: Internet
Author: User

RescueTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 21701 Accepted Submission (s): 7745


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 Input
7 8#.#####.#.a#. r.#. #x ..... #.##...##...#..............

Sample Output
13

Authorchen, Xue
Sourcezoj Monthly, October 2003
Recommendeddy | We have carefully selected several similar problems for you:1240 1010 1072 1253 1372
Statistic | Submit | Discuss | Note

You can also use DFS to do the BFS here.

#include <stdio.h> #include <string.h> #include <queue>using namespace std;struct node{int cost,x,y; friend bool operator< (node A,node b) {return a.cost>b.cost;}}; int Dir[4][2]={0,1,0,-1,1,0,-1,0};char map[205][205];int vis[205][205],n,m;bool limit (int x,int y) {if (map[x][y]!= ' # ' &AMP;&AMP;X&GT;=0&AMP;&AMP;X&LT;N&AMP;&AMP;Y&GT;=0&AMP;&AMP;Y&LT;M) return True;elsereturn false;} int tonum (int x,int y) {if (map[x][y]== ' x ') return 2;elsereturn 1;} void BFs (int x,int y) {priority_queue<node>s;node temp,temp1;int Flag=0;temp.x=x,temp.y=y,temp.cost=0;s.push ( temp); while (!s.empty ()) {temp1=temp=s.top (); S.pop (); if (map[temp.x][temp.y]== ' a ') {flag=1;break;} for (int i=0;i<4;i++) {temp.x=temp.x+dir[i][0];temp.y=temp.y+dir[i][1];if (!vis[temp.x][temp.y]&&limit ( TEMP.X,TEMP.Y) {vis[temp.x][temp.y]=1;temp.cost+=tonum (TEMP.X,TEMP.Y); S.push (temp);} TEMP=TEMP1;}} if (flag) printf ("%d\n", Temp.cost); elseprintf ("Poor ANGEL have to stay in the prison all he life.\n"); while (!s.empty ()) S.pop ();} int main () {int st_x,st_y;while (scanf ("%d%d", &n,&m)!=eof) {for (int i=0;i<n;i++) scanf ('%s ', map[i]); for ( int i=0;i<n;i++) for (int j=0;j<m;j++) {if (map[i][j]== ' R ') {St_x=i,st_y=j;break;}} memset (vis,0,sizeof (Vis)); Vis[st_x][st_y]=1;bfs (st_x,st_y);} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdu1242 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.