ACM--steps--4.2.1--Rescue(BFS+priority)
来源:互联网 发布:js url encode gbk 编辑:程序博客网 时间:2024/05/16 01:15
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 932 Accepted Submission(s): 390Problem Description
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.
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.)
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.)
Input
First line contains two integers stand for N and M.
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.
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.
Output
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."
Sample Input
7 8#.#####.#.a#..r.#..#x.....#..#.##...##...#..............
Sample Output
13
Author
CHEN, Xue
Source
ZOJ Monthly, October 2003
Recommend
Eddy
#include<iostream>#include<queue>#include<cstring>#include<cstdio>using namespace std;struct dyx{ int x,y,time;//x,y表示坐标,time表示所用的时间, friend bool operator<(dyx a,dyx b) { return b.time<a.time;//time小的优先级大。 }};char map[209][209];//输入地图;//dyx now,next;//wyx表示当前的位置,next表示接下来进行搜索的位置。int n,m,vis[209][209];//n,m分别为行列数,访问过的状态。int x1,x2,y1,y2;//表示起始目标和终止目标;int dx[4]={1,0,-1,0};//表示方向向量。int dy[4]={0,1,0,-1};//检查当前的状态。int wyx(int x,int y){ if(x<0||y<0||x>=n||y>=m||map[x][y]=='#'||!vis[x][y]) return 1; return 0;}int bfs(){ dyx now,next;//wyx表示当前的位置,next表示接下来进行搜索的位置。 int i; priority_queue<dyx> que; now.x=x1; now.y=y1; now.time=0; que.push(now); vis[x1][y1]=0; while(!que.empty()) { now=que.top(); que.pop(); if(now.x==x2&&now.y==y2) return now.time; for(i=0;i<4;i++) { next=now; next.x+=dx[i]; next.y+=dy[i]; if(wyx(next.x,next.y)) continue; next.time++; if(map[next.x][next.y]=='x') //在卫兵的地方要达到卫兵所以要多花费1秒。 next.time++; if(vis[next.x][next.y]>=next.time) {//最小时间, vis[next.x][next.y]=next.time; que.push(next); } } } return 0;}int main(){ int i,j; while(~scanf("%d%d",&n,&m)) { for(i=0;i<n;i++) for(j=0;j<m;j++) { cin>>map[i][j]; if(map[i][j]=='r') { x1=i; y1=j; } else if(map[i][j]=='a') { x2=i; y2=j; } } memset(vis,1,sizeof(vis));//1表示未经过的状态。 int ans=0; ans=bfs(); if(ans) cout<<ans<<endl; else cout<<"Poor ANGEL has to stay in the prison all his life."<<endl; } return 0;}
0 0
- ACM--steps--4.2.1--Rescue(BFS+priority)
- hdu 1242 Rescue bfs+优先队列priority
- ACM-BFS之Rescue——hdu1242
- [ACM] hdu 1242 Rescue (BFS+优先队列)
- [ACM] hdu 1242 Rescue (BFS+优先队列)
- ACM--steps--3.1.1
- hdu acm steps 1.3.1
- hdu acm steps 2.1.1
- hdu acm steps 2.2.1
- ACM--steps--dyx--1.2.1
- 杭电ACM Steps (1)
- Rescue+BFS
- Rescue (bfs)
- Rescue(BFS)
- Steps-ACM
- ACM STEPS
- ACM steps
- ACM: 图论题 BFS + STL + priority…
- uva1471
- 英文字典
- ios开发 地图上显示大头针和标注简单教程
- LeetCode | #24 Swap Nodes in Pairs
- Andriod简介
- ACM--steps--4.2.1--Rescue(BFS+priority)
- 《C语言及程序设计》实践参考——求阶乘
- 《C语言及程序设计》实践参考——输出这样的整数
- 《C语言及程序设计》实践参考——多数中的最大
- 《C语言及程序设计》实践参考——麻烦的累加涨功夫
- 深入剖析STL GCC 4.4的STL string
- Android性能测试初谈--内存分析(金阳光)
- java基本数据类型和引用数据类型
- 【BZOJ 1150】 [CTSC2007]数据备份Backup