Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25076 Accepted Submission(s): 8883
Problem 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
题解:
给你一个图,#不能走,r为起点,a是终点。如果遇到x,步数+2。。。。bfs+优先队列。。。。我写了好久普通队列的bfs,结果都写拙了。。。。唉
AC代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
int ans=0;
const int maxn = 1001;
char map[maxn][maxn];
int visited[maxn][maxn];
int dir[4][2]={1,0,0,1,-1,0,0,-1};
struct node
{
int x,y;
int time;
friend bool operator < (node a,node b)
{
return a.time>b.time;
}
};
int n,m;
int check(int x,int y)
{
if(0<=x&&x<n&&0<=y&&y<m&&map[x][y]!='#')
return 1;
else return 0;
}
int bfs(int x,int y)
{
priority_queue<node>q; //优先队列
// queue<node>q;
memset(visited,0,sizeof(visited));
node st,ed;
st.x=x;
st.y=y;
st.time=0;
visited[st.x][st.y]=1;
q.push(st);
while(!q.empty())
{
st=q.top();
// st=q.front();
q.pop();
if(map[st.x][st.y]=='a')
{
return st.time;
}
for(int i=0;i<4;i++)
{
ed.x=st.x+dir[i][0];
ed.y=st.y+dir[i][1];
if(check(ed.x,ed.y)&&!visited[ed.x][ed.y])
{
if(map[ed.x][ed.y]=='x')
ed.time=st.time+2;
else
ed.time=st.time+1;
visited[ed.x][ed.y]=1;
q.push(ed);
}
}
}
return -1;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int i,j;
int x,y;
// memset(map,'\0',sizeof(map));
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>map[i][j];
if(map[i][j]=='r')
{
x=i;
y=j;
}
}
}
ans=bfs(x,y);
if(ans==-1)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",ans);
}
return 0;
}
我写拙了的普通队列的bfs:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
int n,m;
char map[1010][1010];
int Count=0;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
char mark[1010][1010];
struct node
{
int x,y;
int dir;
int mark;
int step;
};
void bfs(int a,int b,int c,int d)
{
queue<node>q;
node st,ed;
st.x=a; st.y=b;
mark[st.x][st.y]='#';
st.step=0;
q.push(st);
while(!q.empty())
{
st=q.front();
q.pop();
if(st.x==c&&st.y==d)
{
printf("%d\n",st.step);
return ;
}
for(int i=0;i<4;i++)
{
ed.x=st.x+dir[i][0];
ed.y=st.y+dir[i][1];
if(ed.x>0 && ed.y>0 && ed.x<n &&ed.y< m&&map[ed.x][ed.y]!='#')
{
if(map[ed.x][ed.y]=='x')
{
st.step+=2;
map[ed.x][ed.y]!='#';
}
else if(map[ed.x][ed.y]=='.')
{
st.step++;
map[ed.x][ed.y]!='#';
}
q.push(ed);
}
}
}
printf("Poor ANGEL has to stay in the prison all his life\n");
}
int main()
{
int sti,stj,edi,edj;
cin>>n>>m;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>map[i][j];
if(map[i][j]=='r')
{
sti=i;
stj=j;
}
if(map[i][j]=='a')
{
edi=i;
edj=j;
}
}
}
bfs(sti,stj,edi,edj);
return 0;
}