概述
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.)
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.
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
优先队列+BFS。在遇到X的时候步数加1 .
但这题奇怪的是。两个OJ上的判别不一样。
貌似是HDU数据太弱。ZOJ数据比较强。
先放出最终AC两个OJ的代码。
#include <cstdio>
#include <string.h>
#include <queue>
#define MAX 205
using namespace std;
typedef struct Node{
int t;
int x,y;
}Node;
char map[MAX][MAX];
int vis[MAX][MAX];
int n,m;
int dir[8] = {0,1,0,-1,1,0,-1,0};
priority_queue<Node> q;
bool operator<(Node a, Node b)
{
return a.t > b.t;
}
int BFS()
{
Node tmp;
int i,a,b,aa,bb,t;
while( !q.empty() )
{
tmp = q.top();
q.pop();
a = tmp.x;
b = tmp.y;
t = tmp.t;
for(i=0; i<8; i+=2)
{
aa = a + dir[i];
bb = b + dir[i+1];
if( aa >= 0 && aa < n && bb >=0 && b < m && map[aa][bb] != '#' && !vis[aa][bb] )
{
vis[aa][bb] = 1;
if( map[aa][bb] == 'a' )
return t+1;
if( map[aa][bb] == '.' )
{
tmp.t = t + 1;
tmp.x = aa;
tmp.y = bb;
q.push(tmp);
}
if( map[aa][bb] == 'x' )
{
tmp.t = t + 2;
tmp.x = aa;
tmp.y = bb;
q.push(tmp);
}
}
}
}
return -1;
}
int main()
{
int i,k,ans;
Node tmp;
while( ~scanf("%d%d",&n,&m) )
{
memset(map,0,sizeof(map));
for(i=0; i<n; i++)
scanf("%s",map[i]);
while( !q.empty() )
q.pop();
memset(vis,0,sizeof(vis));
for(i=0; i<n; i++)
for(k=0; k<m; k++)
if( map[i][k] == 'r' )
{
map[i][k] = '.';
tmp.x = i;
tmp.y = k;
tmp.t = 0;
q.push(tmp);
vis[i][k] = 1;
}
ans = BFS();
if( ans != -1 )
printf("%dn",ans);
else
printf("Poor ANGEL has to stay in the prison all his life.n");
}
return 0;
}
再放出只AC了HDU,WA在ZOJ上的代码。
#include <stdio.h>
#include <string.h>
#include <queue>
#define N 205
using namespace std;
typedef struct Node
{
int x,y,step;
friend bool operator<(Node a, Node b)
{
return a.step > b.step;
}
}Node;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int n,m,kstartx,kstarty,ans=-1;
priority_queue<Node> q;
char input[N][N];
int vis[N][N];
int bfs(int x,int y)
{
memset(vis,0,sizeof(vis));
while(!q.empty())
q.pop();
Node fir;
fir.x=x,fir.y=y;
fir.step=0;
vis[x][y]=1;
q.push(fir);
while(!q.empty())
{
Node fro=q.top();
q.pop();
if(input[fro.x][fro.y]=='r')
return fro.step;
Node nxt;
nxt.step=fro.step+1;
for(int k=0;k<4;k++)
{
int xx=fro.x+dx[k];
int yy=fro.y+dy[k];
if(vis[xx][yy]||input[xx][yy]=='#'||xx<0||xx>=n||yy<0||yy>=m)
continue;
vis[xx][yy]=1;
if(input[xx][yy]=='x')
nxt.step++;
nxt.x=xx;
nxt.y=yy;
q.push(nxt);
}
}
return -1;
}
int main()
{
while(scanf("%d%d",&n,&m)>0)
{
for(int i=0;i<n;i++)
scanf("%s",input[i]);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(input[i][j]=='a')
{
kstartx=i,kstarty=j;
break;
}
ans=bfs(kstartx,kstarty);
if(ans==-1)
printf("Poor ANGEL has to stay in the prison all his life.n");
else
printf("%dn",ans);
}
return 0;
}
最后
以上就是潇洒野狼为你收集整理的ZOJ 1649 Rescue的全部内容,希望文章能够帮你解决ZOJ 1649 Rescue所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复