1381: Munching(BFS)
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;
}
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;
}
#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)的更多相关文章
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 【BZOJ-1656】The Grove 树木 BFS + 射线法
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 186 Solved: 118[Su ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- Sicily 1215: 脱离地牢(BFS)
这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...
- Sicily 1048: Inverso(BFS)
题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...
- Sicily 1444: Prime Path(BFS)
题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...
- Sicily 1051: 魔板(BFS+排重)
相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...
- Sicily 1150: 简单魔板(BFS)
此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...
随机推荐
- HDU 5769 Substring
后缀数组. 然后按照排序完成之后的顺序,每个后缀统计贡献量. 统计第i个后缀的贡献的时候,如果这个后缀中没有X,贡献度为0. 有贡献的分3种情况考虑: 1.如果这个后缀height部分等于0(即与前一 ...
- php错误记录
1.模板不存在ThinkPHP\Library\Think\View.class.php LINE: 110 是因为IndexController的Index函数,而View中没有对应的Index文件 ...
- js第一天 点击按钮显示与隐藏
<!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...
- div+css位置绝对定位和相对定位
position:absolute: 当div中被隔着些元素的话那么用此方法将把元素重叠在一起,所以元素可以不在容器中加也能重叠在一起
- kafka和flume的对比
摘要: (1)kafka和flume都是日志系统.kafka是分布式消息中间件,自带存储,提供push和pull存取数据功能.flume分为agent(数据采集器),collector(数据简单处理和 ...
- HDU2523:SORT AGAIN
Problem Description 给你N个整数,x1,x2...xn,任取两个整数组合得到|xi-xj|,(0<i,j<=N,i!=j). 现在请你计算第K大的组合数是哪个(一个组合 ...
- ocs添加仓库受限问题
添加仓库时受限出现以下问题 如图: 解决方法 修改app\ome\lib\branch\func.php文件的allow_use_num方法 /** * 允许使用的仓库数 * @access publ ...
- 网站的性能优化与安全(高效C#编码优化)
1. Foreach 比 For 性能高30%2. 避免是使用ArrayList, 因为任何对象到ArrayList都有封装为Object,出来还要拆箱. 用泛型去掉3. HashTalbe取代 ...
- Python 模块功能paramiko SSH 远程执行及远程下载
模块 paramiko paramiko是一个用于做远程控制的模块,使用该模块可以对远程服务器进行命令或文件操作,值得一说的是,fabric和ansible内部的远程管理就是使用的paramiko来现 ...
- Application的多种值判断测试程序
Application.Contents.Remove("FriendLink") Response.Write("Application.Contents.Remove ...