B. 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
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:

题目大意:

按照题干中的四种方式走,只要向下走一行,方向就会转变,问最少走的步数能够除掉所有的草。

思路:

1、对应初始的时候是向右走的,首先需要向右走走到最右边的那个W的位子。

2、然后继续判断下一行,如果下一行有W在当前位子右边,还要推着小车向右多走几步。

3、对应同理,如果在向左走的时候,在当前行首先我们走到最左边的那个W的位子,然后再判断下一行,如果下一行有W在当前位子的左边,那么我们还要推着小车向左多走几步。

4、注意当割完所有草之后要跳出,要不然会有多余的行走步数。

Ac代码:

#include<stdio.h>
#include<string.h>
using namespace std;
char a[165][165];
int n,m;
int abs(int aaa)
{if(aaa<0)return -aaa;else return aaa;
}
int main()
{while(~scanf("%d%d",&n,&m)){int sum=0;for(int i=0; i<n; i++){scanf("%s",a[i]);for(int j=0; j<m; j++){if(a[i][j]=='W')sum++;}}int d=1;int tmp=0;int output=0;int y=0;for(int i=0; i<n; i++){int maxn=-1;//printf("%d %d %d\n",y,tmp,output);if(tmp==sum)break;for(int j=0; j<m; j++){if(a[i][j]=='W'){if(d==1)maxn=j;else{if(maxn==-1)maxn=j;}}if(a[i+1][j]=='W'){if(d==1)maxn=j;else{if(maxn==-1)maxn=j;}}}if(maxn==-1){output+=1;d=1-d;continue;}else{if(d==1){d=0;output+=abs(y-maxn);for(int z=0; z<m; z++){if(a[i][z]=='W')tmp++;}y=maxn;if(i!=n-1&&tmp!=sum)output++;}else{d=1;output+=abs(y-maxn);for(int z=0; z<m; z++){if(a[i][z]=='W')tmp++;}y=maxn;if(i!=n-1&&tmp!=sum)output++;}}}printf("%d\n",output);}
}

Codeforces 115 B Lawnmower【思维】相关推荐

  1. CodeForces - 1593G Changing Brackets(思维)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的括号序列,其中包含了 {(,),[,]}\{(,),[,]\}{(,),[,]} 四种括号,现在可以进行两种操作: 将括号反转,代价为 000, ...

  2. CodeForces - 1567C Carrying Conundrum(思维/状压)

    题目链接:点击查看 题目大意:规定加法中使用隔项进位,问给定的 nnn 有多少种方案可以通过 "隔项进位加法" 得到 题目分析:隔项进位意味着奇偶位置的数字互不影响,所以将奇偶位置 ...

  3. CodeForces - 1535C Unstable String(思维)

    题目链接:点击查看 题目大意:规定一个字符串将问号都替换成 000 或 111 后满足 010101 交替的话,该字符串是合法的,现在给出一个长度为 nnn 的字符串,求合法子串的个数 题目分析:两种 ...

  4. CodeForces - 1353E K-periodic Garland(思维+dp)

    题目链接:点击查看 题目大意:给出 n 个灯泡以及其初始状态(开或关),每次操作可以将任意一个灯泡的状态置反,问最少需要操作多少次,可以使得所有开着的灯泡之间相距 k 个单位 题目分析:因为需要满足所 ...

  5. CodeForces - 1323B Count Subrectangles(思维)

    题目链接:点击查看 题目大意:给出一个数组 a 和数组 b 只由 0 和 1 构成,构造出矩阵 maze[ x ][ y ] = a[ x ] * b[ y ],显然maze矩阵同样只由 0 和 1 ...

  6. CodeForces - 1321B Journey Planning(思维)

    题目链接:点击查看 题目大意:给出一个长度为 n 的数列,规定本题中的上升子序列必须满足两个条件: a[ j ] < a[ i ] a[ i ] - a[ j ] = i - j 问累加和最大的 ...

  7. CodeForces - 1066C Books Queries(思维)

    题目链接:点击查看 题目大意:给出n次操作,每次操作分为以下三种:(假设现在有一个空的队列) L x,在最左端插入x R x,在最右端插入x ? x,查询若想要让x到达最右端或最左端,最少需要移除掉几 ...

  8. CF--思维练习-- CodeForces - 215C - Crosses(思维题)

    ACM思维题训练集合 There is a board with a grid consisting of n rows and m columns, the rows are numbered fr ...

  9. CF思维联系--CodeForces -214C (拓扑排序+思维+贪心)

    ACM思维题训练集合 Furik and Rubik love playing computer games. Furik has recently found a new game that gre ...

最新文章

  1. 第七届“数学、计算机与生命科学交叉研究” 青年学者论坛
  2. PostgreSQL — 常规操作
  3. Cordova项目怎样获取项目版本号
  4. 【机器学习】 - keras中的模型可视化plot_model模块(含依赖包pydot和graphviz的详细安装过程与注意事项)
  5. VS Code (visual studio code) VSC 编辑器(微软出品,js开发的编辑器)
  6. 织梦众大云采集插件v9.7
  7. 响应式web之@media screen
  8. 韩顺平php视频笔记36 php基本语法
  9. python怎么把excel单元格里面的文字提取出来_干货 | Excel如何花式秀操作?
  10. php git pull
  11. Arcgis Javascript那些事儿(四)--feature access服务编辑feature本质
  12. vue2+vuex+vue-router 快速入门(三) vue 实例介绍
  13. asp生成和导出excel和word数据源码和代码,简单好用(已经测试可以用)
  14. Unity3D笔记 愤怒的小鸟二 实现Play界面
  15. 过度使用微信,正在让人越来越焦虑
  16. 好用的FTP下载工具 flashfxp工具
  17. 连点器安卓手机版_万代奥特曼变身器安卓版下载_万代奥特曼dx变身器安卓手机版下载 v1.0.0...
  18. dns解析失败如何处理?
  19. 9种 『MySQL数据库』优化的正确姿势
  20. POJ1555-Polynomial Showdown

热门文章

  1. HTML登录注册页面简单实现
  2. 聊一聊HTTPS双向认证的简单应用
  3. PCIe ARI (Alternative Routing-ID Interpretation)介绍
  4. java鬼混笔记:springboot 5、springboot的Scheduled定时器:fixedDelay和fixedRate区别
  5. img标签图片的刷新,删除
  6. 聚焦手机操作系统 运营商的“门户”之战
  7. Docker容器——重命名镜像的TAG
  8. Altium Designer布局布线技巧分享
  9. 主成分分析(Principal Component Analysis,PCA)
  10. LRUCache算法