1381: Munching(BFS)
来源:互联网 发布:免费的域名解析软件 编辑:程序博客网 时间:2024/05/29 09:22
Description
Bessie loves her grass and loves to hurry to the barn for her eveningmilking session. She has partitioned the pasture into a rectilineargrid of R (1 <= R <= 100) rows and C (1 <= C <= 100) columns andmarked the squares as grass or rocky (she can't eat rocks and won'teven go into those squares). Bessie starts on her map at locationR_b,C_b and wishes to munch her way, square by square, to the barnat location 1,1 by the shortest route possible. She moves from onesquare 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 mapwith 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 * . . * . mBessie munched 9 squares. Given a map, determine how many squares Bessie will eat on hershortest route to the barn (there's no grass to eat on the barn'ssquare).
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 6B...*...*....**.*...***.*..*.C
9
#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 eveningmilking session. She has partitioned the pasture into a rectilineargrid of R (1 <= R <= 100) rows and C (1 <= C <= 100) columns andmarked the squares as grass or rocky (she can't eat rocks and won'teven go into those squares). Bessie starts on her map at locationR_b,C_b and wishes to munch her way, square by square, to the barnat location 1,1 by the shortest route possible. She moves from onesquare 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 mapwith 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 * . . * . mBessie munched 9 squares. Given a map, determine how many squares Bessie will eat on hershortest route to the barn (there's no grass to eat on the barn'ssquare).
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 6B...*...*....**.*...***.*..*.C
9
#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;}
0 0
- 1381: Munching(BFS)
- BFS
- bfs
- BFS
- BFS
- bfs
- BFS
- bfs
- BFS
- BFS
- bfs
- BFS
- BFS
- BFS
- BFS
- BFS
- bfs
- BFS
- 获取沙盒文件夹路径
- 第一章 JavaScript基础
- Bulkload Hive表到HBase
- linux系统中内存爆满之后会如何?
- 二叉树的构造(一)
- 1381: Munching(BFS)
- bsoj: 2857 -- 【2011福建】TT的身体
- 简单的NFA转DFA的代码实现
- MyBatis主配置文件SqlMapConfig.xml
- logcat不显示信息
- 表单
- MySQL基本概念
- CodeForces - 659C Tanya and Toys (map&模拟)
- 标准C++中的string类的用法总结