HDU 1242 Dfs

Source: Internet
Author: User

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 Test Instructions: The Angels are trapped in prison, his friends want to see him, the prison's topography is complex, including the road (with dots), the wall (marked with a sign), the position of the angel (marked with a), his friend (with R), the prison is also guarded (by X), His friends can only go up and down in four directions, walk to do not spend a unit of time, if meet the guard, eliminate the guards need to spend an extra unit of time. Ask at least how long the angel can see his friend.

The point to note is that the angel's friends may be more than one, so, should be from the position of the angel to search for their friends on the OK.

 dfs procedure
#include <stdio.h> #include <iostream> #include <string.h> #include <string> #include < algorithm> #include <math.h> #include <cmath> #include <map> #include <set>using namespace STD; #define MAXN 500int hang,lie;int end_x,end_y;int begin_x,begin_y;int maxn;int q = 0;bool flag = False;char map[maxn][m        Axn];int Dir[4][2] = {{1,0}, { -1,0}, {0,1}, {0,-1}};void print () {for (int i = 0; i < hang; i++) {        for (int j = 0; J < Lie; J + +) {printf ("%c", Map[i][j]);    } printf ("\ n");        }}void dfs (int x,int y,int time) {if (map[x][y] = = ' r ') {flag = true;        if (Time < MAXN) {MAXN = time;    }} Map[x][y] = ' # ';        for (int i = 0; i < 4; i++) {int xx = x + dir[i][0];        int yy = y + dir[i][1];        if (xx >= 0 && xx < hang && yy >= 0 && yy < lie && map[xx][yy]! = ' # ')          {  if (map[xx][yy]! = ' # ') {if (map[xx][yy] = = ' x ') {DFS (xx,y                    Y,TIME+2);                MAP[XX][YY] = ' x ';                    } else if (map[xx][yy] = = ' R ') {DFS (xx,yy,time+1);                MAP[XX][YY] = ' R ';                    } else {DFS (xx,yy,time+1);                MAP[XX][YY] = '. ';        }}}}}int main () {while (Cin >> hang >> lie) {maxn = 214748364;        Flag=false;        for (int i = 0; i < hang; i++) {scanf ("%s", Map[i]);                } for (int i = 0, i < hang; i++) {for (int j = 0; J < Lie; J + +) {                printf ("%c", Map[i][j]);                    if (map[i][j] = = ' a ') {begin_x = i;                    begin_y = j;          MAP[I][J] = ' # ';      }}} dfs (begin_x,begin_y,0);        if (flag) {printf ("%d\n", MAXN);        } else {printf ("Poor ANGEL have to stay in the prison all his life.\n"); }    }}

  

HDU 1242 Dfs

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.