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. BaLaBaLa

    #include<cstdio>#include<cstring>#include<cmath>#include<queue>#include<a ...

  2. ios简单实现如果没有开启定位,提示开启系统软件定位功能

    if([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] == ...

  3. c语言_头文件

    传统 C++ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <assert.h> //设定插入点 #include <ctyp ...

  4. Android 调试工具集【转】

    1.TraceView1)功能:用于热点分析和性能优化,分析每个函数占用的CPU时间,调用次数,函数调用关系等 2)方法: a)在程序代码中加入追踪开关 import android.os.Debug ...

  5. Storm官方文档翻译之创建Storm项目

    本文将讲述如何在开发环境创建一个Storm项目,下面是步骤: 1.将Storm的相关jar包添加到classpath中: 2.如果使用多语言开发,也要将多语言目录添加到classpath; 点击sto ...

  6. regular expression tutorial

    \d represent any number \D represents everything but a number \s represents any space \S Anything bu ...

  7. 使用Chrome DevTools的Timeline和Profiles提高Web应用程序的性能

    来源: http://www.oschina.net/translate/performance-optimisation-with-timeline-profiles 我们都希望创建高性能的Web应 ...

  8. iOS开发:后台运行以及保持程序在后台长时间运行

    第一部分 1.先说说iOS 应用程序5个状态: 停止运行-应用程序已经终止,或者还未启动. 不活动-应用程序处于前台但不再接收事件(例如,用户在app处于活动时锁住了设备). 活动-app处于“使用中 ...

  9. C/C++ - 结构体实际申请的空间

    C/C++ - 结构体实际申请的空间 如下的结构体,sizeof()大小,实际申请的空间以及理论上申请最佳空间 struct Spot { int x; int y; bool visible; in ...

  10. poi 合并单元格、设置边框

    HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); //创建一个样式 HSSFCellStyle sty ...