POJ 1054 The Troublesome Frog

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.

解题报告:IOI大模拟题,我们枚举两个点作为每次跳的距离,然后直接跳即可,注意这样是(O(n^3))的,对于不能跳进来的点我们可以直接不跳,对于不管喝不合法跳出去后步数都不能大于当前答案,我们也可以不跳

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
const int N=5005;
bool map[N][N];
struct node{
	int x,y;
	bool operator <(const node &pp)const{
		if(x!=pp.x)return x<pp.x;
		return y<pp.y;
	}
}a[N];
int n,m,s;
void work()
{
	scanf("%d%d%d",&n,&m,&s);
	for(int i=1;i<=s;i++){
		scanf("%d%d",&a[i].x,&a[i].y);
		map[a[i].x][a[i].y]=true;
	}
	sort(a+1,a+s+1);
	int dx,dy,ans=0,tx,ty,tot=0,x,y;
	bool arr=0;
	for(int i=1;i<=s;i++){
		for(int j=i+1;j<=s;j++){
			dx=a[j].x-a[i].x;dy=a[j].y-a[i].y;
			if(a[i].x-dx>=1 &&  a[i].x-dx<=n && a[i].y-dy>=1 && a[i].y-dy<=m)continue;
			tx=a[i].x+dx*ans;ty=a[i].y+dy*ans;
			if(tx>n)break;
			if(tx>n || tx<1 || ty>m || ty<1)continue;
			x=a[j].x;y=a[j].y;tot=1;arr=false;
			while(x>=1 && x<=n && y>=1 && y<=m){
				if(!map[x][y]){
					arr=true;
					break;
				}
				x+=dx;y+=dy;tot++;
			}
			if(tot<=2 || arr)continue;
			if(tot>ans)ans=tot;
		}
	}
	printf("%d
",ans);
}
int main()
{
	work();
	return 0;
}

原文地址:https://www.cnblogs.com/Yuzao/p/7501482.html