POJ1054--The Troublesome Frog
来源:互联网 发布:网络通信socket 编辑:程序博客网 时间:2024/06/05 09:48
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.
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 7142 16 64 22 52 62 73 46 16 22 36 36 46 56 7
Sample Output
7
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;#define maxn 5008int R,C,n;struct PLANT{int x,y;}plants[maxn],plant;int myCompare(const void*a,const void *b){PLANT *p1,*p2;p1 = (PLANT*)a;p2 = (PLANT*)b;if(p1->x == p2->x)return (p1->y - p2->y);return (p1->x - p2->x);}inline int max(int a,int b){return a>b?a:b;}int countpath(PLANT p,int dX,int dY){int step = 0;while(p.x <= R && p.x >= 1 && p.y >= 1 && p.y <= C){if(!bsearch(&p,plants,n,sizeof(PLANT),myCompare)){step = 0;break;}p.x += dX;p.y += dY;step++;}return step;}int main(){//freopen("in.txt","r",stdin);while(scanf("%d%d",&R,&C)==2){scanf("%d",&n);for(int i = 0;i < n;i++){scanf("%d%d",&plants[i].x,&plants[i].y);}qsort(plants,n,sizeof(PLANT),myCompare);int ans = 2;for(int i = 0;i < n-2;i++){for(int j = i+1;j < n-1;j++){int dX = plants[j].x - plants[i].x;int dY = plants[j].y - plants[i].y;//剪枝一,如果起点在田地里。跳过int pX = plants[i].x - dX;int pY = plants[i].y - dY;if(pX >= 1 && pX <= R && pY >= 1 && pY <= C)continue;if(pX + ans*dX > R ||pX + ans*dX < 1)break;if(pY + ans*dY > C ||pY + ans*dY < 1)continue;int step = countpath(plants[i],dX,dY);ans = max(ans,step);}}if(ans == 2)ans -= 2;printf("%d\n",ans);}return 0;}
- POJ1054--The Troublesome Frog
- POJ1054 The Troublesome Frog
- POJ1054-The Troublesome Frog
- POJ1054 The Troublesome Frog
- POJ1054 The Troublesome Frog [dp]
- poj1054--The Troublesome Frog(枚举+二分)
- [搜索+剪枝/]poj1054 The Troublesome Frog
- [poj1054] The Troublesome Frog 暴力枚举+剪支
- poj 1054 The Troublesome Frog
- POJ--1054--The Troublesome Frog
- POJ 1054 The Troublesome Frog
- POJ - 1054 The Troublesome Frog
- POJ 1054 The Troublesome Frog
- POJ 1054 The Troublesome Frog
- POJ 1054 The Troublesome Frog
- POJ 1054 The Troublesome Frog
- poj 1054 The Troublesome Frog
- poj 1054 The Troublesome Frog
- 安装ipmitool监控cpu温度
- 有没有方法在不root手机的情况下读取Data目录下的文件
- 动态语言和静态语言—强类型定义语言和弱类型定义语言
- PhoneGap 3.0发布:新API、更好的工具以及多平台支持
- ubuntu12.04 安装skype
- POJ1054--The Troublesome Frog
- 一个解决JSP中文乱码的过滤器
- Java RMI详解
- c语言各重点语句模块汇总
- android 自定义RadioButton 实现图标居中显示
- makefile中wildcard notdir patsubst的简单介绍
- UVA 10603 (13.07.24)
- KVM-QEMU vdagent实现机制分析(一)
- Android下app生成coredump方法