hdu 1242 Rescue
2016-01-27 12:30
519 查看
问题描述
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.)
输入
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.
输出
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."
样例输入
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
样例输出
13
这题与poj坦克大战那道题类似http://blog.csdn.net/qq_31237061/article/details/50593233
遇到卫兵多花1秒时间;
思路:bfs+优先队列:
代码:
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
struct node{
int x,y,step;
node(int a,int b,int c)
{
x=a;y=b;step=c;
}
bool operator < (const node & a)const
{
return step> a.step;
}
};
const int maxx=205;
priority_queue <node> que;
char ma[maxx][maxx];int vis[maxx][maxx];
int n,m;
int xx,yy,ex,ey;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int bfs()
{
node no(xx,yy,0);
vis[xx][yy]=1;
que.push(no);
while(!que.empty())
{
node no1=que.top();
que.pop();
for(int i=0;i<4;i++)
{
int x=no1.x+dx[i];
int y=no1.y+dy[i];
int step=no1.step;
if(vis[x][y]||x<0||x>=n||y<0||y>=m||ma[x][y]=='#')continue;
if(ma[x][y]=='x')
step++;
if(ma[x][y]=='a')
return step+1;
vis[x][y]=1;
que.push(node(x,y,step+1));
}
}
return -1;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
while(!que.empty())que.pop();
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
{
scanf("%s",&ma[i]);
for(int j=0;j<m;j++)
{
if(ma[i][j]=='r')
{
xx=i;yy=j;
}
}
}
int c;
c=bfs();
if(c!=-1)
printf("%d\n",c);
else
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
}
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.)
输入
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.
输出
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."
样例输入
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
样例输出
13
这题与poj坦克大战那道题类似http://blog.csdn.net/qq_31237061/article/details/50593233
遇到卫兵多花1秒时间;
思路:bfs+优先队列:
代码:
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;
struct node{
int x,y,step;
node(int a,int b,int c)
{
x=a;y=b;step=c;
}
bool operator < (const node & a)const
{
return step> a.step;
}
};
const int maxx=205;
priority_queue <node> que;
char ma[maxx][maxx];int vis[maxx][maxx];
int n,m;
int xx,yy,ex,ey;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int bfs()
{
node no(xx,yy,0);
vis[xx][yy]=1;
que.push(no);
while(!que.empty())
{
node no1=que.top();
que.pop();
for(int i=0;i<4;i++)
{
int x=no1.x+dx[i];
int y=no1.y+dy[i];
int step=no1.step;
if(vis[x][y]||x<0||x>=n||y<0||y>=m||ma[x][y]=='#')continue;
if(ma[x][y]=='x')
step++;
if(ma[x][y]=='a')
return step+1;
vis[x][y]=1;
que.push(node(x,y,step+1));
}
}
return -1;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
while(!que.empty())que.pop();
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
{
scanf("%s",&ma[i]);
for(int j=0;j<m;j++)
{
if(ma[i][j]=='r')
{
xx=i;yy=j;
}
}
}
int c;
c=bfs();
if(c!=-1)
printf("%d\n",c);
else
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
}
相关文章推荐
- 课堂笔记
- EasyUI使用JSON保存数据
- iOS开发-UI控件:代码取消UITableViewCell选中状态
- GCD之dispatch queue
- easyui webuploader 文件上传演示
- iOS开发-进阶:子线程更新UI的两种方法
- 修改UITextField的placeholder颜色
- iOS学习心得——UINavigationController
- easyui根据id获取列索引
- iOS之UITableViewCell左右滑动效果
- iOS开发-UI控件:UIImagePickerController 视频录制操作,视频大小,时间长度
- iOS开发-基础:获取设备UUID,并使用keychain存储
- iOS UITextField 只能输入数字和小数点,只能有两位小数
- request对象的几种用法
- define,require的基本用法
- GPUImage的简单使用
- 钥匙串保存uuid:生成plist的问题
- Java中的StringBuffer与StringBuilder及正则表达式
- IOS 改变UISearchBar的背景色
- 设置UITableView单元格分割线长度