HDU Search Practice Rescue

Source: Internet
Author: User

RescueTime limit:2000/1000ms (java/other) Memory limit:65536/32768k (Java/other) total submission (s): Accepted SUBM Ission (s): 13Problem Descriptionangel was caught by the moligpy! He was put into prison by Moligpy. The prison is described as a n * M (n, M &lt;=) matrix. There is WALLs, ROADs, and guards in the Prison.<br><br>angel ' s friends want to save Angel. Their task Is:approach Angel. We assume that &quot;approach angel&quot; IS-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.<br><br>you has to calculate the minimal time to approach Ang El. (We can move only up, down, left and right, to the neighbor grid within bound, of course.) <br>

Inputfirst line contains-integers stand for N and M.<br><br>then N lines follows, every line have M charact ERs. "." stands for road, "a" stands for Angel, and "R" stands for each of the Angel ' s friend. <br><br>process to the end of the file.<br>

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 his life." & Lt;br>

Sample Input7 8#.#####.#.a#. r.#. #x ..... #.##...##...#.............. Sample Output13 Simple test Instructions: The solution R arrives whether can reach a place, can, at least a few steps of thinking analysis: As with other topics, there is a target BFS
# include <iostream># include<cstring># include<queue># include<fstream>using namespacestd;structinfo{intx, y;} A, R;intN, M;Charmap[ -][ -];intdir[4][2] = {{-1,0},{1,0},{0, -1},{0,1},};intjishu[ -][ -];BOOLBorderintXinty) {    if(x >=1&& x <= n && y >=1&& y <=m)return true; return false;}intBFS ();intMain () {//fstream cin ("Aaa.txt");     while(Cin >> N >>m) { for(inti =1; I <= N; i++)        {             for(intj =1; J <= M; J + +) {cin>>Map[i][j]; if(Map[i][j] = ='a') {a.x=i; A.Y=J; }                if(Map[i][j] = ='R') {r.x=i; R.y=J; }            }        }        inthaha =BFS (); if(haha = =88888888) cout<<"Poor ANGEL have to stay in the prison all he life."<<Endl; Elsecout<< haha <<Endl; }    return 0;}intBFs () { for(inti =0; I < -; i++)     for(intj =0; J < -; J + +) Jishu[i][j]=88888888; JISHU[R.X][R.Y]=0; Queue<Info>Q;    Q.push (R);    Info T, Tep;  while(!Q.empty ()) {Tep=Q.front ();        Q.pop (); if(tep.x = = a.x && Tep.y = =a.y) { Break; }         for(inti =0; I <4; i++) {T.x= tep.x + dir[i][0]; T.y= Tep.y + dir[i][1]; if(!border (T.x, T.Y))//Out of Bounds, past, stone wall            Continue; if(Map[t.x][t.y] = ='#')            Continue; intmin; if(Map[t.x][t.y] = ='x') min= Jishu[tep.x][tep.y] +2; Elsemin= Jishu[tep.x][tep.y] +1; if(Min >=Jishu[t.x][t.y])Continue; JISHU[T.X][T.Y]=min; //cout << Jishu[t.x][t.y] << Endl;Q.push (t); }    }    returnjishu[a.x][a.y];}

HDU Search Practice Rescue

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.