Lawnmower

题意翻译

# 题目描述 你有一个完全由草和杂草组成的花园。你的花园是一个 n×m的网格。每个方格有一对坐标(r,c)表示单元格位于r行c列。每个方格可能有草或杂草。例如,一个4×5的花园可能如下(空单元格表示草): ![](https://cdn.luogu.org/upload/vjudge_pic/CF115B/593291ddc8205e086d1d9f0caee6daf221cd4d06.png) 你有一台割草机可以割除所有的杂草。最初,你站在花园的左上角。也就是说,在方格(1,1)处。在任何时刻,你都面临着某个方向——左或右。最初,你面对右。 在一个步骤中,您可以执行以下任一操作: 1. 沿您面向的方向移动一个单元格。 - 如果你面向右:从方格(r,c )移动到方格(r,c + 1 ) ![](https://cdn.luogu.org/upload/vjudge_pic/CF115B/f511b6ec3d5ee7e9c4711b72b12f3f163a26b1cb.png) - 如果你面向左:从方格(r,c )移动到方格(r,c - 1 ) ![](https://cdn.luogu.org/upload/vjudge_pic/CF115B/817d99d95ad6751bb75b016614c67edbc38bc05f.png) 2) 向下移动一格(即从(r,c )移动到方格(r + 1,c )中),并将你的方向改为相反的方向. - 如果你面向右,你将面向左 ![](https://cdn.luogu.org/upload/vjudge_pic/CF115B/eaac793c8ad146f5aa886c6e03e5682029ae2d0f.png) - 如果你面向左,你将面对右 ![](https://cdn.luogu.org/upload/vjudge_pic/CF115B/0279ba704667c612234f39ddc6d6e73ff67745d6.png) 您不得离开花园。如果你和你的割草机站在含有杂草的方格中(你的方向无关紧要),杂草就会被修剪掉。此操作不算作动作。 割除所有杂草所需的最小移动次数是多少? ------------ # 输入输出格式 ### 输入格式: 第一行包含两个整数n和m(1<=N,M<=150)—— 分别为行数和列数。接下来n行包含每行m个字符——方格(r,c)的内容。“G”表示该方格含有草。“W”表示该方格含有杂草。 保证网格的左上角将包含草。 ### 输出格式: 输出一个数字——割除所有杂草所需的最小移动次数。

题目描述

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): ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF115B/593291ddc8205e086d1d9f0caee6daf221cd4d06.png)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) $ ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF115B/f511b6ec3d5ee7e9c4711b72b12f3f163a26b1cb.png) - if you are facing left: move from cell $ (r,c) $ to cell $ (r,c-1) $ ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF115B/817d99d95ad6751bb75b016614c67edbc38bc05f.png) 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 ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF115B/eaac793c8ad146f5aa886c6e03e5682029ae2d0f.png) - if you were facing left previously, you will face right ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF115B/0279ba704667c612234f39ddc6d6e73ff67745d6.png) 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?

输入输出格式

输入格式


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.

输出格式


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

输入输出样例

输入样例 #1

4 5
GWGGW
GGWGG
GWGGG
WGGGG

输出样例 #1

11

输入样例 #2

3 3
GWW
WWW
WWG

输出样例 #2

7

输入样例 #3

1 1
G

输出样例 #3

0

说明

For the first example, this is the picture of the initial state of the grid: ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF115B/a51efd23c5bfa4baf6ef0e23e14eb6170b199d79.png)A possible solution is by mowing the weeds as illustrated below: ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF115B/15b84025b88eba28511a1ee3af7d5faed84e0e48.png)