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)

  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

Copy

4 5
GWGGW
GGWGG
GWGGG
WGGGG

output

Copy

11

input

Copy

3 3
GWW
WWW
WWG

output

Copy

7

input

Copy

1 1
G

output

Copy

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:

题目大意

一个人割草,输入一个n×mn \times mn×m的图,G代表空地,W代表草,奇数行只能往右走,偶数行只能往左走。问割完草的最少步数。

题目分析

之前各种想,各种特殊情况处理特判……其实只要知道一个矩形图,一个角走到另一个角,只要不往回走,其实走的路径就是一样的。不用考虑一定是往那条路走。那么只要模拟一下走的过程,记录一下步数就行了。我们从左上角走,然后往右遍历找到就计算一下距离,更新一下当前的位置,然后第二行从右往左扫。第三行从左往右扫,如此循环即可。

代码

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef struct{int x, y;
}node;
typedef long long ll;
const int inf = 0x3f3f3f3f;
int n, m, x, y, k;
ll f(node v){int a = inf, b = inf;if(v.x < 0)a = (x - 1) / abs(v.x);else if(v.x > 0)a = (n - x) / abs(v.x);if(v.y < 0)b = (y - 1) / abs(v.y);else if(v.y > 0)b = (m - y) / abs(v.y);int mn = min(a, b);x += mn * v.x; y += mn * v.y;return mn;
}
int main(int argc, char const *argv[]) {scanf("%d%d%d%d", &n, &m, &x, &y);scanf("%d", &k);ll ans = 0;for(int i = 0; i < k; i++){node t;scanf("%d%d", &t.x, &t.y);ans += f(t);}printf("%I64d\n", ans);return 0;
}

训练赛1_E_Lawnmower相关推荐

  1. 2021年度训练联盟热身训练赛第四场 H - Rock Paper Scissors(字符串匹配,FFT)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 2021年度训练联盟热身训练赛第四场 H - Rock Paper Scissors(字符串匹配,FF ...

  2. ACM训练赛--递推专题

    1001: Buy the Ticket Problem Description The "Harry Potter and the Goblet of Fire" will be ...

  3. 校内训练赛题解第三篇

    校内训练赛题解 人气估值 解题思路 脑力训练计划 (模拟 + 字符串) 解题思路 大暑赛期(贪心 + 思维) 人气估值 题目描述 你是某动画制作公司的企划部长.如今动画制作公司制作的东西,已经不仅仅局 ...

  4. DataFountain训练赛 | 用户逾期行为预测

    @Author:Runsen @Date:2020/9/2 为了把简历写的没那么难看,决定在搞定动态规划先,顺便搞一个简单训练赛,不然没有比赛,简历还真的不能过去. 信贷用户逾期预测是新手入门必备的一 ...

  5. 数据挖掘竞赛-北京PM2.5浓度回归分析训练赛

    北京PM2.5浓度回归分析训练赛 简介 DC上的一个回归题,比较简单. 时间原因没有细看,提交到70多名就结束了. 使用stacking方法结合多个回归模型. 过程 数据获取 官方给定. 数据探索 训 ...

  6. 数据挖掘竞赛-美国King County房价预测训练赛

    美国King County房价预测训练赛 简介 DC上的一个回归题(正经的回归题). 比较简单. 时间原因(暂时没什么时间看国内旧赛),看了一下网上的解答,改善了一下神经网络就提交了. 过程 数据获取 ...

  7. 数据挖掘竞赛-轴承故障检测训练赛

    轴承故障检测 简述 DC上的一个训练赛,简单的多分类问题.说实话,还是比较有意思的,虽然很多人正确率都达到了1(也就是测试集预测结果全过),但是如果训练集和测试集数据量加大,那么这个结果可能就不是这样 ...

  8. 训练不出结果_训练赛惨败SKT?FPX直播透露拿冠军原因!Karsa再谈离开RNG?

    都说人逢喜事精神爽,小凤凰拿了世界冠军当然更爽.一回国就开启了快乐直播,人均阴阳怪气,信息量巨大,让我们一起来康康吧! "刘青松赢了装自己很淡定,其实耳机里叫得最大声."" ...

  9. 【ECJTU_ACM 11级队员2012年暑假训练赛(8) - F - A Mame】

    Home Problems Status Contest Register Login B题要套一个数论的模版,注意m=1!! C题可以二分匹配,把行列看作点; 不能开百度,开谷歌搜题解,再次强调!一 ...

最新文章

  1. pycharm设置编写的脚本页面长行实现自动换行(windows版)
  2. c语言趣味教学题目及答案,C语言趣味程序设计-题目百例.doc
  3. 产品打包工具的制作,ant,编译源码,打jar包,打tag,打war包,备份release版本等
  4. POJ - 2826 An Easy Problem?!(计算几何,好题)
  5. 音视频技术开发周刊 63期
  6. div后来居上 html,【CSS】误解:后来居上??有时这是错的
  7. php 旋转图片 并保存,如何在PHP中旋转并保存图像
  8. c语言pwm调制方式,pwm调制原理同步调制_几种pwm调制方式介绍 - 全文
  9. H264 RTP头分析
  10. kubernetes 学习 pod相关
  11. UI设计素材|图标在UI设计界面当中起到什么作用
  12. 常用算法大全-贪婪算法
  13. CentOS 5 常见的configure error的解决方法
  14. opencv-4.5.5-vc14_vc15配置环境VC++
  15. 惠普HP LaserJet 1160 驱动
  16. mysql数据库引擎问题
  17. 千姿百态项目经理2——“缥缈”项目经理
  18. 曲苑杂坛--修改数据库服务器名称
  19. ai旋转扭曲_AI变换及旋转图形工具详解
  20. 高斯投影法正反算代码MATLAB版本

热门文章

  1. 计算机如何连接网络扫描仪,如何添加局域网网络扫描仪
  2. Android接入高德地图SDK如何设置Key(发布版/测试版)
  3. 概率漫谈 转自 林达华
  4. IBM X 3650 M3服务器RAID0设置
  5. c语言编程练习题及答案
  6. RIPv1 RIPv2
  7. 点餐系统部署文档,java后台部署和微信点餐小程序的部署
  8. CSDN(编程的开始)
  9. 计算错误可以用计算机ac,计算器AC是什么键?
  10. localtime 和 localtime_r