| Problem G. Birthday Cake |
Background
Lucy and Lily are twins. Today is their birthday. Mother buys a birthday cake for them.Now we put the cake onto a Descartes coordinate. Its center is at (0,0), and the cake's length of radius is 100.

There are 2N (N is a integer, 1<=N<=50) cherries on the cake. Mother wants to cut the cake into two halves with a knife (of course a beeline). The twins would like to be treated fairly, that means, the shape of the two halves must be the same (that means the beeline must go through the center of the cake) , and each half must have N cherrie(s). Can you help her?
Note: the coordinate of a cherry (x , y) are two integers. You must give the line as form two integers A,B(stands for Ax+By=0), each number in the range [-500,500]. Cherries are not allowed lying on the beeline. For each dataset there is at least one solution.
Input
The input file contains several scenarios. Each of them consists of 2 parts: The first part consists of a line with a number N, the second part consists of 2N lines, each line has two number, meaning (x,y) .There is only one space between two border numbers. The input file is ended with N=0.Output
For each scenario, print a line containing two numbers A and B. There should be a space between them. If there are many solutions, you can only print one of them.Sample Input
2 -20 20 -30 20 -10 -50 10 -5 0
Sample Output
0 1
因为半径较小,A,B是整数,因此可以直接枚举暴力搜索
#include<iostream>
using namespace std;
struct Node
{
int x,y;
}node[120];
int num;
bool line(int a,int b)
{
int r=0,t=0;
for(int i=0;i<num;i++)
{
if(node[i].x*a+node[i].y*b<0) r++;
else if(node[i].x*a+node[i].y*b>0) t++;
else break;
}
if(t==r&&r==num/2) return true;
else return false;
}
int main()
{
int n;
while(cin>>n&&n)
{
int i,j;
num=0;
for(i=0;i<2*n;i++)
{
int x1,y1;
cin>>x1>>y1;
if(x1*x1+y1*y1<=10000)
{
node[num].x=x1;
node[num].y=y1;
num++;
}
}
//cout<<num<<endl;
int tag=0;
for(i=-100;i<=100;i++)
{
for(j=-100;j<=100;j++)
{
if(line(i,j))
{
cout<<i<<" "<<j<<endl;
tag=1;
break;
}
}
if(tag) break;
}
}
return 0;
}
本文探讨了一个有趣的算法问题:如何将蛋糕上的樱桃公平地分配到切开后的两半蛋糕上。通过使用坐标系和直线方程的概念,采用枚举暴力搜索的方法找到了可行的解决方案。
973

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



