模板:HDU 1242: Rescue: (BFS DFS)

来源:互联网 发布:威海近年来人口数据 编辑:程序博客网 时间:2024/04/25 17:07

上题目

Problem Description
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

Angel’s friends want to save Angel. Their task is: approach Angel. We
assume that “approach Angel” is to get to the position where Angel
stays. When there’s a guard in the grid, we must kill him (or her?) 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 have 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 two 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 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 has to stay
in the prison all his life.”

Sample Input 7 8

.#####.

.a#..r.

..#x… ..#..#.

…##.. .#…… ……..

Sample Output 13

翻译

a代表angle x是守卫 r是angle的朋友 (r有多个)

每次经过x的时候都将加时1秒

问angle的朋友到达angle的最短的距离

分析

这道题不妨倒着推

angle到达朋友的最短距离

因为朋友有很多个对每个进行搜索较费时间

而反相之后 只需要从angle开始搜寻到‘r’结束

另外求最短路径的标准做法是BFS

这里附上 DFS和BFS的两种解法

代码+ 注释

标准的bfs和dfs模板

/*DFS*/#include<stdio.h>  #include<string.h>  #define max 205  char map[max][max];long a[100000], step, sum, n, m, visited[max][max];long directions[4][2] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };// 此函数基本思想:// 1.让天使上下左右随便走但是墙和重复的道路排除// 2.dierctions纪录方向 有for循环代表所有方向// 3.visited数组来标记走过的路// 4.在dfs全部循环完毕之后 一步步返回的时候必须清空 23 两步的所有标记void DFS(int x, int y){    int i, mx, my;    if (map[x][y] == 'r')        a[sum++] = step;    else if (map[x][y] != '#')    {        for (i = 0; i < 4; i++)        {            mx = x + directions[i][0];            my = y + directions[i][1];            if (map[mx][my] != '#'&&mx >= 1 && mx <= n&&my >= 1 && my <= m&&!visited[mx][my])            {                if (map[x][y] == 'x')                    step++;                step++;                visited[mx][my] = 1;                DFS(mx, my);                visited[mx][my] = 0;                step--;                if (map[x][y] == 'x')                    step--;            }        }    }}int main(){    long i, j, x, y, min;    while (scanf_s("%ld%ld", &n, &m) > 0)    {        memset(visited, 0, sizeof(visited));        sum = 0;        step = 0;        min = max;        for (i = 1; i <= n; i++)        {            getchar();            for (j = 1; j <= m; j++)            {                scanf_s("%c", &map[i][j]);                if (map[i][j] == 'a')                {                    x = i;                    y = j;                }            }        }        visited[x][y] = 1;        DFS(x, y);        if (sum == 0)            printf("Poor ANGEL has to stay in the prison all his life.\n");        else        {            for (i = 0; i < sum; i++)                if (a[i] < min)                    min = a[i];            printf("%ld\n", min);        }    }    return 0;}/*bfs*//*#include<cstdio>#include<cstring>#include<queue>using namespace std;int dir[4][2] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };char map[205][205];int vis[205][205];int n, m, flag, sum, ex, ey, ez, minn;struct node{    int x;    int y;    int count;};node q;void bfs(node p){    node s, t;    queue<node>a;    a.push(p);    while (!a.empty())    {        s = a.front();        a.pop;        if (s.x < 1 || s.x > n || s.y < 1 || s.y  > m)            continue;        if (map[s.x][s.y] == '#')            continue;        if (map[s.x][s.y] == 'r')        {            minn = s.count;            flag = 1;            return;        }        if (vis[s.x][s.y] == 1)            continue;        vis[s.x][s.y] = 1;        for (int i = 0; i < 4; i++)        {            t.x = s.x + dir[i][0];            t.y = s.y + dir[i][1];            if (map[t.x][t.y] == 'x')                t.count = s.count + 2;            else                t.count = s.count + 1;            bfs(t);        }    }}int main(void){    while (scanf_s("%d %d", &n, &m) != EOF)    {        getchar();        flag = 0;        minn = 0;        memset(vis, 0, sizeof(vis));        for (int i = 1; i <= n; i++)        {            for (int j = 1; j <= m; j++)            {                scanf_s("%c", &map[i][j]);                if (map[i][j] == 'a')                {                    q.x = i;                    q.y = j;                }                if (map[i][j] == 'r')                {                    ex = i;                    ey = j;                }            }            getchar();        }        vis[q.x][q.y] = 1;        q.count = 0;        bfs(q);        if (flag == 0)            printf("Poor ANGEL has to stay in the prison all his life.\n");        else            printf("%d", minn);    }    return 0;}*/
0 0
原创粉丝点击