Buildings
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5301
解题思路:
官方题解:
按照官方题解if顺序,很遗憾,并没有ac。。。后来看了别人的题解。。。才明白只能是:
if(n % 2 && n == m && x == y && x == (n+1)/2)
answer = n / 2;
else if(x == ans || x == ans + 1 || y == 1 || y == m)
answer = ans;
else if(min(left, right) > ans)
answer= min(max(up,down),min(left,right));
else
answer = ans;
AC代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int main(){
int n,m,x,y;
while(scanf("%d%d%d%d",&n,&m,&x,&y) != EOF){
if(n > m){
swap(n,m);
swap(x,y);
} //n<=m
int ans = (n + 1) / 2,answer;
int left = y,right = m - y+1;
int up = x-1,down = n-x;
if(n % 2 && n == m && x == y && x == (n+1)/2)
answer = n / 2;
else if(x == ans || x == ans + 1 || y == 1 || y == m)
answer = ans;
else if(min(left, right) > ans)
answer= min(max(up,down),min(left,right));
else
answer = ans;
printf("%d\n",answer);
}
return 0;
}
本文详细介绍了解决HDU 5301 Building问题的解题思路和AC代码,包括官方题解的局限性和优化方法。通过分析样例输入输出,提出了一种更高效的解决方案,包括条件判断逻辑和边界处理技巧。
1万+

被折叠的 条评论
为什么被折叠?



