IOI2002 POJ1054 The Troublesome Frog 讨厌的青蛙 (离散化+剪枝)
Description

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
Output
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
解题思路
这个问题看起来很复杂,其实目的很简单:帮助农民找到为害最大的青蛙。也就是要找到
一条穿越稻田的青蛙路径,这个路径上被踩踏的水稻不少于其他任何青蛙路径上被踩踏的水
稻数。当然,整个稻田中也可能根本就不存在青蛙路径。问题的关键是:找到穿越稻田的全
部青蛙路径。任何一条穿越稻田的青蛙路径L,至少包括3棵被踩踏的水稻。假设其中前两
棵被踩踏的水稻分别是(X1,Y1)、(X2,Y2),那么:
令dx=X2-X1、dy=Y2-Y1;X0=X1-dx、Y0=Y1- dy;X3=X2 + dx、Y3=Y2+ dy
(X0,Y0)位于稻田之外,青蛙从该位置经一跳后进入稻田、踩踏位置(X1,Y1)上的水稻
(X3,Y3)位于稻田之内,该位置是L上第3棵被青蛙踩踏的水稻
Xi=X0 + idx、Yi=Y1 + idy(i3),如果(Xi,Yi)位于稻田之内,则(Xi,Yi)上的水稻必被
青蛙踩踏
根据上述规则,只要知道一条青蛙路径上的前两棵被踩踏的水稻,就可以找到该路径上其
他的水稻。为了找到全部的青蛙路径,只要从被踩踏的水稻中,任取两棵水稻(X1,Y1)、(X2,
Y2),判断(X1,Y1)、(X2,Y2)是否能够作为一条青蛙路径上最先被踩踏的两颗水稻。
解决方案
这个问题的描述中,最基本的元素是被踩踏的水稻。在程序中要选择一个合适的数据结构,
来表达这个基本元素。这个数据结构是否合适的标准是:在程序中要表达这个元素时,能否
用一个单词或者短语,即用一个变量来表示。
struct PLANT {//描述一棵被踩踏的水稻
int x; //水稻的行号
int y; //水稻的列号
}
这个问题的主要计算是:从被踩踏的水稻中选择两棵(X1,Y1)、(X2,Y2)。判断它们是否
能够作为一条青蛙路径上最先被踩踏的两颗水稻。(X1,Y1)、(X2,Y2)唯一确定了蛙跳的方
向和步长,从(X2,Y2)开始,沿着这个方向和步长在稻田内走。每走一步,判断所到达位置
上(X,Y)的水稻是否被踩踏,直到走出稻田为止。如果在某一步上,(X,Y)没有被踩踏,
则表明(X1,Y1)、(X2,Y2)是一条青蛙路径上最先被踩踏的两颗水稻的假设不成立。这个判
断的算法在问题求解过程中要反复使用,它的效率成为决定整个计算效率的关键。
用一个PLANT型的数组plants[5001]表示全部被踩踏的水稻
将plants中的元素按照行/列序号的升序(或者降序)排列
采用二分法查找plants中是否有值为(X,Y)的元素:将(X,Y)与plants中间的元素比较,
(1)相等,表明找到了元素;(2)比plants中间元素的小,继续在plants的前半部寻找;(3)
比plants中间元素的大,继续在plants的后半部寻找。
采用上述方法判断每走一步所到达位置上(X,Y)的水稻是否被踩踏,最多只要比较Log2N,
其中N是稻田中被踩踏水稻的总量。
参考程序
//By LYLtim
//2015.2.3 #include <iostream>
#include <algorithm>
using namespace std; class Plant
{
public:
int x, y;
bool operator< (const Plant& p2) const { // binary_search 3rd argument const T& val
if (x == p2.x)
return y < p2.y;
return x < p2.x;
}
}; int r, c, n;
Plant plants[]; int searchPath(const Plant secPlant, const int dx, const int dy){
Plant tmpPlant;
int steps = ;
tmpPlant.x = secPlant.x + dx;
tmpPlant.y = secPlant.y + dy;
while( (tmpPlant.x <= r) && (tmpPlant.y >= ) && (tmpPlant.y <= c) ) {
if (!binary_search(plants, plants + n, tmpPlant)) {
//每一步都必须踩倒水稻才算合理, 否则这就不是一条行走路径
steps = ;
break;
}
steps++; //print
// cout << tmpPlant.x << "," << tmpPlant.y << endl; tmpPlant.x += dx;
tmpPlant.y += dy;
}
return steps;
} int main()
{
int dx, dy, px, py, max = , steps;
cin >> r >> c;
cin >> n;
for (int i = ; i < n; i++)
cin >> plants[i].x >> plants[i].y;
sort(plants, plants + n); //print plants
// for (int i = 0; i < n; i++)
// cout << plants[i].x << " " <<plants[i].y << endl; for (int i = ; i < n-; i++)
for (int j = i+; j < n-; j++) {
dx = plants[j].x - plants[i].x;
dy = plants[j].y - plants[i].y;
px = plants[i].x - dx;
py = plants[i].y - dy;
if ( (px>=) && (py>=) && (py<=c))
continue;
if (plants[i].x + (max-)*dx > r)
break;
py = plants[i].y + (max-)*dy;
if ( (py<) || (py>c) )
continue; //print
// cout << "p1:" << plants[i].x << "," << plants[i].y << " p2:"<<plants[j].x << "," << plants[j].y << " dx,dy: " << dx << "," << dy << endl; steps = searchPath(plants[j], dx, dy); //print steps
// cout << "steps:" <<steps << endl << endl; if (steps > max)
max = steps;
}
if (max == )
max = ;
cout << max;
return ;
}

IOI2002 POJ1054 The Troublesome Frog 讨厌的青蛙 (离散化+剪枝)的更多相关文章
- POJ1054 The Troublesome Frog
题目来源:http://poj.org/problem?id=1054 题目大意: 有一种青蛙在晚上经过一片稻田,在庄稼上跳跃,会把庄稼压弯.这让农民很苦恼.我们希望通过分析青蛙跳跃的路径,找出对稻田 ...
- poj1054 The Troublesome Frog 瞎搞。
连接:http://poj.org/problem?id=1054 题意:就是一个格子里一条线上最长有几个青蛙(青蛙间隔相同)~.但是其实青蛙的起点重点必须是在外面. 直接写一个搜就是. #inclu ...
- POJ 1054 The Troublesome Frog
The Troublesome Frog Time Limit: 5000MS Memory Limit: 100000K Total Submissions: 9581 Accepted: 2883 ...
- Poj 1054 The Troublesome Frog / OpenJudge 2812 恼人的青蛙
1.链接地址: http://poj.org/problem?id=1054 http://bailian.openjudge.cn/practice/2812 2.题目: 总时间限制: 10000m ...
- POJ 1054 The Troublesome Frog(枚举+剪枝)
题目链接 题意 :给你r*c的一块稻田,每个点都种有水稻,青蛙们晚上会从水稻地里穿过并踩倒,确保青蛙的每次跳跃的长度相同,且路线是直线,给出n个青蛙的脚印点问存在大于等于3的最大青蛙走的连续的脚印个数 ...
- (中等) POJ 1054 The Troublesome Frog,记忆化搜索。
Description In Korea, the naughtiness of the cheonggaeguri, a small frog, is legendary. This is a we ...
- The Troublesome Frog
In Korea, the naughtiness of the cheonggaeguri, a small frog, is legendary. This is a well-deserved ...
- poj 1054 The Troublesome Frog (暴力搜索 + 剪枝优化)
题目链接 看到分类里是dp,结果想了半天,也没想出来,搜了一下题解,全是暴力! 不过剪枝很重要,下面我的代码 266ms. 题意: 在一个矩阵方格里面,青蛙在里面跳,但是青蛙每一步都是等长的跳, 从一 ...
- 【POJ】1054 The Troublesome Frog
题目是非常经典的搜索+剪枝.题意简言之就是,青蛙需要沿着直线踩着踏点通过田地,并且踏点需要至少为3.问哪条路径青蛙踩坏的作物最多.很好的一个条件是青蛙每次移动都是等间距的.题目需要注意将其排序. #i ...
随机推荐
- C# Page基础功能,用于各页面继承
IBasePage.cs文件 /// <summary> /// 用于页面或用户控件 /// </summary> public interface IBasePage { / ...
- PostgreSQL pg_hba.conf 文件简析
作者:高张远瞩(HiLoveS) 博客:http://www.cnblogs.com/hiloves/ 转载请保留该信息 最近试用PostgreSQL 9.04,将pg_hba.conf配置的一些心得 ...
- 代码审查工具Sonarqube安装
前言:在项目开发当中,完成需求并上线是一件很开心的事情,但为了能按时上线功能不得不为了完成功能而写代码,写的时候觉得先把功能上了以后再回头优化此处代码,但真正上线之后你就会发现你再也不想去修改之前遗留 ...
- vue-router的一个小实例
非2.0的 vue2.0还有vue-router2.0的改变还是挺大的 vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,适合用于构建单页面应用.vue的单页面应用是基于 ...
- UVALive - 6709树套树
题意:给你一个矩阵,q次操作,每次查询长宽l的矩阵最大值a和最小值b,然后把中间点换成floor((a+b)/2), 解法:暴力可过,建n颗线段树暴力更新,但是正解应该是树套树,树套树需要注意的是当建 ...
- 第七天 Linux用户管理、RHEL6.5及RHEL7.2 root密码破解、RHEL6.5安装vmware tools
1.Linux用户管理 Linux系统中,存在三种用户 A.超级用户:root 最高权限,至高无上 在windows中 administrator是可以登录的最高权限,但是,system权限最高,不能 ...
- Java SHA256/Base64转.NET(C#)实现---(华为云云市场.NET版本加密方式)
前言: 工作需要,对接华为云应用市场的 API 接口,由于维护团队都是 .NET 所以用 .NET 来开发. 简单了解一下 SHA256 加密算法,本质就是一个 Hash,与 MD5 相比就是计算量大 ...
- POJ 1568 极大极小搜索 + alpha-beta剪枝
极小极大搜索 的个人理解(alpha-beta剪枝) 主要算法依据就是根据极大极小搜索实现的. 苦逼的是,查了两个晚上的错,原来最终是判断函数写错了..瞬间吐血! ps. 据说加一句 if sum & ...
- MySQL相关问题总结
希望此贴能够将MySQL安装周围的问题总结清楚,也免得自己再遇到问题时而不知所措.本帖中所有关于MySQL的问题均涉及到两个平台:Ubuntu 和 Windows(本人没有Mac) 问题1:MySQL ...
- 尝试优化骨骼动画计算的意外收获——使用嵌入式汇编对float转int进行优化
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4984530.html 公司引擎目前是使用CPU计算骨骼动画(采用了D3DX提供的函数 ...