题目链接: 传送门

Lawnmower

time limit per test:2 second     memory limit per test:256 megabytes

Description

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.

Sample Input

4 5
GWGGW
GGWGG
GWGGG
WGGGG

3 3
GWW
WWW
WWG

1 1
G

Sample Output

11

7

0

解题思路:

题目大意:一块矩形草坪,除掉所有杂草,问最少需要走多少步,割草机行走规则如图。
简单贪心,考虑第一次出现草的那行向右割,之后往下向左的策略,在向右的时候必须走到本行与下一行更右位置的地方,向左的话必须走到本行与下一行更左位置的地方。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
int abx(int x)
{
    if (x < 0)
        return -x;
    else
        return x;
}

int main()
{
    int N,M;
    while (~scanf("%d%d",&N,&M))
    {
        int res = 0,maxx = 0,tmp = 0;
        int left[155],right[155];
        char maze[155][155];
        memset(left,0,sizeof(left));
        memset(right,0,sizeof(right));
        for (int i = 0;i < N;i++)
        {
            left[i] = INF;
            right[i] = -INF;
            scanf("%s",maze[i]);
            for (int j = 0;j < M;j++)
            {
                if (maze[i][j] == 'W')
                {
                    left[i] = min(left[i],j);
                    right[i] = max(right[i],j);
                }
            }
        }
        for (int i = 0;i < N;i++)
        {
            if (left[i] <= right[i])
            {
                maxx = i;
                if (!(i&1))
                {
                    res += abx(tmp - left[i]) + right[i] -  left[i];
                    tmp = right[i];
                }
                else
                {
                    res += abx(tmp - right[i]) + right[i] - left[i];
                    tmp = left[i];
                }
            }
        }
        printf("%d\n",res + maxx);
    }
    return 0;
} 

CF 115B Lawnmower(贪心)的更多相关文章

  1. CF 949D Curfew——贪心(思路!!!)

    题目:http://codeforces.com/contest/949/problem/D 有二分答案的思路. 如果二分了一个答案,首先可知越靠中间的应该大约越容易满足,因为方便把别的房间的人聚集过 ...

  2. CF Covered Path (贪心)

    Covered Path time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  3. CF 389 E 贪心(第一次遇到这么水的E)

    http://codeforces.com/contest/389/problem/E 这道题目刚开始想的特别麻烦...但是没想到竟然是贪心 我们只需要知道偶数的时候可以对称取的,然后奇数的时候没次取 ...

  4. CF 463A && 463B 贪心 && 463C 霍夫曼树 && 463D 树形dp && 463E 线段树

    http://codeforces.com/contest/462 A:Appleman and Easy Task 要求是否全部的字符都挨着偶数个'o' #include <cstdio> ...

  5. cf 之lis+贪心+思维+并查集

    https://codeforces.com/contest/1257/problem/E 题意:有三个集合集合里面的数字可以随意变换位置,不同集合的数字,如从第一个A集合取一个数字到B集合那操作数+ ...

  6. 【清真dp】cf1144G. Two Merged Sequences

    成就:赛后在cf使用错误的贪心通过一题 成就:在cf上赛后提交hack数据 成就:在cf上赛后hack自己 题目大意 有一长度$n \le 2\times 10^5$的序列,要求判断是否能够划分为一个 ...

  7. Codeforces Round #401 (Div. 2) 离翻身就差2分钟

    Codeforces Round #401 (Div. 2) 很happy,现场榜很happy,完全将昨晚的不悦忘了.终判我校一片惨白,小董同学怒怼D\E,离AK就差一个C了,于是我AC了C题还剩35 ...

  8. CF #374 (Div. 2) D. 贪心,优先队列或set

    1.CF #374 (Div. 2)   D. Maxim and Array 2.总结:按绝对值最小贪心下去即可 3.题意:对n个数进行+x或-x的k次操作,要使操作之后的n个数乘积最小. (1)优 ...

  9. CF 628C --- Bear and String Distance --- 简单贪心

    CF 628C 题目大意:给定一个长度为n(n < 10^5)的只含小写字母的字符串,以及一个数d,定义字符的dis--dis(ch1, ch2)为两个字符之差, 两个串的dis为各个位置上字符 ...

随机推荐

  1. canvas drag 实现拖拽拼图小游戏

    博主一直心心念念想做一个小游戏-  前端时间终于做了一个小游戏,直到现在才来总结,哈哈- 以后要勤奋点更新博客! 实现原理 1.如何切图? 用之前的方法就是使用photoshop将图片切成相应大小的图 ...

  2. ASP.MVC EASY UI 入门之 —— Tree & ComboTree

    1.常规的EASY UI的tree和comboTree代码基本是官方的DEMO都有的,虽然很简单,但是还是要实践的做一次,才能更清晰的了解和使用它!先上效果图 因为用的是code first,所以数据 ...

  3. Struts2 默认Action和模块包含

    在我们定义Action的时候会希望有一个默认的action在写错action时或者不写action时不至于报错 这里我只写一个简单的strut.xml例子 <?xml version=" ...

  4. Expression Blend4经验分享:制作一个简单的图片按钮样式

    这次分享如何做一个简单的图片按钮经验 在我的个人Silverlight网页上,有个Iphone手机的效果,其中用到大量的图片按钮 http://raimon.6.gwidc.com/Iphone/de ...

  5. 各种主流 SQLServer 迁移到 MySQL 工具对比

          我之所以会写这篇对比文章,是因为公司新产品研发真实经历过这个痛苦过程(传统基于SQL Server开发的C/S产品转为MySQL云产品).首次需要数据转换是测试环节,当时为了快速验证新研发 ...

  6. github开源:企业级应用快速开发框架CIIP WEB+WIN+移动端

    简介 CIIP是基于XAF开发的开源信息系统框架.CIIP最常见的应用场景是基于数据库的企业级应用程序,例如供应链系统,ERP系统,MRP系统,CRM系统等. CIIP支持WEB版本.Windows桌 ...

  7. C#中数组Array、ArrayList、泛型List<T>的比较

    在C#中数组Array,ArrayList,泛型List都能够存储一组对象,但是在开发中根本不知道用哪个性能最高,下面我们慢慢分析分析. 一.数组Array 数组是一个存储相同类型元素的固定大小的顺序 ...

  8. ASP.NET - SqlSugar ORM框架 更新列表

    以后SqlSugar所有更新都会在这个贴子更新 SqlSugar是一款轻量级的MSSQL ORM ,除了具有媲美ADO的性能外还具有和EF相似简单易用的语法. 学习列表 0.功能更新 1.SqlSug ...

  9. 探究JVM——运行时数据区

    最近在读<深入理解Java虚拟机>,收获颇丰,记录一下,部分内容摘自原书. Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域.这些区域都有各自的用途,以 ...

  10. 20160220 - JavaScript for OS X Automation 调试技巧

    在JXA代码中加入如下代码后,可使用 Safari Web Inspector 调试: //debugger; 使用 Safari Web Inspector 查看 Array 或 Object 并不 ...