Rescue
2015-08-10 21:40
597 查看
[align=left]Problem Description[/align]
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.)
[align=left]Input[/align]
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.
[align=left]Output[/align]
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."
[align=left]Sample Input[/align]
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
[align=left]Sample Output[/align]
13
题解:广搜,每次找时间最短的一个,由于遇到士兵时间多1,此时这个点应该排在后面,所以用优先队列排序。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct Node
{
int x;
int y;
int time;
Node(int a,int b,int c)
{
x = a;
y = b;
time = c;
}
bool operator< (Node t) const
{
return time > t.time;
}
};
int sx,sy;
int n,m;
int ans;
char map[205][205];
int d[4][2] = {{0,-1},{0,1},{-1,0},{1,0}};
bool bfs()
{
priority_queue<Node> q;
q.push(Node(sx,sy,0));
ans = 0;
map[sx][sy] = '#';
while(!q.empty())
{
Node p = q.top();
q.pop();
for(int i = 0;i < 4;i++)
{
int x = p.x + d[i][0];
int y = p.y + d[i][1];
if(x >= 0 && x < n && y >= 0 && y < m && map[x][y] != '#')
{
if(map[x][y] == 'a')
{
ans = p.time + 1;
return true;
}
if(map[x][y] == 'x')
{
q.push(Node(x,y,p.time + 2));
}
else
{
q.push(Node(x,y,p.time + 1));
}
map[x][y] = '#';
}
}
}
return false;
}
int main()
{
while(scanf("%d%d",&n,&am
4000
p;m) != EOF)
{
getchar();
for(int i = 0;i < n;i++)
{
for(int j = 0;j < m;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j] == 'r')
{
sx = i;
sy = j;
}
}
getchar();
}
if(bfs())
{
printf("%d\n",ans);
}
else
{
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
}
return 0;
}
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.)
[align=left]Input[/align]
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.
[align=left]Output[/align]
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."
[align=left]Sample Input[/align]
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
[align=left]Sample Output[/align]
13
题解:广搜,每次找时间最短的一个,由于遇到士兵时间多1,此时这个点应该排在后面,所以用优先队列排序。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct Node
{
int x;
int y;
int time;
Node(int a,int b,int c)
{
x = a;
y = b;
time = c;
}
bool operator< (Node t) const
{
return time > t.time;
}
};
int sx,sy;
int n,m;
int ans;
char map[205][205];
int d[4][2] = {{0,-1},{0,1},{-1,0},{1,0}};
bool bfs()
{
priority_queue<Node> q;
q.push(Node(sx,sy,0));
ans = 0;
map[sx][sy] = '#';
while(!q.empty())
{
Node p = q.top();
q.pop();
for(int i = 0;i < 4;i++)
{
int x = p.x + d[i][0];
int y = p.y + d[i][1];
if(x >= 0 && x < n && y >= 0 && y < m && map[x][y] != '#')
{
if(map[x][y] == 'a')
{
ans = p.time + 1;
return true;
}
if(map[x][y] == 'x')
{
q.push(Node(x,y,p.time + 2));
}
else
{
q.push(Node(x,y,p.time + 1));
}
map[x][y] = '#';
}
}
}
return false;
}
int main()
{
while(scanf("%d%d",&n,&am
4000
p;m) != EOF)
{
getchar();
for(int i = 0;i < n;i++)
{
for(int j = 0;j < m;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j] == 'r')
{
sx = i;
sy = j;
}
}
getchar();
}
if(bfs())
{
printf("%d\n",ans);
}
else
{
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
}
return 0;
}
相关文章推荐
- 设置UItextfield边框颜色
- UIImageView 和UIButton
- CCBValue
- JAVA中String与StringBuffer,StringBuilder的区别
- OpenCV入门学习(三)HistogramEquivalent
- 自定义UICollectionViewLayout
- UITableView cell下拉菜单的做法
- UI_导航视图控制器
- UI09_重写Cell
- GDC Tips For Students & The GDC Survival Guide
- Advanced Fruits (LCS+递归、模板)
- HDOJ 1503 Advanced Fruits(LCS)
- UITableView 总结
- hdoj1159 Common Subsequence
- UITextField值变化则赋值的小技巧
- Frequently Used Algo
- leetcode 96: Unique Binary Search Trees
- UICollectionView的使用
- HDOJ 1503 Advanced Fruits (最长公共子序列 LCS)
- UI09_modal管理字典