acm 2 1012 Rescue
来源:互联网 发布:网络工程公司 编辑:程序博客网 时间:2024/06/04 19:30
1.1012
2.
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
路--1秒,x--2秒
求到达终点的最少时间
4.基本思路BFS+优先队列
5.
#include<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
#include<numeric>
#include<math.h>
#include<string.h>
#include<stdio.h>
#include<string>
#include<cstdlib>
#include<queue>
#include<iomanip>
#include<cstdio>
using namespace std;
struct ans
{
int x,y;
int t;
friend bool operator<(ans n1,ans n2)
{
return n1.t>n2.t;
}
}n1,n2;
char map[300][300];
int a[300][300],n,m;
int dix[10]={0,0,-1,1};
int diy[10]={1,-1,0,0};
int BFS(int ax,int ay, int rx,int ry)
{
int i;
priority_queue<ans> Q;
n1.x=rx;
n1.y=ry;
n1.t=0;
Q.push(n1);
while(!Q.empty())
{
n1=Q.top();
Q.pop();
if(n1.x==ax&&n1.y==ay) return n1.t;
for(i=0;i<4;i++)
{
n2.x=n1.x+dix[i];
n2.y=n1.y+diy[i];
if(map[n2.x][n2.y]=='x')
{
n2.t=n1.t+2;
}
else
{
n2.t=n1.t+1;
}
if(map[n2.x][n2.y]!='#'&&n2.x>0&&n2.x<=n&&n2.y>0&&n2.y<=m)
{
map[n2.x][n2.y]='#';
Q.push(n2);
}
}
}
return -1;
}
int main()
{
char x;
int i,k,z,j;
while(cin>>n>>m)
{
x=getchar();
int rx,ry,ax,ay;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cin>>map[i][j];
if(map[i][j]=='a') {ax=i;ay=j;}
if(map[i][j]=='r') {rx=i;ry=j;}
}
}
z=BFS(ax,ay,rx,ry);
if(z==-1) cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
else cout<<z<<endl;
}
}
- acm 2 1012 Rescue
- ACM ZOJ 1649 (Rescue)
- 1012 Rescue
- 1012 Rescue
- 1012 Rescue
- ACM-BFS之Rescue——hdu1242
- [ACM] hdu 1242 Rescue (BFS+优先队列)
- [ACM] HDU 1242 Rescue (优先队列)
- ACM--steps--4.2.1--Rescue(BFS+priority)
- [ACM] HDU 1242 Rescue (优先队列)
- [ACM] hdu 1242 Rescue (BFS+优先队列)
- Rescue
- Rescue
- Rescue
- Rescue
- Rescue
- Rescue
- Rescue
- C++中重载强制类型转换
- 【常用类库之一—org.apache.commons.lang3】
- MySQL事务隔离级别详解以及例子
- VR虚拟现实 让你成为真正的主角
- java进程间通信(IPC interProcess communication)
- acm 2 1012 Rescue
- laravel安装笔记
- java fock join 列子
- PHP常见面试题
- log4J错误
- 313. Super Ugly Number
- Mac 安装 Scrapy
- Web开发_问题解决(3)之linux服务器上获取路径问题||jsp页面报错问题
- android基础学习之back键的监听和回到主页面