Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 37778 Accepted Submission(s): 13101
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
比较乱,不过还能看。
#include<bits/stdc++.h>
using namespace std;
const int MAXN=202;
char str[MAXN][MAXN];
bool vis[MAXN][MAXN];
int d[4][2]={1,0, -1,0, 0,1, 0,-1};
int n,m,cnt,stx,sty;
int k;
struct node{
int x,y,step;
node(){};
node(int _x,int _y,int _step)
{
x=_x; y=_y; step=_step;
}
bool operator <(const node &b)const
{
return step>b.step;
}
};
void bfs(int x,int y)
{
vis[x][y]=1;
priority_queue<node> que;
node e1,e2;
que.push(node(x,y,0));
while(que.size())
{
e1=que.top();
que.pop();
// printf("(%d,%d)\n",e.x,e.y);
if(str[e1.x][e1.y]=='r')
{
cnt=e1.step;
k=1;
break;
}
if(k) break;
for(int i=0;i<4;i++)
{
e2.x=e1.x+d[i][0];
e2.y=e1.y+d[i][1];
e2.step=e1.step;
int dx=e1.x+d[i][0];
int dy=e1.y+d[i][1];
if(!vis[dx][dy] && str[dx][dy]!='#' && 0 <= dx && dx < n && 0 <= dy && dy <m)
{
vis[dx][dy]=1;
if(str[dx][dy]=='x') e2.step++;
e2.step++;
que.push(e2);
}
}
}
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++) scanf("%s",str[i]);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(str[i][j]=='a')
{
stx=i;
sty=j;
break;
}
}
cnt=0;
k=0;
bfs(stx,sty);
if(k) printf("%d\n",cnt);
else printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}
975



被折叠的 条评论
为什么被折叠?



