寒假前刷题(10)搜索系列 hdu 1242
来源:互联网 发布:矢量图软件coreldraw 编辑:程序博客网 时间:2024/06/05 14:48
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9912 Accepted Submission(s): 3626
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.
7 8#.#####.#.a#..r.#..#x.....#..#.##...##...#..............
13//这是一道广搜的题,因为需要求最短的时间 ,本来看了指导,知道要用优先队列+bfs,后来在小健健学长的指导下,知道其实可以不用优先队列,可以同一个结点弹出后,在往队列里压入,以此来代替优先队列,膜拜小健健!下面是代码:#include<stdio.h>#include<stdlib.h>#include<string.h>#include<iostream>#include<queue>
using namespace std;
char map[205][205];int dir[4][2]={1,0,0,-1,-1,0,0,1};//有些人喜欢分方向遍历,我还是喜欢这种顺序遍历int visit[205][205]; //标记数组,标记是否经过结点int n,m;
struct node//这点没想到啊,老是记不得用结构体,这样一用,在表示上就简单多了 { int x; int y; int time;};
void bfs(node vs){ int i; int flag=0; node vn,vw; queue<node> q; vn.x=vs.x; vn.y=vs.y; vn.time=0; visit[vn.x][vn.y]=1; q.push(vn); while(!q.empty()) { vn=q.front(); q.pop(); if(map[vn.x][vn.y]=='x') //守卫这里是难点,因为这里杀死守卫还需要1个单位的时间,然后从这里在搜索下一个地方,又需要一个单位的时间。 {
map[vn.x][vn.y]='.'; vn.time++; q.push(vn); continue; //这个题的关键就在于这里,这里不能跳出循环,而要继续在压入堆栈。 } if(map[vn.x][vn.y]=='a') { flag=1; break; //这里一找到就退出,说明结束了 } else { for(i=0;i<4;i++) { vw.x=vn.x+dir[i][0]; vw.y=vn.y+dir[i][1]; if(vw.x>=0&&vw.x<n&&vw.y>=0&&vw.y<m&&!visit[vw.x][vw.y]&&map[vw.x][vw.y]!='#') { vw.time=vn.time+1; visit[vw.x][vw.y]=1; q.push(vw); } } } } if(flag) printf("%d\n",vn.time); else printf("Poor ANGEL has to stay in the prison all his life.\n");}
int main(){ int i,j; node vs; while(scanf("%d%d",&n,&m)!=EOF) { for(i=0;i<n;i++) { scanf("%s",map[i]); for(j=0;j<m;j++) { if(map[i][j]=='r') { vs.x=i; vs.y=j; } } } memset(visit,0,sizeof(visit)); bfs(vs); } return 0;}
- 寒假前刷题(10)搜索系列 hdu 1242
- 寒假前刷题(7)搜索系列 dfs hdu 1010
- 寒假前刷题(8)搜索系列 dfs hdu 1241
- 寒假前刷题(9)搜索系列 bfs hdu 1312
- 寒假前刷题(5)搜索系列 bfs poj 1426
- 寒假前刷题(3)kmp系列 hdu 2203
- 寒假前刷题(4)kmp系列 hdu 2087
- 寒假前刷题(6)广度优先搜索 poj 3126
- 寒假前刷题(2)kmp系列 hdu1711题
- 寒假前刷题(1)
- wx网罗系列之寒假
- 寒假菜鸟系列学习题
- hdu 1242Rescue(记忆化搜索)
- HDU 1242 Rescue (搜索)水题
- hdu 1242 Rescue(BFS搜索)
- HDU 1078(搜索)
- 寒假培训——简单搜索
- 2016寒假训练——搜索
- 存储读写性能测试
- Android控件之Chronometer(定时器)
- C#调用摄像头
- 可传参或不传参的decorator
- 各种面试题
- 寒假前刷题(10)搜索系列 hdu 1242
- 孕妈妈害喜的最好膳食法
- Android中Spinner下拉列表(使用ArrayAdapter和自定义Adapter实现)
- Zigbee 之SerialApp InitBoard( OB_COLD );
- Android开发历程_12(Handler的使用)
- 开发板重写Linux+Qt 和 WinCE步骤
- 陌陌触顶求变:弃陌生人概念 转型LBS社区
- JVM启动参数之 -verbose:gc
- 【shell】/dev/null 2>&1