P4431 [COCI2017-2018#2] ​Košnja

       

题目描述

Mirko wants to buy land on which he will build a house for his family. So far, he’s seen K pieces of land. Each of them is in the shape of a rectangle and we can think of it as a matrix with N rows and M columns, N × M fields in total.

Mirko is aware that, before construction begins, the property needs to be regularly maintained and the lawn needs to be mowed. Because of this, Mirko bought a lawn mower. In order to mow the entire lawn of N rows and M columns, he needs to go over each field at least once. He can start from any field facing one of the four main directions (up, down, left, and right). His lawn mower can only go forwards (to the adjacent field facing the current direction) or make a 90 degree turn. Additionally, because of his own safety, Mirko can only use the lawn mower on his land, so he cannot leave the matrix.

Since making the lawn mower turn isn’t simple, Mirko wants to mow the lawn with the minimal amount of turns. For each piece of land he saw so far, Mirko wants to know the minimal number of turns he can make so that the entire lawn is mowed. Help Mirko solve this problem.

翻译:

        给定一个n*m或n∗m的矩阵,每次你可以选择前进一格或转弯(90度),求在不出这个矩阵的情况下遍历全部格点所需最少转弯次数。有多组数据组成。

输入格式:

第一行一个整数kk,表示数据组数

以下kk行,每行两个整数n,mn,m,表示矩阵大小

输出格式:

输出一个整数,即最少转弯次数

 

        这道题是个英文的,还好下面有翻译,要不然直接废掉……

        看了一眼,开始觉得要用dfs,写了一会,突然想到这题是一道找规律,因为他是一个矩阵,遍历求最少次数就要让每次直线距离最长,自然是从长边走直线,转弯转在短边上,如下:

        发现,每一行可贡献2次转弯;那么,表达式就应为2*min(n,m),可是发现数据不对,因为最后一行多算了2个,减去,为:2*min(n,m)-2;则代码如下:

#include<bits/stdc++.h>
using namespace std;
int main(){
	int k,n,m;
	cin>>k;
	for(int i=1;i<=k;i++){
		cin>>n>>m;
		cout<<2*min(n,m)-2<<endl; 
	}
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值