ZOJ 649 Rescue (priority queue +bfs)

Source: Internet
Author: User

Rescue
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 521 Accepted Submission (s): 217
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.) 
Input First 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.  
Output for 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

Test instructions: Ask Angel friends in the shortest time to save Angels,a for Angels,R for Angel friends. Representing the road,# representing the wall,x for the creeps,

Every step takes a time, and a creep kills him. It takes a time to move to a soldier's position.

The shortest time to output,

If you can't save it, the output is "Poor ANGEL have to stay in the prison all of his life."

/*problem: Using priority queue +bfs This question has been wrong, the reason is ... Read in a group of data, I read only a set of*/#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<vector>using namespacestd;intf[202][202];Charmp[202][202];intN,m,sx,sy;structnode{intx, y; Node (intAintb) {x=a; y=b;}};intdr[4][2]={{1,0},{0,1},{-1,0},{0,-1} };structcmp{BOOL operator() (node A,node b) {returnF[a.x][a.y]>F[B.X][B.Y]; }};intBFs () {priority_queue<node,vector<node>,cmp>Q; Memset (F,0,sizeof(f));    Q.push (Node (sx,sy)); F[sx][sy]=1;  while(!Q.empty ()) {Node P=Q.top ();        Q.pop ();  for(intI=0;i<4; i++)        {            intxx=p.x+dr[i][0]; intyy=p.y+dr[i][1]; if(Xx>=n | | xx<0|| Yy>=m | | yy<0)Continue; if(mp[xx][yy]=='#'|| F[xx][yy]>0)Continue; F[XX][YY]=f[p.x][p.y]+1; if(mp[xx][yy]=='x') f[xx][yy]++;            Q.push (Node (xx,yy)); if(mp[xx][yy]=='R')returnf[xx][yy]-1; }    }    return-1;}intMain () { while(~SCANF ("%d%d",&n,&m)) { for(intI=0; i<n;i++) {scanf ("%s",&Mp[i]);  for(intj=0; j<m;j++)             if(mp[i][j]=='a') sx=i,sy=J; }        intans=BFS (); if(ans==-1) printf ("Poor ANGEL have to stay in the prison all his life.\n"); Elseprintf"%d\n", ans); }    return 0;}

ZOJ 649 Rescue (priority queue +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.