题目描述

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=155,M=1000005;
int n,m,mp[N][N],cnt;
char s;
int main(){scanf("%d%d",&n,&m);For(i,1,n) For(j,1,m) {cin>>s;if(s=='W') mp[i][++mp[i][0]]=j;}int ans=0,lsx=1,lsy=1;For(i,1,n) {if(i&1) {if(mp[i][0]){ans+=(abs(i-lsx)+abs(mp[i][1]-lsy));ans+=(abs(mp[i][mp[i][0]]-mp[i][1]));lsx=i,lsy=mp[i][mp[i][0]],cnt++;}}else {if(mp[i][0]){ans+=(abs(i-lsx)+abs(mp[i][mp[i][0]]-lsy));ans+=(abs(mp[i][mp[i][0]]-mp[i][1]));lsx=i,lsy=mp[i][1],cnt++;}}}cout<<ans;return 0;
}

转载于:https://www.cnblogs.com/five20/p/9323154.html

CF115B Lawnmower相关推荐

  1. CF115B Lawnmower(贪心)

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

  2. 训练赛一:bfs广搜题目 CF115B Lawnmower

    CF115B Lawnmower time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  3. 为什么需要api产品经理

    It is very possible that your company is doing APIs wrong. Your organization may conflate REST with ...

  4. Lawnmower(洛谷 CF115B)

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

  5. Lawnmower(除草)

    Lawnmower You have a garden consisting entirely of grass and weeds. Your garden is described by an n ...

  6. Codeforces 115 B Lawnmower【思维】

    B. Lawnmower time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  7. CF 115B. Lawnmower

    CF 115B. Lawnmower  http://www.codeforces.com/problemset/problem/115/B 贪心.由于每次必须将整行的走完,而且不能变换方向,由此可知 ...

  8. [Codeforces 115B] Lawnmower

    题目传送门:点击传送 Time limit per test:2 seconds Memory limit per test:256 megabytes Description You have a ...

  9. zzd 的割草机(Lawnmower)

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

最新文章

  1. android 顺序执行任务
  2. Python中随机森林的实现与解释
  3. statistics DATA in CHINA
  4. WCF 服务端+客户端动态调用
  5. 【laravel】【转发】laravel 导入导出excel文档
  6. P1422 小玉家的电费--2022.03.15
  7. Node.js读取mongoDB并输出json数据
  8. 十八.搭建Nginx服务器、配置网页认证、基于域名的虚拟主机、ssl虚拟主机
  9. 【转】Windows和Ubuntu双系统,修复UEFI引导的两种办法
  10. Puget Systems发布硬件可靠性报告,三星SSD表现低故障率
  11. STM32之红外接收
  12. python 绘制损失函数曲线_绘制loss曲线
  13. 【ROS】ubuntu20.04+ROS安装上遇到的坑(主要是time out)
  14. 基于软总线的实时组件调度技术研究
  15. 如何解决Paypal多账号登录账户关联?
  16. “要吃鲷鱼到岛上钓”团队小结
  17. AcceptEx函数的示例代码
  18. 如何架设你自己的个人网站
  19. 金 融 量 化 分 析 • JoinQuant • 第 六 篇
  20. 如何利用ArcGIS绘制国界线/省界线

热门文章

  1. Shiro登录的使用以及原理(一)
  2. 用python发邮件便利之处_第18课 python 发送邮件
  3. <塞梅普雷斯 如是说> 第二部 0.序
  4. component动态组件
  5. ROWNUM和ROWID的认识
  6. android访问win10共享文件夹,手机怎么用ES浏览器访问Win10共享文件教程
  7. BUUCTF-社团考核
  8. 使用jQuery制作图书简介
  9. 超级账本hyperledger fabric第五集:共识排序及源码阅读
  10. HTML5期末大作业:旅游酒店网站设计——旅游酒店服务预订(1页) web网页设计—— 出游