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

题意:有个R*C的格网。上面有若干个点,这些点可以连成一些直线,满足:这些点在直线上均匀排布(也就是间隔相等),直线的两段穿过网格(也就是第一个,最后一个在网格的边界附近)

求某条直线上最多的点数

题解:先排序,再任取两个水稻,算出之间的dx,dy,然后就能推出前后的水稻坐标,用如果满足路径上一直有水稻(用binarysearch),且第一个点与最后一个点在水稻外面,就是一个可行的解,维护其最大值。

注意一些判断。

ac代码:

#include<iostream>
#include<algorithm>
#define _for(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const int maxn = 5e3 + 5;
struct plant {
    int x, y;
    bool operator <(const plant b)const { return this->x == b.x ? this->y < b.y : this->x < b.x; }
    //bool operator ==(const plant b)const { return this->x == b.x&&this->y == b.y; }
}p[maxn];
int c, r;
int n;
int dx, dy, px, py,mx=2,steps;
int searchPath(plant p1, int dx, int dy) {
    plant dp;
    dp.x = p1.x + dx;
    dp.y = p1.y + dy;
    int steps =2;
    while (dp.x <= r&&dp.x >= 1 && dp.y <= c&&dp.y >= 1) {
        if (!binary_search(p, p + n, dp)) {steps = 0; break;}
        dp.x += dx;
        dp.y += dy;
        steps++;
    }
    return steps;
}
int main() {

    cin >> r >> c;

    cin >> n;
    _for(i, 0, n) {
        cin >> p[i].x >> p[i].y;
    }
    sort(p, p + n);
    _for(i, 0, n - 2)_for(j, i + 1, n - 1) {
        dx = p[j].x - p[i].x;
        dy = p[j].y - p[i].y;
        px = p[i].x -dx;
        py = p[i].y -dy;
        if (px <= r&&px >= 1 && py <= c&&py >= 1)continue;
        if (p[i].x + (mx - 1)*dx > r)break;//已经是最小dx了,这都不满足,直接换第一个点
        if (p[i].y + (mx - 1)*dy > c|| p[i].y + (mx - 1)*dy < 1)continue;//dy并不递增,也可能小于0
        steps = searchPath(p[j], dx, dy);
        mx = max(mx, steps);
    }
    cout << (mx == 2 ? 0:mx);
}

原文地址:https://www.cnblogs.com/SuuT/p/8635070.html

时间: 2024-11-09 03:13:25

POJ - 1054 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),然后枚举两个点,这两个 点代表这条路径的起始的两个点.然后是三个剪枝,下面有. 开始遍历时,

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

(中等) 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 hav

[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, b

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 3087 Shuffle&#39;m Up,枚举。

Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, S1 and S2, each stack containing C chips. Each stack may contain chips of several

poj 1180 Batch Scheduling(DP-单调性优化)

Batch Scheduling Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3145   Accepted: 1442 Description There is a sequence of N jobs to be processed on one machine. The jobs are numbered from 1 to N, so that the sequence is 1,2,..., N. The s

poj 2965 The Pilots Brothers&#39; refrigerator[ 枚举 ]

传送门:http://poj.org/problem?id=2965 思路:二进制枚举,递归输出路径.G++ 900+Ms勉强过,C++超时. 代码: #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<queue> #include<stack> #include<map>