POJ1054-The Troublesome Frog-两种存图方式+剪枝+模拟

题目描述:

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.

输入描述:

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.

输出描述:

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.

输入样例:

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

输出样例:

7

核心思想:

图的边长和点的个数都是5000
时间限制是5s,所以算法的时间复杂度应该是O(n2)。
空间限制是100MB,所以既可以用数组的连续存储方式,也可以用数组的离散存储方式
对于每一条路径,我们只需要从最开始的两个点模拟。
因为初始的两个点可以确定方向和步长,也就确定了一条可能的路径。
每条路径有两个开端(就像一条线段有两个端点),为了保证每条路径都会被模拟一次且仅一次,我们将离散的点排序,就顺序而言,路径的第一个点一定排在第二个点之前(双重for循环的内层循环的j从i+1开始)。
遍历每两个点时:
1、如果这两个点不是起始点,则剪枝。(判断方式见代码)
2、如果是起始点,模拟青蛙踩踏路径的过程,并记录路径长度。

代码如下:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
bool vis[5002][5002];//连续存储,vis[i][j]=1表示i,j处被踩踏 
struct node{
	int x,y;
}h[5002];//离散存储
bool pd(node v,node w)
{
	if(v.x!=w.x)
		return v.x<w.x;
	return v.y<w.y;
}
int fun(int v,int w,int r,int c)
{
	int tx=h[w].x-h[v].x,ty=h[w].y-h[v].y;
	int cnt=2,flag=0;//cnt记录点数,flag标记路径是否有效,值为1表示中断,无效 
	int sx=h[v].x,sy=h[v].y;//sx初始点横坐标,sy初始点纵坐标 
	if(sx-tx>0&&sy-ty>0&&sx-tx<=r&&sy-ty<=c)//倒求上一个点,如果越界,则此点就是第一点 
		return 0;//若没越界,则不是此点不是初始点,剪枝 
	sx=h[w].x,sy=h[w].y;
	while(sx+tx>0&&sy+ty>0&&sx+tx<=r&&sy+ty<=c)//开始往后跳 
	{
		if(!vis[sx+tx][sy+ty])//足迹不连续,则不是路径 
		{
			flag=1;
			break;
		}
		cnt++;
		sx+=tx;sy+=ty;
	}
	if(flag)
		return 0;
	if(cnt>2)
		return cnt;
	return 0;
}
int main()
{
	int r,c,n;
	cin>>r>>c>>n;
	for(int i=0;i<n;i++)//两种存图的数据结构 
	{
		scanf("%d%d",&h[i].x,&h[i].y);
		vis[h[i].x][h[i].y]=1;
	}
	sort(h,h+n,pd);//必须排序! 
	int ans=0;
	for(int i=0;i<n;i++)//暴力有序枚举每两个点 
		for(int j=i+1;j<n;j++)
			ans=max(ans,fun(i,j,r,c));
	cout<<ans<<endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值