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标示),他的朋友(用r标示),监狱里还有守卫(用x标示),他的朋友只能向左右上下四个方向走,走以不花一单位时间,若碰上守卫,消灭守卫需要额外花费一单位时间。问最少多长时间天使能见到他的朋友。

本题需要注意的是,天使的朋友可能不只一个,所以,应该从天使的位置开始搜去找其朋友就ok了。

 dfs做法
#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 500

int hang,lie;
int end_x,end_y;
int begin_x,begin_y;
int maxn;
int q = 0;
bool flag = false;
char MAP[Maxn][Maxn];
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,yy,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 has to stay in the prison all his life.\n");
        }
    }
}

接下来是bfs做法,记得要标记,要不然就死循环了

#include <stdio.h>
#include <iostream>
#include <queue>
#include <string>
#include <string.h>
using namespace std;

#define Maxn 1000

struct Node
{
	int x;
	int y;
	int step;
};

char MAP[Maxn][Maxn];
int begin_x,begin_y;
int hang,lie;
bool flag;
int maxn;
bool visit[Maxn][Maxn];
int dir[4][2] = {
	{0,1},
	{0,-1},
	{1,0},
	{-1,0}
};

void bfs(int x,int y)
{
	memset(visit,false,sizeof(visit));
	queue <Node> q;
	Node a,b,c;
	a.x = x;
	a.y = y;
	visit[x][y] = true;
	a.step = 0;
	q.push(a);
	while(!q.empty())
	{
		b = q.front();
		q.pop();
		if (MAP[b.x][b.y] == 'r')
		{
			if (maxn > b.step)
			{
				maxn = b.step;
			}
			flag = true;
		}
		for(int i = 0; i < 4; i++)
		{
			c.x = b.x + dir[i][0];
			c.y = b.y + dir[i][1];
			if (c.x >= 0 && c.x < hang && c.y >= 0 && c.y < lie && MAP[c.x][c.y] != '#' && !visit[c.x][c.y])
			{
				visit[b.x][b.y] = true;
				if (MAP[c.x][c.y] == 'x')
				{
					c.step = b.step + 2;
				}
				else
				{
					c.step = b.step + 1;
				}
				q.push(c);
			}
		}
	}
}

int main()
{
    while(scanf("%d%d",&hang,&lie)!=EOF&&(hang+lie))
    {
		flag = false;
		maxn = 2147483646;
		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++)
			{
				if (MAP[i][j] == 'a')
				{
					begin_x = i;
					begin_y = j;
				}
			}
		}
		bfs(begin_x,begin_y);
		if (flag)
		{
			printf("%d\n",maxn);
		}
		else
		{
			printf("Poor ANGEL has to stay in the prison all his life.\n");
		}
    }
}

  

  

hdu 1242 dfs/bfs的更多相关文章

  1. HDU 4771 (DFS+BFS)

    Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...

  2. HDU 1242 Rescue(BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...

  3. HDU 1242 Rescue(BFS),ZOJ 1649

    题目链接 ZOJ链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The ...

  4. hdu 1241(DFS/BFS)

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  5. HDU 1242 dFS 找目标最短路

    //多个起点,要最短得目标,不妨倒过来从目标出发,去找最近的点更新!!!!!!递归时思路要清楚 #include<iostream> #include<cstring> usi ...

  6. hdu 1242 Rescue (BFS)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  7. hdu 1242 Rescue

    题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...

  8. ID(dfs+bfs)-hdu-4127-Flood-it!

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...

  9. DFS/BFS+思维 HDOJ 5325 Crazy Bobo

    题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...

随机推荐

  1. jQuery 遍历过滤

    缩写搜索元素的范围 三个最基本的过滤方法是:first(), last() 和 eq(),它们允许您基于其在一组元素中的位置来选择一个特定的元素. 其他过滤方法,比如 filter() 和 not() ...

  2. zoom与transform:scale的区别

    一. zoom特性 1. zoom是IE的私有属性,但目前除Firefox不支持外,其他浏览器支持尚好. 2.定义: zoom即变焦,可改变元素尺寸,属于真实尺寸.zoom:百分值/数值/normal ...

  3. Input的readonly 属性与disabled属性

    readonly 不可编辑,可以获得焦点,背景颜色默认灰色,值的字体颜色默认为灰色,值可以在请求中传递 disabled 不可编辑,不可以获得焦点,背景颜色默认灰色,值的字体颜色默认为灰色,值不可以在 ...

  4. 解决打不开jar包

    Java应用程序jar文件可以由 JVM(Java虚拟机)直接执行,只要操作系统安装了JVM便可以运行作为Java应用程序的jar文件,其跨平台特性使得很多工具软件都用jar方式来部署分发,比如用于H ...

  5. 趣味C程序100.9 绘制杨辉三角

    说明:1.本问题来源于<C语言经典.趣味.实用程序设计编程百例精解>,所有程序为本人自己编写.与原程序不同之处作有标记. 2.本系列所有程序均使用codeblocks编译,操作系统为Win ...

  6. 《Braid》碎片式台词

    谁见到过风? 你没有,我也没有. 但当树儿低下头, 便是风儿经过时. 便是风儿穿过的时候. 但当树叶微微摇首, 你没有,我也没有. 谁见到过风? 二.时间与宽恕 1.提姆要出发了!他要去寻找并救出公主 ...

  7. [BZOJ 3218] A + B Problem 【可持久化线段树 + 网络流】

    题目连接:BZOJ - 3218 题目分析 题目要求将 n 个点染成黑色或白色,那么我们可以转化为一个最小割模型. 我们规定一个点 i 最后属于 S 集表示染成黑色,属于 T 集表示染成白色,那么对于 ...

  8. In machine learning, is more data always better than better algorithms?

    In machine learning, is more data always better than better algorithms? No. There are times when mor ...

  9. 转载:用Dreamweave cs 5.5+PhoneGap+Jquery Mobile搭建移动开发

    转载地址:http://blog.csdn.net/haha_mingg/article/details/7900221 移动设备应用开发有多难,只要学会HTML5+Javascript就可以.用Dr ...

  10. js页面传参数时,参数值包含特殊字符的处理

    js页面传参数时,参数值包含特殊字符应该怎么处理,解决方法就是利用js的escape函数,这个函数在解决中文乱码等方面应用的比较广泛.推荐使用. 工作中遇到的小问题,一个页面中通过window.sho ...