BFS HDOJ 1242 Rescue
题意:从r走到a,遇到x多走一步,问最小走到a的步数
分析:因为r有多个,反过来想从a走到某个r的最小步数,简单的BFS。我对这题有特殊的感情,去年刚来集训队时肉鸽推荐了这题,当时什么都不会,看个数组模拟队列的BFS看的头晕,现在看起来也不过如此,额,当年开始是从r走到a的,因为数据巨弱才过的,应该要用到优先队列。
/************************************************
* Author :Running_Time
* Created Time :2015/9/25 星期五 09:13:51
* File Name :B_BFS.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 2e2 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;
struct Angle {
int x, y, step;
Angle () {}
Angle (int x, int y, int step) : x (x), y (y), step (step) {}
};
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
bool vis[N][N];
char maze[N][N];
int n, m; bool judge(int x, int y) {
if (x < 1 || x > n || y < 1 || y > m || vis[x][y] || maze[x][y] == '#') return false;
else return true;
} void BFS(Angle s) {
int ret = INF;
memset (vis, false, sizeof (vis));
queue<Angle> Q; Q.push (s);
vis[s.x][s.y] = true;
while (!Q.empty ()) {
Angle r = Q.front (); Q.pop ();
for (int i=0; i<4; ++i) {
int tx = r.x + dx[i];
int ty = r.y + dy[i];
if (!judge (tx, ty)) continue;
vis[tx][ty] = true;
if (maze[tx][ty] == 'r') {
ret = min (ret, r.step + 1); continue;
}
else if (maze[tx][ty] == 'x') {
Q.push (Angle (tx, ty, r.step + 2)); continue;
}
else Q.push (Angle (tx, ty, r.step + 1));
}
}
if (ret == INF) puts ("Poor ANGEL has to stay in the prison all his life.");
else printf ("%d\n", ret);
} int main(void) {
while (scanf ("%d%d", &n, &m) == 2) {
for (int i=1; i<=n; ++i) {
scanf ("%s", maze[i] + 1);
}
bool find = false; Angle start;
for (int i=1; i<=n && !find; ++i) {
for (int j=1; j<=m; ++j) {
if (maze[i][j] == 'a') {
start = Angle (i, j, 0);
find = true; break;
}
}
}
BFS (start);
} return 0;
}
当年的代码不忍直视。。。
#include<stdio.h>
typedef struct point
{
int x,y,step;
}target;
int N,M,dir[4][2]={0,1,0,-1,1,0,-1,0},ax,ay;
int flag[202][202];
char map[302][302];
target que[40005];
int BFS(target start)
{
int end,top,i;
int min=1000000;
target in,next;
end=top=0;
que[top]=start;
while (top>=end)
{
in=que[end];
end=(end+1);
for (i=0;i<4;i++)
{
next.x=in.x+dir[i][0];
next.y=in.y+dir[i][1];
if (map[next.x][next.y]=='r')
{
if (min>in.step+1)
min=in.step+1;
}
if (next.x>=0&&next.x<N&&next.y>=0&&next.y<M&&map[next.x][next.y]!='#')
{
if (flag[next.x][next.y]>in.step+1)
{
next.step=in.step+1;
if (map[next.x][next.y]=='x')
next.step++;
flag[next.x][next.y]=next.step;
top=(top+1);
que[top]=next;
}
}
}
}
if (min!=1000000)return min;
else
return -1;
}
int main()
{
int i,j,num;
target start;
while (scanf("%d%d",&N,&M)!=EOF)
{
for (i=0;i<N;i++)
{
scanf("%s",map[i]);
for (j=0;j<M;j++)
{
flag[i][j]=1000000;
if (map[i][j]=='a')
{
//map[i][j]='.';
ax=i;
ay=j;
}
}
}
start.x=ax;
start.y=ay;
start.step=0;
//map[ax][ay]='.';
num=BFS(start);
if (num==-1)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",num);
}
return 0;
}
BFS HDOJ 1242 Rescue的更多相关文章
- hdoj 1242 Rescue
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)
Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is ...
- hdu 1242:Rescue(BFS广搜 + 优先队列)
Rescue Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...
- HDU 1242 Rescue (BFS(广度优先搜索))
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- hdu 1242 Rescue(bfs)
此刻再看优先队列,不像刚接触时的那般迷茫!这也许就是集训的成果吧! 加油!!!优先队列必须要搞定的! 这道题意很简单!自己定义优先级别! +++++++++++++++++++++++++++++++ ...
- HDU 1242 Rescue(BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...
- hdu 1242 Rescue (BFS)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
随机推荐
- HTML页面中点击按钮关闭页面几种方式与取消
1.不带任何提示关闭窗口的js代码 <input type="button" name="close" value="关闭" oncl ...
- 嵌入式开发之davinci--- 8168 question about capture PAL on 8168
http://e2e.ti.com/support/dsp/davinci_digital_media_processors/f/717/t/340483.aspx?pi199607=2
- C# 事件处理与自定义事件
http://blog.csdn.net/cyp403/article/details/1514023 图一 ...
- hihoCoder 1586 Minimum 【线段树】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
#1586 : Minimum 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 You are given a list of integers a0, a1, …, a2 ...
- HBase运维和优化
管理工具 HBase ShellHBase Shell是HBase组件提供的基于JRuby IRB的字符界面的交互式客户端程序,通过HBase Shell可以实现对HBase的绝大部分操作 通过hel ...
- 【转】使用git 工具下载android.jar Source Code
为了开发android应用,在开发时发现sdk没有源代码,这样在开发时太麻烦了,下面说说如何下载源代码,以及如何配置. 下载源代码需要git,先下载一个git.下面的操作都是在windows下完成的. ...
- [树套树]K大数查询
有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是多少.为了 ...
- html5--6-53 阶段练习4-画廊
html5--6-53 阶段练习4-画廊 学习要点 运用所学过的知识完成一个简单的小练习,理解对过渡动画的应用. @charset "utf-8"; /* CSS Document ...
- 第五届蓝桥杯C++B组省赛
1.啤酒和饮料 2.切面条 3.李白打酒 4.史丰收速算 5.打印图形 6.奇怪的分式 7.六角填数 8.蚂蚁感冒 9.地宫取宝 10.小朋友排队
- centos安装cowboy过程
在centos机器上安装erlang: yum install erlang -y 接着把之前在ubuntu上的cowboy工程拷贝到centos机器上,进入到工程目录,输入: make run 提示 ...