hdu 1242 dfs/bfs
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.)
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.
本题需要注意的是,天使的朋友可能不只一个,所以,应该从天使的位置开始搜去找其朋友就ok了。
#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的更多相关文章
- HDU 4771 (DFS+BFS)
Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...
- HDU 1242 Rescue(BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...
- HDU 1242 Rescue(BFS),ZOJ 1649
题目链接 ZOJ链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The ...
- hdu 1241(DFS/BFS)
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- HDU 1242 dFS 找目标最短路
//多个起点,要最短得目标,不妨倒过来从目标出发,去找最近的点更新!!!!!!递归时思路要清楚 #include<iostream> #include<cstring> usi ...
- hdu 1242 Rescue (BFS)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- hdu 1242 Rescue
题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...
- ID(dfs+bfs)-hdu-4127-Flood-it!
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...
- DFS/BFS+思维 HDOJ 5325 Crazy Bobo
题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...
随机推荐
- sec:authorize 标签 通过不通过权限例子
1. 方式一 <sec:authorize ifAnyGranted="ROLE_A"> <a href="a.jsp"> ...
- AngularJS初体验
最近突然发现,Coding.net真是一个神奇的网站.这各网站90%的请求都是通过ajax完成的.可以发现,不管你点任何链接,网页都不会刷新,点击浏览器的返回或前进按钮也是这样,打开chrome的开发 ...
- 《python源码剖析》笔记一——python编译
1.python的架构: 2.python源码的组织结构: 3.windows环境下编译python:
- 将图片文件转换为.py文件
最近用wxpython写了一个脚本,其中要给窗体设置图标文件,需要单独的一个ico文件,这样就比较影响美观,另外打包的时候还要将图标文件一起打包很繁琐.这时候看到wxpython文件有一个工具img2 ...
- 虚拟机下linux上网
一.概述 1. 常见的上网方式 有以下两种: 桥接 NAT(推荐) 有关虚拟机几种不同联网方式的讲述,可以参考VMware网络选项分析 通常的配置步骤: <1> 配置PC端 <2&g ...
- mysql 中创建存储过程
mysql中创建存储过程和存储函数虽相对其他的sql语言相对复杂,但却功能强大,存储过程和存储函数更像是一种sql语句中特定功能的一种封装,这种封装可以大大简化外围调用语句的复杂程度. 首先以表emp ...
- xcode5时代如何设置Architectures和Valid Architectures
目前ios的指令集有以下几种: 1,armv6,支持的机器iPhone,iPhone2,iPhone3G及对应的iTouch 2,armv7,支持的机器iPhone4,iPhone4S 3,armv7 ...
- 国内更新Android SDK汇总
以下两个网站提供了响应的办法. http://www.androiddevtools.cn/ --国内镜像 http://blog.csdn.net/boonya/article/details/38 ...
- Life Forms
poj3294:http://poj.org/problem?id=3294 题意:就是求n个串的中一个最大的子串,这个子串在超过n/2的串中出现. 题解:这是一道好题.首先一种解法就是用后缀数组来搞 ...
- C++ 1
1 new 建立一个堆对象 new 类名(初值列表) 返回一个指针 int * p=new int(3)动态分配 2 delete 释放指针 delete p; delete [] p ;释放动态申 ...