训练赛一:bfs广搜题目 CF115B Lawnmower

CF115B Lawnmower
time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output

You have a garden consisting entirely of grass and weeds. Your garden is described by an n × m grid, with rows numbered 1 to n from top to bottom, and columns 1 to m from left to right. Each cell is identified by a pair (r, c) which means that the cell is located at row r and column c. Each cell may contain either grass or weeds. For example, a 4 × 5 garden may look as follows (empty cells denote grass):
在这里插入图片描述
You have a land-mower with you to mow all the weeds. Initially, you are standing with your lawnmower at the top-left corner of the garden. That is, at cell (1, 1). At any moment of time you are facing a certain direction — either left or right. And initially, you face right.

In one move you can do either one of these:

  1. Move one cell in the direction that you are facing.

if you are facing right: move from cell (r, c) to cell (r, c + 1)
在这里插入图片描述
if you are facing left: move from cell (r, c) to cell (r, c - 1)
在这里插入图片描述
2) Move one cell down (that is, from cell (r, c) to cell (r + 1, c)), and change your direction to the opposite one.
if you were facing right previously, you will face left
在这里插入图片描述
if you were facing left previously, you will face right

在这里插入图片描述
You are not allowed to leave the garden. Weeds will be mowed if you and your lawnmower are standing at the cell containing the weeds (your direction doesn’t matter). This action isn’t counted as a move.

What is the minimum number of moves required to mow all the weeds?

Input
The first line contains two integers n and m (1 ≤ n, m ≤ 150) — the number of rows and columns respectively. Then follow n lines containing m characters each — the content of the grid. “G” means that this cell contains grass. “W” means that this cell contains weeds.

It is guaranteed that the top-left corner of the grid will contain grass.

Output
Print a single number — the minimum number of moves required to mow all the weeds.
Examples

输入
4 5
GWGGW
GGWGG
GWGGG
WGGGG
输出
11

Note
For the first example, this is the picture of the initial state of the grid:
在这里插入图片描述
A possible solution is by mowing the weeds as illustrated below:
在这里插入图片描述

题意

要你除草,把草除完,问最少的步数。
走的方式:起点已经确定,就在左上角,每一行的方向是确定的,踩到草所在的格子就能够除草,往下走一行会改变前进的方向。

思路

看到题目求最少的步数,并且地图很小个,那可以用广搜做。

每行的除草,如果当前第n行已经除完了,在进入下一行n+1行之前,得确保进入下一行导致除草的方向变了之后,这第n+1行没有草是在背后的,不然就除漏了。

这里要注意的是,有可能除完某个草后,后面还有很多行,但是都是没有草的,这个时候要判断一下(用一个数组存每一行的草数),每次除完当前行后都判断一下,提前结束,避免继续走下去多算了步数。

代码

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
typedef pair<int,int>p;
queue<p>que;
int n,m;//150
char a[155][155];
int w[155];
int sx=0,sy=0;
int l[155],r[155];
int main()
{
	memset(w,0,sizeof(w));
	memset(l,99,sizeof(l));
	memset(r,-1,sizeof(-1));
	scanf("%d %d",&n,&m);
	for(int i=0;i<n;i++)
		scanf("%s",&a[i]);
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			if(a[i][j]=='W')
			{
				l[i]=min(l[i],j);
				r[i]=max(r[i],j);
				w[i]++;
			}	
	que.push(p(sx,sy));
	int step=-1;
	while(!que.empty())
		{	
			p now=que.front();
			que.pop();
			int nx=now.first,ny=now.second;
			int flag=0;
			for(int i=nx;i<n;i++)
				if(w[i]>0)
					flag=1;	
			if(flag==0)
				break;
			if(nx<0||nx>=n||ny<0||ny>=m)//当前行是否出界?
				continue;
			step++;
			if(a[nx][ny]=='W')
				w[nx]--;
			if(w[nx]>0)//当前行是否除完?没有 
			{
				if(nx%2==0)//奇
					que.push(p(nx,ny+1));
				else
					que.push(p(nx,ny-1));
			}
			else if(w[nx]==0&&w[nx+1]>0)//除完了,看下一行(奇)的最前面/下一行(偶)最后面一个不在下面 
			{
				//不仅要看下一行的,还要看后面所有行的是否还有草,没有的话就可以停了
				if(nx%2==0&&r[nx+1]>ny)//奇
				{
					que.push(p(nx,ny+1));
					continue;
				}
				if(nx%2!=0&&l[nx+1]<ny){
					que.push(p(nx,ny-1));
					continue;
				}
				que.push(p(nx+1,ny));
			} 
			else
				que.push(p(nx+1,ny));
		}
		if(step==-1)
			printf("0\n");
		else
			printf("%d\n",step);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值