For each testcase, print only one interger, representing the answer.
解题思路:
假设没有不合法的块,那结果就是长和宽中最小值的一半,而,不合法的块所影响的仅仅有它周围的四块,计算出这四块距离四个边的距离的最小值,就是加入上不合法块之后该块所须要的最长距离。
须要注意特判一中情况。即不合法块在正中间的时候,并不造成影响。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;
int n, m, x, y;
int main()
{
while(scanf("%d%d%d%d", &n, &m, &x, &y)!=EOF)
{
if(n == m && (n % 2 == 1 && m % 2 == 1) && (x == y && x == (n+1)/2))
{
cout << (n -1) / 2 << endl;
continue;
}
int Min = min(n, m); int ans;
if(Min & 1) ans = (Min + 1) / 2;
else ans = Min / 2;
int res = -10;
int xx = x - 1, yy = y;
if(xx >= 1 && yy >= 1) res = max(res, min(xx-1,min(yy-1,m-yy)));
xx = x, yy = y-1;
if(xx >= 1 && yy >= 1) res = max(res, min(min(xx-1,n-xx),yy-1));
xx = x + 1, yy = y;
if(xx <=n && yy >= 1) res = max(res, min(n-xx,min(yy-1,m-yy)));
xx = x, yy = y+1;
if(xx >= 1 && yy <= m) res = max(res, min(min(xx-1,n-xx),m-yy));
res += 1;
ans = max(ans, res);
printf("%d
", ans);
}
return 0;
}