Description

Bessie loves her grass and loves to hurry to the barn for her evening
milking session. She has partitioned the pasture into a rectilinear
grid of R (1 <= R <= 100) rows and C (1 <= C <= 100) columns and
marked the squares as grass or rocky (she can't eat rocks and won't
even go into those squares). Bessie starts on her map at location
R_b,C_b and wishes to munch her way, square by square, to the barn
at location 1,1 by the shortest route possible. She moves from one
square to any one of the potentially four adjacent squares. Below is the original map [with rocks ('*'), grass ('.'), the barn
('B'), and Bessie ('C' for cow) at row 5, col 6] and a route map
with the optimal route marked with munches ('m'): Map Optimal Munched Route
1 2 3 4 5 6 <-col 1 2 3 4 5 6 <-col
1 B . . . * . 1 B m m m * .
2 . . * . . . 2 . . * m m m
3 . * * . * . 3 . * * . * m
4 . . * * * . 4 . . * * * m
5 * . . * . C 5 * . . * . m Bessie munched 9 squares. Given a map, determine how many squares Bessie will eat on her
shortest route to the barn (there's no grass to eat on the barn's
square).

Input

* Line 1: Two space-separated integers: R and C

* Lines 2..R+1: Line i+1 describes row i with C characters (and no
spaces) as described above

Output

* Line 1: A single integer that is the number of squares of grass that
Bessie munches on the shortest route back to the barn
5 6
B...*.
..*...
.**.*.
..***.
*..*.C

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int inf=9999999;
typedef pair<int,int>pos;
int R,C;
char Map[105][105];//地图
int ans[105][105];//由起点到(i,j)点的最小距离&&(i,j)点不是石头
int bi,bj;//起点
int ci,cj;//终点
int X[4]={0,1,-1,0},Y[4]={1,0,0,-1};//四个方向
int BFS(){
queue<pos>que;
for(int i=0;i<R;i++)
for(int j=0;j<C;j++)
ans[i][j]=inf;
que.push(pos(bi,bj));
ans[bi][bj]=0;
while(que.size()){
pos p=que.front();
que.pop();
if(p.first==ci&&p.second==cj)
break;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++){
int xn=p.first+X[i];
int yn=p.second+Y[i];
if(xn>=0&&xn<R&&yn>=0&&yn<C&&ans[xn][yn]==inf&&Map[xn][yn]!='*'){
que.push(pos(xn,yn));
ans[xn][yn]=ans[p.first][p.second]+1;
}
}
}
return ans[ci][cj];
}
int main ()
{
while(~scanf("%d%d",&R,&C)){
for(int i=0;i<R;i++)
for(int j=0;j<C;j++){
cin>>Map[i][j];
if(Map[i][j]=='B'){
bi=i;bj=j;
}
else if(Map[i][j]=='C'){
ci=i;cj=j;
}
}
printf("%d\n",BFS());
}
return 0;
}

1381: Munching(BFS)的更多相关文章

  1. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  2. 【BZOJ-1656】The Grove 树木 BFS + 射线法

    1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 186  Solved: 118[Su ...

  3. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  4. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  5. Sicily 1215: 脱离地牢(BFS)

    这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...

  6. Sicily 1048: Inverso(BFS)

    题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...

  7. Sicily 1444: Prime Path(BFS)

    题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...

  8. Sicily 1051: 魔板(BFS+排重)

    相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...

  9. Sicily 1150: 简单魔板(BFS)

    此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...

随机推荐

  1. 初探OpenGL(一)

    OPenGL ES 1.X 面向功能固定的硬件所涉及并提供加速支持,图形质量以及性能标准. OpenGL ES2.X则提供包括着色器技术在内的全编程3D图形算法.----硬件要求比较高. OpenGL ...

  2. 自定义VBS脚本(统计在指定文件中搜索字符串出现的次数)

    '=========================================================================='' VBScript Source File - ...

  3. 获取机器网卡的物理(MAC)地址

    <?php  /**   * 获取机器网卡的物理(MAC)地址* 目前支持WIN/LINUX系统   * 编辑: www.jbxue.com**/  class MacAddInfo {     ...

  4. log4j.xml(信息打印)

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SY ...

  5. LeetCode OJ 226. Invert Binary Tree

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...

  6. OpenCV ——背景建模之CodeBook(1)

    1,CodeBook算法流程介绍 CodeBook算法的基本思想是得到每个像素的时间序列模型.这种模型能很好地处理时间起伏,缺点是需要消耗大量的内存.CodeBook算法为当前图像的每一个像素建立一个 ...

  7. GitHub这么火,程序员你不学学吗? 超简单入门教程 【转载】

    本GitHub教程旨在能够帮助大家快速入门学习使用GitHub. 本文章由做全栈攻城狮-写代码也要读书,爱全栈,更爱生活.原创.如有转载,请注明出处. GitHub是什么? GitHub首先是个分布式 ...

  8. 全选js实现

    前端全选框 <input type='checkbox' name='allSelect' onclick='sel(this)'></input> //有i个选择框 < ...

  9. Sublime text 3 如何格式化HTML代码

    使用Sublime text 3 编写代码是一种享受,使用Sublime text 3 格式化HTML代码,需要安装插件,具体安装步骤如下:   1.打开菜单->首选项->插件控制,输入 ...

  10. maven 国内镜像地址

    由于连接国外网站时网速特慢,为解决这个问题,os china 建立了一个maven 的私服.为了记忆,特将此记录. settings.xml 设置镜像方法步骤如下: 1. mirrors 设置 < ...