Codeforces Beta Round #87 (Div. 1 Only)B Lawnmower(贪心)

题目链接: 传送门

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)
  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.

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;
} 
posted @   zxzhang  阅读(260)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示

目录导航