您的位置:首页 > 其它

poj 1054 The Troublesome Frog

2017-01-16 16:53 471 查看
The Troublesome Frog

Time Limit: 5000MS Memory Limit: 100000K
Total Submissions: 12062 Accepted: 3632
Case Time Limit: 500MS
Description

In Korea, the naughtiness of the cheonggaeguri, a small frog, is legendary. This is a well-deserved reputation, because the frogs jump through your rice paddy at night, flattening rice plants. In the morning, after noting which plants have been flattened, you
want to identify the path of the frog which did the most damage. A frog always jumps through the paddy in a straight line, with every hop the same length: 


 

Your rice paddy has plants arranged on the intersection points of a grid as shown in Figure-1, and the troublesome frogs hop completely through your paddy, starting outside the paddy on one side and ending outside the paddy on the other side as shown in Figure-2: 


 

Many frogs can jump through the paddy, hopping from rice plant to rice plant. Every hop lands on a plant and flattens it, as in Figure-3. Note that some plants may be landed on by more than one frog during the night. Of course, you can not see the lines showing
the paths of the frogs or any of their hops outside of your paddy ?for the situation in Figure-3, what you can see is shown in Figure-4: 


 

From Figure-4, you can reconstruct all the possible paths which the frogs may have followed across your paddy. You are only interested in frogs which have landed on at least 3 of your rice plants in their voyage through the paddy. Such a path is said to be
a frog path. In this case, that means that the three paths shown in Figure-3 are frog paths (there are also other possible frog paths). The vertical path down column 1 might have been a frog path with hop length 4 except there are only 2 plants flattened so
we are not interested; and the diagonal path including the plants on row 2 col. 3, row 3 col. 4, and row 6 col. 7 has three flat plants but there is no regular hop length which could have spaced the hops in this way while still landing on at least 3 plants,
and hence it is not a frog path. Note also that along the line a frog path follows there may be additional flattened plants which do not need to be landed on by that path (see the plant at (2, 6) on the horizontal path across row 2 in Figure-4), and in fact
some flattened plants may not be explained by any frog path at all. 

Your task is to write a program to determine the maximum number of landings in any single frog path (where the maximum is taken over all possible frog paths). In Figure-4 the answer is 7, obtained from the frog path across row 6. 

Input

Your program is to read from standard input. The first line contains two integers R and C, respectively the number of rows and columns in your rice paddy, 1 <= R,C <= 5000. The second line contains the single integer N, the number of flattened rice plants,
3 <= N <= 5000. Each of the remaining N lines contains two integers, the row number (1 <= row number <= R) and the column number (1 <= column number <= C) of a flattened rice plant, separated by one blank. Each flattened plant is only listed once.
Output

Your program is to write to standard output. The output contains one line with a single integer, the number of plants flattened along a frog path which did the most damage if there exists at least one frog path, otherwise, 0.
Sample Input
6 7
14
2 1
6 6
4 2
2 5
2 6
2 7
3 4
6 1
6 2
2 3
6 3
6 4
6 5
6 7

Sample Output
7

Source

IOI 2002


提示

题意:

有许许多多讨厌的青蛙会经过庄稼地且它们落脚时会踩坏庄稼,因此很想找出哪条路径是踩坏庄稼数最多的,输出踩坏的庄稼数。青蛙的路径只有直线,且这条直线上踩坏数大于等于3个才算以及他们落脚位置之间的距离必须相等,否则算零个。

思路:

先对y最下排序(按个人习惯)后直接枚举,但需要有条件限制,不然会超时。

示例程序

Source Code

Problem: 1054 Code Length: 1626B
Memory: 24920K Time: 375MS
Language: G++ Result: Accepted
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct point
{
int x,y;
}p[5000],walk;
char v[5001][5001];
int cmp(struct point x,struct point y)
{
if(x.y<y.y)
{
return 1;
}
else if(x.y==y.y&&x.x<y.x)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
int c,r,n,i,i1,max,num,dx,dy;
scanf("%d %d",&r,&c);
scanf("%d",&n);
memset(v,0,sizeof(v));
max=0;
for(i=0;n>i;i++)
{
scanf("%d %d",&p[i].x,&p[i].y);
v[p[i].x][p[i].y]=1;
}
sort(p,p+n,cmp);
for(i=0;n>i;i++)
{
for(i1=i+1;n>i1;i1++)
{
dx=p[i1].x-p[i].x;
dy=p[i1].y-p[i].y;
if(p[i].y+dy*max>c) //如果最大毁坏庄稼数不在此范围内的跳过
{
break; //因为y是递增的,所以直接break
}
if(p[i].x-dx>=1&&p[i].x-dx<=r&&p[i].y-dy>=1&&p[i].y-dy<=c) //不是从边界跳过来的跳过,因为不是从边界来的一定不是最大的
{
continue;
}
if(p[i].x+max*dx<1||p[i].x+max*dx>r||p[i].y+max*dy<1||p[i].y+max*dy>c) //如果最大毁坏庄稼数不在此范围内的跳过
{
continue;
}
walk=p[i];
num=1;
while(walk.x+dx>=1&&walk.x+dx<=r&&walk.y+dy>=1&&walk.y+dy<=c)
{
walk.x=walk.x+dx;
walk.y=walk.y+dy;
if(v[walk.x][walk.y]==1)
{
num++;
}
else
{
num=0;
break;
}
}
if(max<num&&num>=3)
{
max=num;
}
}
}
printf("%d\n",max);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: