hdu 1242 Rescue(BFS入门)
第一次用容器做的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')//杀一个守卫时间+2s.step += 2;elses.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入门)相关推荐
- HDU 1242 Rescue BFS+优先队列
题目链接:点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=1242 #include <stdio.h> #include <stri ...
- (step4.2.3)hdu 1242(Rescue——BFS)
题目大意:friends用最短的时间去救angel '.'表示通道 '#'表示墙壁 'x'表示guard.走一格要一单位时间,杀死一个guard要一个单位时间. 如果可以救求最短时间,否则按要求输出 ...
- 1242 Rescue BFS
#include<iostream> #include<string> #include<string.h> #include<stdio.h> #in ...
- HDU 1242 Rescue
bfs问题. Angel有被关在监狱,她有非常多朋友要去救她. #表示墙,.表示路,x表示警卫,r表示她的朋友. 因为可能有非常多朋友,可是Angel仅仅有一个,所以搜索起点设为Angel.仅仅要找到 ...
- 【HDU 2612 Find a Way(BFS)】(兼BFS入门笔记)
[HDU 2612 Find a Way(BFS)](兼BFS入门笔记) 原题入口: http://acm.hdu.edu.cn/showproblem.php?pid=2612 第一篇在CSDN的博 ...
- *【HDU - 1242 】 Rescue (反向dfs,或bfs)
题干: Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N ...
- BFS HDOJ 1242 Rescue
题目传送门 题意:从r走到a,遇到x多走一步,问最小走到a的步数 分析:因为r有多个,反过来想从a走到某个r的最小步数,简单的BFS.我对这题有特殊的感情,去年刚来集训队时肉鸽推荐了这题,当时什么都不 ...
- hdu - 1072 Nightmare(bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1072 遇到Bomb-Reset-Equipment的时候除了时间恢复之外,必须把这个点做标记不能再走,不然可能造 ...
- HDU - 1043 Eight(bfs打表)
题目链接:点击查看 题目大意:八数码经典问题,给出一个3*3的矩阵,其中随机分布着1~8的数字以及一个空位(我们用x来表示空位),在整个矩阵中,每一次操作都可以将x和他附近的方块互换,问经过多少次操作 ...
- hdu 1241Oil Deposits(BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 Oil Deposits Time Limit: 2000/1000 MS (Java/Othe ...
最新文章
- 瞭望|事关未来10~15年工业操作系统主导权之争,工业互联网下一步怎么走?...
- Unity iPhone Touch Animation Tutorial,untiy3d iphone简单动画开发教程
- python django flask介绍_django和flask哪个值得研究学习
- sql查询中使用in和exits比较和区别
- 浅析数字签名的工作原理 【小白级别的原创文章 仅供扫盲】
- sublime text 3的组合快捷键汇总
- java 数组或者链表输出
- zynq以太网官网例子调试
- 收藏 | 江苏省各地教师公务员等实际工资爆料
- 盛帮股份深交所上市:市值24亿 赖喜隆父子为实控人
- SQL在执行JOIN ON时,到底发生了什么?
- 软件专业面试心理测试题,面试心理测试题目,据说很准
- 2021/10/15 考试总结
- java大文件pdf水印_java – 如何扩展PDF的页面大小以添加水印?
- 微信h5禁止分享和复制链接 超简单
- 华三H3C端口聚合与链路聚合
- 一个故事看懂AI神经网络工作原理
- BUUCTF(12_1-12_5)
- 英语单词学习-8-39
- 360路由器更新网络
热门文章
- GitHub 的 Pull Request
- CodeMirror入门
- BZOJ:1816 [Cqoi2010]扑克牌 (贪心或二分答案)
- vmware centos6.4挂载ntfs移动硬盘
- shell脚本批量删除几天前的文件
- SQL注入原理解说,非常不错!
- 干货 | 这是一份你急需的数据分析的职业规划
- 统计挖掘那些事(六)——强大的逻辑回归(理论+案例)
- html ul标签宽度,css - ul中每个li的不同宽度 - SO中文参考 - www.soinside.com
- 项目中的设计模式【工厂方法模式】