[poj1054] The Troublesome Frog 暴力枚举+剪支

The Troublesome Frog

Time Limit: 5000MS Memory Limit: 100000K

Total Submissions: 11339 Accepted: 3383

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

题目链接http://poj.org/problem?id=1054

题意:当青蛙经过农田时的痕迹是一条直线。农田里的植物就在这个农田的二维坐标系的整数格点上。如果某只青蛙经过农田,也就是某条直线穿过农田。那么那条直线经过的所有的整数格点上的植物会都被破坏掉。求破坏最多青蛙破坏的个数;

思路:暴力枚举(i,j)+剪支;如在范围内都破坏,统计最多的,如枚举的点以前出现(上一个点存在),continue,表示搜过;

代码

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
using namespace std;
bool vis[5001][5001];
int dx,dy,tx,ty,maxx=0,cnt;
int r,c;
int n;

struct node{
   int x,y;
}q[5005];
int cmp(node xx,node yy)
{
    if(xx.y=yy.y) return xx.x<yy.x;
    return xx.y<yy.y;
}

int main()
{
    scanf("%d%d",&r,&c);
    scanf("%d",&n);
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&q[i].x,&q[i].y);
        vis[q[i].x][q[i].y]=1;
    }

    sort(q+1,q+1+n,cmp);
    for(int i=1;i<=n;i++)
    for(int j=i+1;j<=n;j++)
    {
        dx=q[j].x-q[i].x;
        dy=q[j].y-q[i].y;
        if(q[i].x-dx>0&&q[i].x-dx<=r&&q[i].y-dy>0&&q[i].y-dy<=c) continue;
        tx=q[j].x+dx;
        ty=q[j].y+dy;
        cnt=0;
        while(tx>0&&ty>0&&tx<=r&&ty<=c)
        {

            if(vis[tx][ty])cnt++;
            else
            {
                cnt=0;
                break;
            }
            tx+=dx;
            ty+=dy;
        }
        maxx=max(cnt,maxx);
    }
    if(maxx)
    printf("%d\n",maxx+2);
     else puts("0");
    return 0;

}
时间: 2024-11-06 15:16:50

[poj1054] The Troublesome Frog 暴力枚举+剪支的相关文章

POJ 1054 The Troublesome Frog(枚举+剪枝)

题目链接 题意 :给你r*c的一块稻田,每个点都种有水稻,青蛙们晚上会从水稻地里穿过并踩倒,确保青蛙的每次跳跃的长度相同,且路线是直线,给出n个青蛙的脚印点问存在大于等于3的最大青蛙走的连续的脚印个数. 思路 : 暴力了一下,顺便剪剪枝就可以过.... 1 //POJ1054 2 #include <stdio.h> 3 #include <string.h> 4 #include <iostream> 5 #include <algorithm> 6 7

poj 1054 The Troublesome Frog (暴力搜索 + 剪枝优化)

题目链接 看到分类里是dp,结果想了半天,也没想出来,搜了一下题解,全是暴力! 不过剪枝很重要,下面我的代码 266ms. 题意: 在一个矩阵方格里面,青蛙在里面跳,但是青蛙每一步都是等长的跳, 从一个边界外,跳到了另一边的边界外,每跳一次对那个点进行标记. 现在给你很多青蛙跳过后的所标记的所有点,那请你从这些点里面找出 一条可能的路径里面出现过的标记点最多. 分析:先排序(目的是方便剪枝,break),然后枚举两个点,这两个 点代表这条路径的起始的两个点.然后是三个剪枝,下面有. 开始遍历时,

POJ1054 The Troublesome Frog [dp]

艰难的写上一篇,小学期太累了,,,很难坚持刷 #include <iostream> #include <cmath> #include <cstring> #include <cstdio> #include <cstdlib> #include <algorithm> using namespace std; int m,n; int num; struct Grid { int x,y; }grid[5555]; int cmp

IOI2002 POJ1054 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 hav

poj1054--The Troublesome Frog(枚举+二分)

题目链接:点击打开链接 题目大意:青蛙经过一块农田,每一次跳相同的距离,经过的点的植物被踩坏,给出n个被踩坏的坐标,问危害最大的一个青蛙踩坏了几块植物.(最少要有三个,否则是0) 每一次跳的距离相同,枚举最先开始的两个点p[i],p[j],得到距离差(x,y)来计算之后的点的坐标,用二分查找该点是否被踩坏,找出最大值. 优化:1.枚举的是最先开始的两个点,所以p[i].x-x,p[i].y-y应该在农田外, 2.判断p[j].x+max1*x,p[j].y+max1*y如果在农田外,那么他不可能

POJ - 1054 The Troublesome Frog 模拟 枚举优化。

题意:有个R*C的格网.上面有若干个点,这些点可以连成一些直线,满足:这些点在直线上均匀排布(也就是间隔相等),直线的两段穿过网格(也就是第一个,最后一个在网格的边界附近) 求某条直线上最多的点数 题解:先排序,再任取两个水稻,算出之间的dx,dy,然后就能推出前后的水稻坐标,用如果满足路径上一直有水稻(用binarysearch),且第一个点与最后一个点在水稻外面,就是一个可行的解,维护其最大值. 注意一些判断. ac代码: #include<iostream> #include<al

POJ - 1054 The Troublesome Frog

题意:给你个矩阵,里面有n个标记的点,许多只青蛙在上面跳,每次跳的距离都是一样的且轨迹是直线,目标是从一边跳到另一边,求最多步数的青蛙 思路:排序后,枚举判断 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 5050; struct point{ int x,y; void i

CodeForces 200C Football Championship(暴力枚举)

C. Football Championship time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Any resemblance to any real championship and sport is accidental. The Berland National team takes part in the local

Gym 101194L / UVALive 7908 - World Cup - [三进制状压暴力枚举][2016 EC-Final Problem L]

题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5930 题意: 现有四支队伍两两打比赛,总共就是打六场比赛,每场比赛赢的队伍可得 $3$ 分,输的队伍得 $0$ 分,平局则两个队各得 $1$ 分. 现在给出四个队伍最终的积分