Lawnmower
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)

  1. 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
Input
4 5
GWGGW
GGWGG
GWGGG
WGGGG
Output
11
Input
3 3
GWW
WWW
WWG
Output
7
Input
1 1
G
Output
0


划重点:他每走一行会换方向

#include <iostream>
#include <algorithm>
#include <cstring>using namespace std ;
//奇数行向右走
//偶数行向左走
int main()
{int n , m ; char a[160][160] ;cin >> n >> m ;for(int i = 0 ; i < n ; i ++)for(int j = 0 ; j < m ; j ++)cin >> a[i][j] ;int x = 0 , y = 0 ;//记录每一行的停留点int ans = 0 ;int j ;for(int i = 0 ; i < n; i ++)//每一行的,记录最后一株草的位置{if(i%2==0)j = 0 ;elsej = m-1 ; while(j >= 0 && j < m){if(a[i][j]=='W'){ans += abs(x-i)+abs(y-j) ;x = i , y = j ;}    if(i%2==0)j ++ ;elsej -- ;}}cout << ans << endl ;return 0 ;
}

Lawnmower(除草)相关推荐

  1. [Codeforces 115B] Lawnmower

    题目传送门:点击传送 Time limit per test:2 seconds Memory limit per test:256 megabytes Description You have a ...

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

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

  3. 【笔记】基于Faster R-CNN的除草机器人杂草识别算法

    <基于Faster R-CNN的除草机器人杂草识别算法> 单位:河北科技大学信息科学与工程学院 作者:李春明 数据获取 相机:Intel RealSense Depth CameraD43 ...

  4. CF115B Lawnmower

    题目描述 You have a garden consisting entirely of grass and weeds. Your garden is described by an n×mn×m ...

  5. CF115B Lawnmower(贪心)

    CF115B Lawnmower \(solution:\) 很明显的一道贪心题,奇数行只能向左走,偶数行只能向右走,每一行的起点应该在上一行就已确定,而这一行的终点只和(这一行最后一棵草(相对于你走 ...

  6. Codeforces 115 B Lawnmower【思维】

    B. Lawnmower time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  7. CF 115B. Lawnmower

    CF 115B. Lawnmower  http://www.codeforces.com/problemset/problem/115/B 贪心.由于每次必须将整行的走完,而且不能变换方向,由此可知 ...

  8. zzd 的割草机(Lawnmower)

    评测传送门 [题目描述] 已知花坛为一个 n * m 的矩形,草只会长在某些个格子上,zzd 有一个割草机,一开始, zzd 站在(1,1)处,面向(1,m)(面向右).每次 zzd 有两个选择(耗费 ...

  9. 电动除草机驱动方案设计开发

    割草机,又叫打草机.割灌机,用途广泛.主要应用在园林装饰修剪.草地绿化修剪.城市街道.绿化景点.田园修剪.田地除草,特别是公园内的草地和草原,足球场等其他用草场地,私人别墅花园,以及农林畜牧场地植被等 ...

最新文章

  1. 图像去噪的深度学习最新综述论文,36页pdf,Deep Learning on Image Denoising
  2. 别再被 Python 洗脑了!!
  3. Xcode10 修改代码后,编译没有反应,或者导入头文件没有提示
  4. 多路径下使用ASMLIB创建ASM磁盘
  5. Springboot 解决跨域的四种姿势
  6. mac os系统使用Visual Studio Code打开浏览器查看HTML文件
  7. MySQL主从延时这么长,要怎么优化?
  8. 车牌识别训练出现问题
  9. 【记录贴】cs231n课程作业一遇到问题总结
  10. 练习-CSS3 多栏(Multi-column)
  11. [转载] Python - filter()用法
  12. JDK 1.5 HashMap 源代码读解
  13. Java后端开发常用规范
  14. EXCEL中数据分析涉及的一些操作
  15. 护眼灯频闪是什么意思?无频闪护眼灯哪个好
  16. java防止文件上传_文件上传漏洞:getshell的最好方式,我们如何防御?
  17. H263H264MPEG4
  18. python爬虫一:爬虫简介
  19. ddtv.space index.php,图解MongoDB原理(二)
  20. pta数据结构-线性表(判断题和选择题)

热门文章

  1. Python用try-except的时候获取错误行信息和文件信息
  2. 汇编语言: 试编制一个程序:从键盘输入一行字符,要求第一个键入的字符必须是空格符,如不 是,则退出程序;如是,则开始接收键入的字符并顺序存放在首地址为buffer的缓冲区中(空 格符不存入),直到接收
  3. 内核提速开机linux,Linux系统开机提速我有招!
  4. social-GAN
  5. 【强化学习】Q-Learning算法求解悬崖行走问题 + Python代码实战
  6. Javaweb后端开发必学(HTML、CSS、JS、Vue)
  7. win怎么查看显示器大小,显示器是多少寸,查看显示器尺寸
  8. java 判断当前月天数
  9. 如何更好的做线上引流
  10. python open encoding=utf-8_Python 文件操作中的读写模式:open(path, '-模式-',encoding='UTF-8')...