您的位置:首页 > 产品设计 > UI/UE

Rescue 3解法:(1.DFS 2. BFS 3.BFS+优先队列模板)

2017-07-20 11:50 417 查看
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
题意:'.'为道路,‘#’障碍,‘a’为天使, 'r '为朋友 然后找从朋友到天使的最短路径
思路: 朋友不只是一个,而天使却是一个,所以我们可以反过来寻找,天使找朋友,那么目标状态就是找到朋友
可以说是一个棋盘类型的变形题目,DFS与BFS均可。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;

int n, m;
char map[205][205];
int sx, sy;
bool flag;
struct node {
int x, y, step;
bool operator <(const node & t) const
{
return step>t.step;
}
};
int dx[]= {-1,0,0,1};
int dy[]= {0,-1,1,0};

void bfs() {
node now, tmp;
int i,xx,yy;
priority_queue<node> q;
now.x = sx, now.y = sy, now.step = 0;
map[sx][sy] = '#';
q.push(now);
while(!q.empty()) {
now = q.top();
q.pop();
//         cout<<now.x<<" "<<now.y<<" "<<now.step<<endl;
for(i=0; i<4; i++) {
xx = now.x +dx[i];
yy = now.y +dy[i];
if(xx<0||xx>=n||yy<0||yy>=m||map[xx][yy]=='#') continue;
if(map[xx][yy]=='r') {
cout<<now.step+1<<endl;
flag = true;
return ;
}
if(map[xx][yy]=='x') {
tmp.x =xx, tmp.y = yy, tmp.step = now.step+2;
q.push(tmp);
} else {
tmp.x =xx, tmp.y = yy, tmp.step = now.step+1;
q.push(tmp);
}
map[xx][yy] = '#';
}
}
}

int main() {
int i, j;
while(~scanf("%d%d",&n,&m)) {
for(i=0; i<n; i++)
for(j=0; j<m; j++) {
cin>>map[i][j];
if(map[i][j]=='a')
sx=i,sy=j;
}
flag = false;
bfs();
if(!flag) printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}




BFS:把当扩展到“x”点时,把他放到与他耗时一样的那一层。这样也能保证到扩展到目标状态时,耗时是最小

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;

int n, m;
char map[205][205];
int sx, sy;
bool flag;
struct node {
int x, y, step;
bool operator <(const node & t) const
{
return step>t.step;
}
};
int dx[]= {-1,0,0,1};
int dy[]= {0,-1,1,0};

void bfs() {
node now, tmp;
int i,xx,yy;
priority_queue<node> q;
now.x = sx, now.y = sy, now.step = 0;
map[sx][sy] = '#';
q.push(now);
while(!q.empty()) {
now = q.top();
q.pop();
//         cout<<now.x<<" "<<now.y<<" "<<now.step<<endl;
for(i=0; i<4; i++) {
xx = now.x +dx[i];
yy = now.y +dy[i];
if(xx<0||xx>=n||yy<0||yy>=m||map[xx][yy]=='#') continue;
if(map[xx][yy]=='r') {
cout<<now.step+1<<endl;
flag = true;
return ;
}
if(map[xx][yy]=='x') {
tmp.x =xx, tmp.y = yy, tmp.step = now.step+2;
q.push(tmp);
} else {
tmp.x =xx, tmp.y = yy, tmp.step = now.step+1;
q.push(tmp);
}
map[xx][yy] = '#';
}
}
}

int main() {
int i, j;
while(~scanf("%d%d",&n,&m)) {
for(i=0; i<n; i++)
for(j=0; j<m; j++) {
cin>>map[i][j];
if(map[i][j]=='a')
sx=i,sy=j;
}
flag = false;
bfs();
if(!flag) printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}




BFS+优先队列,这是效率相对最高的算法,因为利用小根堆,每次都是让最小的出队列判断,所以只要找到r即为答案

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <queue>
using namespace std
4000
;
char map[205][205];
int vis[205][205];
int m,n;
int d[4][2]= {0,-1,0,1,-1,0,1,0};
struct node
{
int x,y;
int time;
friend bool operator<(const node &a,const node &b)//运算符重载对优先队列里的小于号进行的重载
{
return a.time>b.time;//时间小的先出队
}
};
int bfs(int x,int y)
{
node st,nt;
priority_queue<node> q;
st.x=x;
st.y=y;
st.time=-1;
q.push(st);
vis[st.x][st.y]=1;
while(!q.empty())
{
st=q.top();
q.pop();
if(map[st.x][st.y]=='r')
return st.time;
for(int i=0; i<4; i++)
{
nt.x=st.x+d[i][0];
nt.y=st.y+d[i][1];
if(!vis[nt.x][nt.y] && nt.x>=0 && nt.x<m && nt.y>=0 && nt.y<n && map[nt.x][nt.y]!='#')
{
vis[nt.x][nt.y]=1;
if(map[nt.x][nt.y]=='.')
nt.time=st.time+1;
else
nt.time=st.time+2;
q.push(nt);
}
}
}
return -1;
}
int main()
{
int x,y,count;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(vis,0,sizeof(vis));
count=-1;
for(int i=0; i<m; i++)
scanf("%s",map[i]);
for(int i=0; i<m; i++)
for(int j=0; j<n; j++)
if(map[i][j]=='a')
{
x=i;
y=j;
break;
}
count=bfs(x,y);
if(count==-1)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",count);
}
return 0;
}




反思:多对题目进行思考,采用时间效率较高的算法,不要因为题目的AC排名而顾虑太多,知识需要掌握,个人能力有所差距
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: