第一次用容器做的BFS题目,题目有个地方比较坑,就是遍历时的方向,比如上下左右能AC,右上左下就WA

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
using namespace std;
char map[205][205];
int x_begin,y_begin,flag,n,m;
int v[205][205],d[4][2] = { {-1,0},{1,0},{0,-1},{0,1} };
struct node
{
int x;
int y;
int step;
};
void bfs()
{
queue <node> q;
node s,temp;
s.x = x_begin;
s.y = y_begin;
s.step = 0;
v[x_begin][y_begin] = 0;
q.push(s);
while(!q.empty())
{
temp = q.front();
q.pop();
if(map[temp.x][temp.y] == 'r')
{
printf("%d\n",temp.step);
flag = 1;
return ;
}
for(int i = 0 ; i < 4 ; i ++)
{
s = temp;//temp为本次遍历的结点
s.x += d[i][0];
s.y += d[i][1];
if(s.x >= 0 && s.x < n && s.y >= 0 && s.y < m && map[s.x][s.y] != '#')
{
if(map[s.x][s.y] == 'x')//杀一个守卫时间+2
s.step += 2;
else
s.step ++;
if(v[s.x][s.y] >= s.step)
{
v[s.x][s.y] = s.step;
q.push(s);
}
}
}
}
}
int main()
{
int i,j;
while(~scanf("%d%d",&n,&m))
{
for(i = 0 ; i < n ; i ++)
{
scanf("%s",map[i]);
for(j = 0 ; j < m ; j ++)
if(map[i][j] == 'a')
{
x_begin = i;
y_begin = j;
}
}
memset(v,1,sizeof(v));
flag = 0;
bfs();
if(!flag)
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}

hdu 1242 Rescue(BFS入门)的更多相关文章

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

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

  2. HDU 1242 Rescue(BFS),ZOJ 1649

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

  3. hdu 1242 Rescue (BFS)

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

  4. hdu 1242 Rescue

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

  5. hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...

  6. hdu 1242 Rescue(bfs)

    此刻再看优先队列,不像刚接触时的那般迷茫!这也许就是集训的成果吧! 加油!!!优先队列必须要搞定的! 这道题意很简单!自己定义优先级别! +++++++++++++++++++++++++++++++ ...

  7. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

  8. hdu 1242:Rescue(BFS广搜 + 优先队列)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  9. HDU 1242 Rescue (BFS(广度优先搜索))

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

随机推荐

  1. hdoj2544 最短路(Dijkstra || Floyd || SPFA)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2544 思路 最短路算法模板题,求解使用的Dijkstra算法.Floyd算法.SPFA算法可以当做求解 ...

  2. python 关键知识点

    学习资源:笨方法学习 python3 将变量传递给脚本--argv 脚本:你编写的 .py 文件. argv 参数变量(argument variable)保存着你运行 python 脚本的参数. i ...

  3. CSUOJ 1901 赏赐 OR 灾难 单调栈

    Description 大G南征北战终于打下了大片土地成立了G国,大G在开国大典上传召帮助自己南征北战的三大开国元勋小A,小B,小C进殿,并要赏赐三人大量宝物以显示天恩浩荡.大G在征服其他国家的时候抢 ...

  4. 俄罗斯方块 Tetris

    今天,为大家带来一个用Qt C++ (Windows环境下)做的一个简易俄罗斯方块小游戏 思路和模块介绍都在注释里面,其次就是一些项目中遇到的问题以及解决方案,在后面部分说明. 一.效果 测试图样 Q ...

  5. Framework类库(FCL)简介

    Framework类库(Framework Class Library,FCL)是一组DLL程序集的统称,其中含有数千个类型定义,每个类型都公开了一些功能 部分常规的FCL命名空间 命名空间 内容说明 ...

  6. Kolla O版本部署

    Kolla O版部署和之前的版本还是有些区别的,环境还是all-in-one 基本准备: 关闭Selina和firewalld [root@kolla ~]# cat /etc/redhat-rele ...

  7. hashmap的遍历方法

    How to iterate over the entries of a Map? What is the order of iteration - if you are just using Map ...

  8. BZOJ3473 字符串 广义后缀自动机

    今天主攻了下SAM 好多东西以前都没理解到 对于这道题 我们建一个自动机存所有串 每个穿last从1开始 对于自动机上每个点额外记一个cnt 表示能匹配到这个点的不同串个数 建完对每个串在自动机上匹配 ...

  9. Codeforces Round #283 (Div. 2) C. Removing Columns 暴力

    C. Removing Columns time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  10. Codechef December Challenge 2014 Chef and Apple Trees 水题

    Chef and Apple Trees Chef loves to prepare delicious dishes. This time, Chef has decided to prepare ...