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

很简单的一个bfs,但是写的真揪心

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int n,m;
int ex,ey;
char map[55][55];
int vis[55][55],hp[55][55];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
struct node
{
int x;
int y;
int step;
friend bool operator<(node n1,node n2)
{
return n2.step<n1.step;
}
}p,temp;
int check(int xx,int yy)
{
if(xx<=0||yy<0||xx>n||yy>=m)
return 1;
if(vis[xx][yy])
return 1;
return 0;
}
int bfs()
{
int i;
priority_queue<node>q;
p.x=1;
p.y=0;
p.step=0;
vis[p.x][p.y]=1;
q.push(p);
while(!q.empty())
{
p=q.top();
q.pop();
if(p.x==ex&&p.y==ey-1)
{
return p.step;
}
for(i=0;i<4;i++)
{
temp.x=p.x+dx[i];
temp.y=p.y+dy[i];
if(check(temp.x,temp.y)==0)
{
temp.step=p.step+hp[temp.x][temp.y]+1;
vis[temp.x][temp.y]=1;
q.push(temp);
}
}
}
return 0;
}
int main()
{
int i,j;
int sum;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(hp,0,sizeof(hp));
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++)
{
scanf("%s",map[i]);
for(j=0;j<m;j++)
{
if(map[i][j]=='O')
hp[i][j]=0;
else if(map[i][j]=='#')
hp[i][j]=3;
}
}
scanf("%d%d",&ex,&ey);
sum=bfs();
printf("%d\n",sum);
}
return 0;
}


nyoj--635--Oh, my goddess(dfs)的更多相关文章

  1. nyoj 325 zb的生日(dfs)

    描述今天是阴历七月初五,acm队员zb的生日.zb正在和C小加.never在武汉集训.他想给这两位兄弟买点什么庆祝生日,经过调查,zb发现C小加和never都很喜欢吃西瓜,而且一吃就是一堆的那种,zb ...

  2. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  3. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  4. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

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

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

  6. 【算法导论】图的深度优先搜索遍历(DFS)

    关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...

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

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

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

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

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

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

随机推荐

  1. W3C标准冒泡、捕获机制

    (一) 捕获和冒泡如何相互影响 我们来做几个任务吧! 有一个前提,#parent为标签,#child为子标签,他们是嵌套关系支线任务1 //捕获模式 document.getElementById(' ...

  2. MySQL 优化之 index_merge (索引合并)

    深入理解 index merge 是使用索引进行优化的重要基础之一.理解了 index merge 技术,我们才知道应该如何在表上建立索引. 1. 为什么会有index merge 我们的 where ...

  3. 用Python+selenium打开IE浏览器和Chrome浏览器的问题

    这几天在学Python+selenium自动化,对三大浏览器Firefox,Chrome和IE都做了尝试,也都分别下载了对应的webdriver,如:geckodriver.chromedriver. ...

  4. 机器学习:随机森林RF-OBB袋外错误率

    文章讲解比较详细,且有Python代码,可以作为有用的参考. 原文链接:http://blog.csdn.net/zhufenglonglove/article/details/51785220  参 ...

  5. (转)Bootstrap 之 Metronic 模板的学习之路 - (5)主题&布局配置

    https://segmentfault.com/a/1190000006736457 Theme Setup 主题配置 Metronic comes with 6 color themes,defa ...

  6. luogu P2422 良好的感觉 单调栈

    Code: #include<bits/stdc++.h> #define maxn 1000000 #define ll long long using namespace std; v ...

  7. 【udacity】机器学习-神经网络

    Evernote Export 1.神经网络 神经元 细胞的主体称为细胞体,然后有轴突.突触 他们构建的方式是可以调整的 我们会有一些输入的放电信号视为放电频率或输入的强度 X1​w1​X2​w2​X ...

  8. eas之获取当前登陆信息

    public void getSystemInfo()    {        // SysContext工具类可获取当前登陆用户的信息,可根据需要进行调用.        // 举两例如下:     ...

  9. 【剑指Offer】62、二叉搜索树的第k个结点

      题目描述:   给定一棵二叉搜索树,请找出其中的第k小的结点.例如(5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4.   解题思路:   本题实际上比较简单,主要还是考察对 ...

  10. 【剑指Offer】6、旋转数组的最小数字

      题目描述:   把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5 ...