题目传送门:点击传送

Time limit per test:2 seconds

Memory limit per test:256 megabytes

Description

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

input

4 5
GWGGW
GGWGG
GWGGG
WGGGG

output

11

input

3 3
GWW
WWW
WWG

output

7

input

1 1
G

output

0

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:

解析:

要你除草:W是杂草。开始在左上角向右,每次移动只能朝一个方向(左或者右),换行的时候会转方向。最后要求最小移动步数

如图,因为要找最小路径,可能会想到除完某一行的草(i1,j1)就直接下走,这样下一行在j1的某一边杂草是除不了的

            所以不能单看一行来判断下走

解决办法是:把所有格子都走一遍,把两个杂草之间的距离相加即为答案(坐标差的绝对值之和)

(补充:有人跟我说这是叫曼哈顿距离,不懂不懂...)

代码

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
char a[155][155];
int main()
{int n,m,ans=0;scanf("%d%d",&n,&m);getchar();int i,j;for(i=1;i<=n;i++)   //读图{for(j=1;j<=m;j++)a[i][j]=getchar();getchar();}int lasti=1,lastj=1;for(i=1;i<=n;i++){if(i%2==1)      //奇数行向右走   偶数行向左走{for(j=1;j<=m;j++)if(a[i][j]=='W'){ans+=abs(i-lasti)+abs(j-lastj);lasti=i;lastj=j;}}else{for(j=m;j>0;j--)if(a[i][j]=='W'){ans+=abs(i-lasti)+abs(j-lastj);lasti=i;lastj=j;}}}printf("%d\n",ans);return 0;
}

[Codeforces 115B] Lawnmower相关推荐

  1. Codeforces 115B Lawnmower

    题意: 给你张n*m的图,图上W表示草,G表示平地,一开始你从左上角朝右开始推草,每往下走一步朝向相反,问你最少需要走几步才能推完所有的草. 思路: 因为奇数行和偶数行的朝向是固定的,所以即使该行已经 ...

  2. CF 115B. Lawnmower

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

  3. 为什么需要api产品经理

    It is very possible that your company is doing APIs wrong. Your organization may conflate REST with ...

  4. Codeforces 115 B Lawnmower【思维】

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

  5. CodeForces 375D Tree and Queries

    传送门:https://codeforces.com/problemset/problem/375/D 题意: 给你一颗有根树,树上每个节点都有其对应的颜色,有m次询问,每次问你以点v为父节点的子树内 ...

  6. 「日常训练」Bad Luck Island(Codeforces Round 301 Div.2 D)

    题意与分析(CodeForces 540D) 是一道概率dp题. 不过我没把它当dp做... 我就是凭着概率的直觉写的,还好这题不算难. 这题的重点在于考虑概率:他们喜相逢的概率是多少?考虑超几何分布 ...

  7. 【codeforces 812C】Sagheer and Nubian Market

    [题目链接]:http://codeforces.com/contest/812/problem/C [题意] 给你n个物品; 你可以选购k个物品;则 每个物品有一个基础价值; 然后还有一个附加价值; ...

  8. CodeForces 获得数据

    针对程序的输出可以看见 CodeForces :当输入.输出超过一定字符,会隐藏内容 所以:分若干个程序进行输入数据的获取 1. 1 for (i=1;i<=q;i++) 2 { 3 scanf ...

  9. codeforces水题100道 第二十七题 Codeforces Round #172 (Div. 2) A. Word Capitalization (strings)...

    题目链接:http://www.codeforces.com/problemset/problem/281/A 题意:将一个英文字母的首字母变成大写,然后输出. C++代码: #include < ...

最新文章

  1. idea debug使用
  2. mysql和redis的区别
  3. 有计算机二级证书当兵有好处吗,大学生拿到这个证书可享受的福利,当兵有什么不一样?...
  4. oracle正在没落,Oracle停掉一个正在运行的job
  5. 《化工原理》基本知识点
  6. [小程序]微信小程序获取input并发送网络请求
  7. SQL SERVER 2012修改数据库名称(包括 db.mdf 名称的修改)
  8. 工作172:自己工作开始封装的一个小组件
  9. Bootstrap 警告块
  10. 首期openGauss训练营结营,48个FAQ和全部PPT通通给你,随附62人结营学员名单
  11. 手机 html5 hammer drag widget,javascript – HTML5使用Hammer.js拖放事件拖放div上的元素
  12. 【Matlab绘图】plot3函数绘制三维点或线图
  13. java 三次样条插值_java – 三次样条插值的正确实现
  14. 干货|爬虫被封的几个常见原因
  15. Ubuntu20.04安装Mysql(亲测有效,一定要按步骤来)
  16. Linux目录一个点.和两个点..的区别
  17. 循环调用scrapy框架出现的问题:twisted.internet.error.ReactorNotRestartable,解决方法
  18. html火狐浏览器秒杀插件,火狐浏览器Firefox(已装载常用渗透插件) 2018-06
  19. one-hot和multi-hot编码
  20. 传世藏书 医部目录(1-6册)

热门文章

  1. torch.repeat()与numpy.repeat()和 numpy.tile()比较
  2. 仓储绩效评价指标的原则
  3. [渝粤教育] 中国地质大学 大学物理(上) 复习题 (2)
  4. 2009年5月30日
  5. c语言股价连续上涨的天数,为什么通达信没有连涨天数,我的通达信怎么没有连涨天数显示...
  6. 论文精读:Selective Convolutional Descriptor Aggregation
  7. 五大经典算法之四贪心算法
  8. B站弹幕姬,弹幕礼物感谢,关注感谢,自动回复,房管工具,房管助手,基于java
  9. 安卓手机的证书信任设置在哪_如何在Android设备上安装受信任的CA证书?
  10. python在国内外研究现状_机器人教育_国内外Python课程现状?