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+思维:按照权值的大小,从小的到大的连有 ...
随机推荐
- 在Windows下用MingW 4.5.2编译OpenCV 2.3.0
需要的工具:1.安装QT SDK环境2.安装CMake for Windows3.OpenCV最新Windows源码步骤:1.将QT SDK安装目录下的{QtSDK}\mingw\bin添加到系统环境 ...
- 3D Game Programming with directx 11 习题答案 8.2
第八章 第二题 1.首先找到Directx Texture Tool,它位于 2.填入配置 3.用画图工具画好每个level的图片,例如level0 4.用Directx Texture Tool添加 ...
- C++类型转换总结 转
一.前言: C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是: TYPE b = (TYPE)a. C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用. con ...
- CSS3中的background-size(对响应性图片等比例缩放)
background-size的基本属性 background-size: 可以设定背景图像的尺寸,该属性是css3中的,在移动端使用的地方很多,比如最常见的地方在做响应性布局的时候,比如之前做的项目 ...
- JavaScript学习总结【8】、面向对象编程
1.什么是面向对象编程 要理解面向对象,得先搞清楚什么是对象,首先需要明确一点这里所说的对象,不是生活中的搞男女朋友对象,面向对象就是面向着对象,换在代码中,就是一段代码相中了另一段代码,自此夜以继日 ...
- 【python】dir(__builtins__)查看python中所用BIF(内置函数)
dir(__builtins__)查看python中所用BIF(内置函数)
- iOS: 学习笔记, Swift与C指针交互(译)
Swift与C指针交互 Objective-C和C API经常需要使用指针. 在设计上, Swift数据类型可以自然的与基于指针的Cocoa API一起工作, Swift自动处理几种常用的指针参数. ...
- codevs 1557 热浪
传送门 题目描述 Description 德克萨斯纯朴的民眾们这个夏天正在遭受巨大的热浪!!!他们的德克萨斯长角牛吃起来不错,可是他们并不是很擅长生產富含奶油的乳製品.Farmer John此时以先天 ...
- JSP中两种模式的总结
运用JSP/Servlet实现的Web动态交互,主要采用: 模式一:JSP+JavaBean 链接:http://wxmimperio.coding.io/?p=155 模式二;JSP+Servlet ...
- c/c++的函数参数压栈顺序
整理日:2015年3月18日 为了这句话丢了很多次人.无所谓了,反正咱脸皮厚. 总结一下 编译出来的c/c++程序的参数压栈顺序只和编译器相关! 下面列举了一些常见的编译器的调用约定 VC6 调用约定 ...