博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
位置数组zoj1649 BFS
阅读量:5142 次
发布时间:2019-06-13

本文共 2615 字,大约阅读时间需要 8 分钟。

查了好多资料,发现还是不全,干脆自己整理吧,至少保证在我的做法正确的,以免误导读者,也是给自己做个记载吧!

    

    Rescue

    

    


    

    Time Limit:

    2 Seconds     

    Memory Limit:

    65536 KB

    

    


    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是按照步数优先来求解的,但对于本题来说,步数最少的解确纷歧定是最优解。如:

    2 5

    axxxr

    .....

    每日一道理
我拽着春姑娘的衣裙,春姑娘把我带到了绿色的世界里。

    明显按照普通的BFS求解的话,结果是7(即笔挺向左走到头),但最优解应该是6。为了解决这个抵触,我们可以另设一个数组,记载从出发点走到以后位置的最少时光,然后在BFS的过程中,只要从以后位置走到下一个位置的时光小于下一个位置的最小时光,就入队。此处还可以做一个优化,为了避免同一个节点重复入队,我们用优先队列,按照时光由小到大的顺序扩展。

#include 
#include
#include
using namespace std;struct st{ int x,y; int time; bool operator<(const st t)const { return time>t.time; }}cur,next;char map[205][205];int mt[205][205];//记载最少时光int dir[4][2]={
{0,1},{0,-1},{1,0},{-1,0}};int n,m;bool path(int x,int y){ if(x>=0&&x
=0&&y
q; cur.x=sx; cur.y=sy; cur.time=0; mt[sx][sy]=0; q.push(cur); while(!q.empty()) { cur=q.top(); q.pop(); if(map[cur.x][cur.y]=='a') return cur.time; for(int i=0;i<4;i++) { next.x=cur.x+dir[i][0]; next.y=cur.y+dir[i][1]; next.time=cur.time+1; if(map[next.x][next.y]=='x') next.time++; if(path(next.x,next.y)&&next.time

    

 

文章结束给大家分享下程序员的一些笑话语录: 现在社会太数字化了,所以最好是有一个集很多功能于一身的设备!

转载于:https://www.cnblogs.com/xinyuyuanm/archive/2013/05/19/3087740.html

你可能感兴趣的文章
用户空间与内核空间,进程上下文与中断上下文[总结]
查看>>
JAVA开发环境搭建
查看>>
Data truncation: Out of range value for column 'Quality' at row 1
查看>>
ad logon hour
查看>>
Linux内核态、用户态简介与IntelCPU特权级别--Ring0-3
查看>>
第23月第24天 git命令 .git-credentials git rm --cached git stash clear
查看>>
java SE :标准输入/输出
查看>>
[ JAVA编程 ] double类型计算精度丢失问题及解决方法
查看>>
好玩的-记最近玩的几个经典ipad ios游戏
查看>>
tmux的简单快捷键
查看>>
[Swift]LeetCode922.按奇偶排序数组 II | Sort Array By Parity II
查看>>
Vue_(组件通讯)子组件向父组件传值
查看>>
移动开发平台-应用之星app制作教程
查看>>
DataGridView的行的字体颜色变化
查看>>
[Serializable]的应用--注册码的生成,加密和验证
查看>>
Android-多线程AsyncTask
查看>>
LeetCode【709. 转换成小写字母】
查看>>
如果没有按照正常的先装iis后装.net的顺序,可以使用此命令重新注册一下:
查看>>
【题解】青蛙的约会
查看>>
Android 官方新手指导教程
查看>>