Problem 1606 - Funny Sheep
Time Limit: 1000MS
Memory Limit: 65536KB
Total Submit: 612 Accepted: 169 Special Judge: No
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)
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
2 2
Sample Output
1
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;
}