点击打开链接题目链接
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15155 Accepted Submission(s): 5487
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
你在a的位置 需要去r救人
x是士兵 打败他需要1秒
求最短时间
#include<stdio.h> #include<string.h> #include<iostream> #include<queue> using namespace std; struct node { int x,y,step; }; int n,m,x1,y1,x2,y2; int mark[210][210]; int vis[210][210]; int dx[4]={-1,0,0,1}; int dy[4]={0,-1,1,0}; void bfs() { queue<node> q; while(!q.empty()) q.pop(); node fr,ed; int i; fr.x=x1,fr.y=y1,fr.step=0; q.push(fr); while(!q.empty()) { fr=q.front(); q.pop(); if(fr.x==x2&&fr.y==y2) { printf("%d\n",fr.step); return ; } if(mark[fr.x][fr.y]==2) { fr.step++; mark[fr.x][fr.y]=1; q.push(fr);//入队(下一层) continue; } for(i=0;i<4;i++) { ed.x=fr.x+dx[i]; ed.y=fr.y+dy[i]; if(vis[ed.x][ed.y]==1||mark[ed.x][ed.y]==0) continue; ed.step=fr.step+1; vis[ed.x][ed.y]=1; q.push(ed); } } printf("Poor ANGEL has to stay in the prison all his life.\n"); } int main() { int i,j; char s[210]; while(scanf("%d%d",&n,&m)!=EOF) { memset(mark,0,sizeof(mark)); memset(vis,0,sizeof(vis)); for(i=1;i<=n;i++) { scanf(" %s",s+1); for(j=1;j<=m;j++) { if(s[j]=='r') { x1=i,y1=j; mark[i][j]=1; vis[i][j]=1; } else if(s[j]=='a') { x2=i,y2=j; mark[i][j]=1; } else if(s[j]=='x') { mark[i][j]=2; } else if(s[j]=='.') mark[i][j]=1; } } bfs(); } return 0; }
此题打怪时间为一秒
如果打怪需要n秒 就可能需要用到优先队列了