题目描述

You have a garden consisting entirely of grass and weeds. Your garden is described by an n×mn×m grid, with rows numbered 11 to nn from top to bottom, and columns 11 to mm from left to right. Each cell is identified by a pair (r,c)(r,c)which means that the cell is located at row rr and column cc . Each cell may contain either grass or weeds. For example, a 4×54×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)(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)(r,c) to cell (r,c+1)(r,c+1) 
  • if you are facing left: move from cell (r,c)(r,c) to cell (r,c-1)(r,c−1) 

    2) Move one cell down (that is, from cell (r,c)(r,c) to cell (r+1,c)(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?

输入输出格式

输入格式:

The first line contains two integers nn and mm ( 1<=n,m<=1501<=n,m<=150 ) — the number of rows and columns respectively. Then follow nn lines containing mm 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:

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

Solution:

  本题比较水,直接贪心模拟就好了。

  题意就是从$(1,1)$出发,最少需要多少步能除完草(即$W$),而每次移动的方向已经确定了,当$i$为奇数则第$i$行的方向为$1\rightarrow m$,否则为$m\rightarrow 1$,每次下移必须转变到所到行的方向上。

  只需要记录一下每一行中草的位置,然后按行的方向模拟找到两边界(要么是一行草的开头位置,要么是结尾位置,具体由该行的方向确定),然后直接算曼哈顿距离,求和就解决了。

代码:

#include<bits/stdc++.h>
#pragma GCC optimize(2)
#define il inline
#define ll long long
#define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)
using namespace std;
const int N=,M=;
int n,m,mp[N][N],cnt;
char s;
int main(){
scanf("%d%d",&n,&m);
For(i,,n) For(j,,m) {
cin>>s;
if(s=='W') mp[i][++mp[i][]]=j;
}
int ans=,lsx=,lsy=;
For(i,,n) {
if(i&) {
if(mp[i][]){
ans+=(abs(i-lsx)+abs(mp[i][]-lsy));
ans+=(abs(mp[i][mp[i][]]-mp[i][]));
lsx=i,lsy=mp[i][mp[i][]],cnt++;
}
}
else {
if(mp[i][]){
ans+=(abs(i-lsx)+abs(mp[i][mp[i][]]-lsy));
ans+=(abs(mp[i][mp[i][]]-mp[i][]));
lsx=i,lsy=mp[i][],cnt++;
}
}
}
cout<<ans;
return ;
}

CF115B Lawnmower的更多相关文章

  1. CF115B Lawnmower(贪心)

    CF115B Lawnmower \(solution:\) 很明显的一道贪心题,奇数行只能向左走,偶数行只能向右走,每一行的起点应该在上一行就已确定,而这一行的终点只和(这一行最后一棵草(相对于你走 ...

  2. Lawnmower(洛谷 CF115B)

    题目看这里 题目大意 简单来讲就是从(1,1)向左或右或下走,经过所有草坪的最短路程 思路: 由于在第一行只能向右走,那么我们就知道,在单数行和双数行分别是向右走和向左走,那么我们在单数行就只需要统计 ...

  3. CF 115B Lawnmower(贪心)

    题目链接: 传送门 Lawnmower time limit per test:2 second     memory limit per test:256 megabytes Description ...

  4. zzd 的割草机(Lawnmower)

    评测传送门 [题目描述] 已知花坛为一个 n * m 的矩形,草只会长在某些个格子上,zzd 有一个割草机,一开始,zzd 站在(1,1)处,面向(1,m)(面向右).每次 zzd 有两个选择(耗费一 ...

  5. C# 语言规范_版本5.0 (第7章 表达式)

    1. 表达式 表达式是一个运算符和操作数的序列.本章定义语法.操作数和运算符的计算顺序以及表达式的含义. 1.1 表达式的分类 一个表达式可归类为下列类别之一: 值.每个值都有关联的类型. 变量.每个 ...

  6. C#6.0语言规范(七) 表达式

    表达式是运算符和操作数的序列.本章定义了操作数和运算符的语法,求值顺序以及表达式的含义. 表达式分类 表达式分类为以下之一: 一个值.每个值都有一个关联的类型. 一个变量.每个变量都有一个关联的类型, ...

  7. C# 各版本新特性

    C# 2.0 泛型(Generics) 泛型是CLR 2.0中引入的最重要的新特性,使得可以在类.方法中对使用的类型进行参数化. 例如,这里定义了一个泛型类: class MyCollection&l ...

  8. NCE3

    Lesson1  A puma at large Pumas are large, cat-like animals which are found in America. When reports ...

  9. CIFAR10/CIFAR100数据集介绍

    CIFAR-10/CIFAR-100数据集解析 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 CIFAR-10/CIFAR-100数据集 CIFAR-10和CIFAR-100被标记 ...

随机推荐

  1. SI - 硬件 - 服务器 - 知识科普

    服务器对每个从事IT工作的人来说并不陌生,但是服务器所涉及的各种知识细节,并非大家都十分清楚,为了让大家深入了解服务器的关键知识点,笔者特意抽时间总结了这篇科普文章,旨在帮助读者全面了解服务器.今天内 ...

  2. java程序执行系统命令

    String cmd="orakill orcl 1233";//解锁数据库表 Process proc = Runtime.getRuntime().exec(cmd);

  3. JS如何给ul下的所有li绑定点击事件,点击使其弹出下标和内容

    这是一个非常常见的面试题,出题方式多样,但考察点相同,下面我们来看看这几种方法:方法一: var itemli = document.getElementsByTagName("li&quo ...

  4. JavaScript -- 继承与原型链

    JavaScript对象有一个指向一个原型对象的链,当试图访问一个对象的属性的时候,他不仅仅会在该对象上面搜寻,还会搜寻该对象的原型,以及对象的原型的原型,依次层层搜索,直到找到名字匹配的属性或者到达 ...

  5. [原]解决phpstudy下的nginx无法运行的问题

    一直在用phpstudy下的apache,今天忽然想切换到nginx,出现了一些错误,最终还是解决了. 之前是php 5.3 + apache 现在是php 5.3n + nginx 问题就出在这n上 ...

  6. Python的scrapy之爬取boss直聘网站

    在我们的项目中,单单分析一个51job网站的工作职位可能爬取结果不太理想,所以我又爬取了boss直聘网的工作,不过boss直聘的网站一次只能展示300个职位,所以我们一次也只能爬取300个职位. jo ...

  7. Mysql通过Adjacency List(邻接表)存储树形结构

    转载自:https://www.jb51.net/article/130222.htm 以下内容给大家介绍了MYSQL通过Adjacency List (邻接表)来存储树形结构的过程介绍和解决办法,并 ...

  8. R语言学习笔记(五):零碎知识点(11-15)

    11--which.min(), which.max()和which() which(x, arr.ind = FALSE, useNames = TRUE) x 是一个向量或者数组,可以是NA,但会 ...

  9. 10-mongodb启动错误

    1.error信息 python@ubuntu:~$ mongod --22T17:: I CONTROL [initandlisten] MongoDB starting : pid= port= ...

  10. oracle 认识

    有一家叫甲骨文的粮店,老板很严谨,为了防止仓库的粮食在买入卖出的时候发生问题,他制订一套流程,首先进出仓库的每一旦粮食都要求有一个编号(SCN),而且出入库之前必须先放到一个平台上(buffer ca ...