Land oj 1606 - Funny Sheep(技巧&规律)

85 篇文章 0 订阅
34 篇文章 0 订阅
Problem 1606 - Funny Sheep
Time Limit: 1000MS     Memory Limit: 65536KB    
Total Submit: 612    Accepted: 169    Special Judge: No
Description

There are N+1 rows and M+1 columns fence with N*M grids on the grassland. Each grid has a sheep. In order to let the sheep together, we need to dismantle the fence. Every time you can remove a row or a column of fences. What’s the least number of times to reach the goal? 

Input
There are multiple test cases.
The first line of each case contains two integers N and M. (1≤N,M≤1000)
Output
For each case, output the answer in a line.
Sample Input
1 2
2 2
Sample Output


2

//题意:

现在有一个n*m的矩形的网格栅栏,每个网格里有一头羊,问最少拆掉多少根的栅栏可以让所有的羊走到一起。

//思路:

因为是让所有的羊走到一起,所以通过模拟可以找出规律:拆的最少栅栏数为min(min(n,m),m+n-2);n表示将横行拆掉,只剩下一行

m表示将所有的纵列拆掉,只剩下一列,n+m-2表示将中间的栅栏拆掉。找到它们三个之间的最小值即可。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<algorithm>
#include<iostream>
#define INF 0x3f3f3f3f
#define ull unsigned long long
#define ll long long
#define IN __int64
#define N 100010
#define M 1000000007
using namespace std;
int main()
{
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		int ans=min(n,m);
		ans=min(ans,n+m-2);
		printf("%d\n",ans);
	}
	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值