Oh, my goddess

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
 
描述

Shining Knight is the embodiment of justice and he has a very sharp sword can even cleavewall. Many bad guys are dead on his sword.

One day, two evil sorcerer cgangee and Jackchess decided to give him some colorto see. So they kidnapped Shining Knight's beloved girl--Miss Ice! They built a M x Nmaze with magic and shut her up in it.

Shining Knight arrives at the maze entrance immediately. He can reach any adjacent emptysquare of four directions -- up, down, left, and right in 1 second. Or cleave one adjacent wall in 3

seconds, namely,turn it into empty square. It's the time to save his goddess! Notice: ShiningKnight won't leave the maze before he find Miss Ice.

 
输入
The input consists of blocks of lines. There is a blank line between two blocks.

The first line of each block contains two positive integers M <= 50 and N <= 50separated by one space. In each of the next M lines there is a string of length N contentsO and #.

O represents empty squares. # means a wall.

At last, the location of Miss Ice, ( x, y ). 1 <= x <= M, 1 <= y <= N.

(Shining Knight always starts at coordinate ( 1, 1 ). Both Shining and Ice's locationguarantee not to be a wall.)

输出
The least amount of time Shining Knight takes to save hisgoddess in one line.
样例输入
3 5
O####
#####
#O#O#
3 4
样例输出
14

题解:坑,水,搞定#花费3,过去1,总共应该是4;

代码:
 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct Node{
int x,y,step;
friend bool operator < (Node a,Node b){
return a.step>b.step;
}
};
char map[][];
int vis[][];
int disx[]={,,,-};
int disy[]={,-,,};
int M,N,ex,ey;
void bfs(int sx,int sy){
memset(vis,,sizeof(vis));
vis[sx][sy]=;
priority_queue<Node>dl;
Node a,b;
a.x=sx;a.y=sy;a.step=;
dl.push(a);
while(!dl.empty()){
a=dl.top();
dl.pop();
for(int i=;i<;i++){
b.x=a.x+disx[i];b.y=a.y+disy[i];
if(b.x<||b.y<||b.x>M||b.y>N||vis[b.x][b.y])continue;
if(map[b.x][b.y]=='O'){
b.step=a.step+;
}
if(map[b.x][b.y]=='#')b.step=a.step+;
if(b.x==ex&&b.y==ey){
printf("%d\n",b.step);
return;
}
vis[b.x][b.y]=;
dl.push(b);
}
}
}
int main(){
while(~scanf("%d%d",&M,&N)){
for(int i=;i<=M;i++)scanf("%s",map[i]+);
scanf("%d%d",&ex,&ey);
bfs(,);
}
return ;
}

Oh, my goddess(bfs)的更多相关文章

  1. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  2. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  3. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  4. 【BZOJ5492】[HNOI2019]校园旅行(bfs)

    [HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...

  5. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

  6. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

  7. 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)

    一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...

  8. 层层递进——宽度优先搜索(BFS)

    问题引入 我们接着上次“解救小哈”的问题继续探索,不过这次是用宽度优先搜索(BFS). 注:问题来源可以点击这里 http://www.cnblogs.com/OctoptusLian/p/74296 ...

  9. HDU.2612 Find a way (BFS)

    HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...

随机推荐

  1. 修改host文件的P处理

    notepad C:\WINDOWS\system32\drivers\etc\hosts 用文档创建hosts文件,添加上面代码.把文件后缀修改为 .bat 就不用每次很麻烦的查找host文件了.

  2. php中如何输出当前服务器的(中国)当前时间

    date_default_timezone_set('PRC');//PRC是什么?PRC是中华人民共和国啊-_- echo "今天是".date("Y年m月d日&quo ...

  3. servlet tomcat eclipse

    网上搜到的很多利用eclipse结合tomcat开发servlet的教程都要修改server.xml 感觉这种方式太粗暴了,server.xml最好是与应用无关的, 这里比较推荐export war的 ...

  4. Core开发-MVC 使用dotnet 命令创建Controller和View

    NET Core开发-MVC 使用dotnet 命令创建Controller和View   使用dotnet 命令在ASP.NET Core MVC 中创建Controller和View,之前讲解过使 ...

  5. 7.java.lang.IllegalAccessException

    java.lang.IllegalAccessException 没有访问权限 当应用程序要调用一个类,但当前的方法即没有对该类的访问权限便会出现这个异常.对程序中用了Package的情况下要注意这个 ...

  6. Cortex-M3学习日志(四) -- UART0实验

    LPC1768含有4 个符合16C550工业标准的异步串口UATR0-UART3,其中UART1具有标准的MODEM接口和RS-485/EIA-485接口模式.串口通讯接口是连接计算机.终端.通讯控制 ...

  7. CentOS 删除自带的OpenJDK 和 安装SunJDK

    [root@WX32 local]# java -version java version "1.6.0" OpenJDK Runtime Environment (build - ...

  8. python 正则表达式 学习笔记(不断补充ing)

    本文参考了以下博客,感谢众位大神的分享! http://www.oschina.net/question/12_9507 和 http://www.crifan.com/python_re_sub_d ...

  9. 请问下mtk双卡手机怎样发短信是怎样选择sim卡来发(双卡都可用的情况下)?

    如题,我如今可以获取双卡状态,当仅仅有单一卡的时候可以指定sim卡进行发短信,可是双卡都可用的情况下,程序就默认使用卡1发短信了.即使指定了sim卡编号.

  10. 填充Z形二维数组

    形如  1   3 4 10  2  5 9 11  6  8 12 15  7 13 14 16 的数组称谓Z形二维数组.填充这样的数组其实只要按照Z形进行行走填充即可,设置一个flag指示方向,行 ...