CF115B.Lawnmower
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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:
- 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)

- 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?
你的花园完全由草和杂草组成。花园用一个 n×m 的网格表示,行号从上到下依次为 1 到 n,列号从左到右依次为 1 到 m。每个格子用一对坐标 (r,c) 表示,即该格子位于第 r 行、第 c 列。每个格子中要么是草,要么是杂草。例如,一个 4×5 的花园可能如下所示(空格表示草):

你手中有一台割草机,用于清除所有杂草。初始时,你和割草机位于花园的左上角,即格子 (1,1)。在任意时刻,你都面向某个方向——要么向左,要么向右;初始时你面向右。
每次操作你可以执行以下两种动作之一:
- 向当前面向的方向移动一格。
-
若你面向右:从格子 (r,c) 移动到格子 (r,c+1)

-
若你面向左:从格子 (r,c) 移动到格子 (r,c−1)

- 向下移动一格(即从格子 (r,c) 移动到格子 (r+1,c)),并使你的朝向变为相反方向。
-
若之前面向右,则之后面向左

-
若之前面向左,则之后面向右

你不允许离开花园边界。当你和割草机站在含有杂草的格子上时,该处杂草即被清除(朝向无关紧要)。此清除动作不计为一次操作。
问:清除所有杂草所需的最少操作次数是多少?
输入格式
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.
第一行包含两个整数 n 和 m(1≤n,m≤150),分别表示网格的行数和列数。接下来是 n 行,每行包含 m 个字符——表示网格的内容。“G” 表示该格子中长有草,“W” 表示该格子中长有杂草。
保证网格的左上角格子中长有草。
输出格式
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:

A possible solution is by mowing the weeds as illustrated below:

对于第一个样例,以下是网格初始状态的示意图:

一种可能的解法是按如下方式清除杂草:

输入解题思路,AI测评打分。不知道怎么写?