Hdoj 1424 Rescue "BFS"

Source: Internet
Author: User

RescueTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) Total Submission (s): 18493 Accepted Submission (s): 6606

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

Test instructions: Give you a picture, ' # ' stands for the wall, '. ' On behalf of the road, ' X ' stands for soldiers. ' A ' stands for the end, ' R ' is the starting point, move up and down, each move one step time plus 1, meet the soldier to add 1, ask for the least

Time.

Bfs+priority_queue

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cstdlib >const int M = 205;const int dx[] = {0, 0, 1, -1};const int dy[] = {1,-1, 0, 0};using namespace Std;char Map[m][m];boo    L vis[m][m];int N, m;struct node{int step;    int x, y;    BOOL operator < (const node & a) const {return step > a.step; }};node St, En;bool Limit (node s) {return (s.x >=0 && s.x < n&& s.y >= 0&& S.y < m& amp;& Map[s.x][s.y]! = ' # ');}    BOOL BFs () {memset (Vis, 0, sizeof (VIS));    VIS[ST.X][ST.Y] = 1;    Priority_queue<node >q;    Q.push (ST);        while (!q.empty ()) {Node temp = q.top ();        Q.pop ();            for (int i = 0; i < 4; + + i) {node cur = temp; Cur.x + = Dx[i];            Cur.y + = Dy[i];            if (cur.x = = en.x&& Cur.y = = en.y) {printf ("%d\n", cur.step+1); return 1; } if (!vis[cur.x][cur.y]&&limit (cur) {VIS[CUR.X][CUR.Y] = 1; if (map[cur.x][cur.y]! = '. ')                {Cur.step + = 2;                } else Cur.step + +;            Q.push (cur); }}} return 0;}  int main () {while (scanf ("%d%d", &n, &m) = = 2) {for (int i = 0; i < n; + + i) {//cin >>            Map[i];            scanf ("%s", Map[i]);            for (int j = 0; J < m; + + j) if (map[i][j] = = ' a ') {en.x = i; en.y = j;            } else if (map[i][j] = = ' R ') {st.x = i; st.y = j; st.step = 0;        }} BOOL flag = BFS ();    if (!flag) printf ("Poor ANGEL have to stay in the prison all he life.\n");  } return 0;}


Hdoj 1424 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.