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.

 
  十分蛋疼的题意描述,就是给一个图,上面有很多点,然后问最长的一个轨迹是多少。
  刚开始想错了方向,一直在想从整体的角度怎么DP,后来百度发现是搜索,然后才想到了直接枚举两个点,然后dfs就好,其中加上记忆化,就可以了。
  注意卡内存,所以说要hash存点,hash值也要取个合适的,第一次就TLE了。。。
 
代码如下:
// ━━━━━━神兽出没━━━━━━
// ┏┓ ┏┓
// ┏┛┻━━━━━━━┛┻┓
// ┃ ┃
// ┃ ━ ┃
// ████━████ ┃
// ┃ ┃
// ┃ ┻ ┃
// ┃ ┃
// ┗━┓ ┏━┛
// ┃ ┃
// ┃ ┃
// ┃ ┗━━━┓
// ┃ ┣┓
// ┃ ┏┛
// ┗┓┓┏━━━━━┳┓┏┛
// ┃┫┫ ┃┫┫
// ┗┻┛ ┗┻┛
//
// ━━━━━━感觉萌萌哒━━━━━━ // Author : WhyWhy
// Created Time : 2015年07月20日 星期一 08时55分13秒
// File Name : 1054.cpp #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int MaxN=; int R,C;
short dp[MaxN][MaxN];
int N; struct Point
{
int x,y; bool operator < (const Point &b) const
{
if(x==b.x)
return y<b.y; return x<b.x;
}
}P[MaxN]; inline bool in(int x,int y)
{
return x<=R && x>= && y<=C && y>=;
} const int HASH=; struct HASHMAP
{
int head[HASH],next[MaxN],Hcou;
unsigned long long key[MaxN];
int rem[MaxN]; void init()
{
Hcou=;
memset(head,-,sizeof(head));
} void insert(unsigned long long val,int id)
{
int h=val%HASH; for(int i=head[h];i!=-;i=next[i])
if(val==key[i])
return; rem[Hcou]=id;
key[Hcou]=val;
next[Hcou]=head[h];
head[h]=Hcou++;
} int find(unsigned long long val)
{
int h=val % HASH; for(int i=head[h];i!=-;i=next[i])
if(val==key[i])
return rem[i]; return ;
}
}map1; int dfs(int u,int v)
{
if(dp[u][v])
return dp[u][v]; int dx=(P[v].x<<)-P[u].x,dy=(P[v].y<<)-P[u].y; if(!in(dx,dy))
return ; int rP=map1.find(dx*+dy); if(!rP)
{
dp[u][v]=-;
return -;
} dp[u][v]=dfs(v,rP)+; return dp[u][v];
} inline bool judge(int u,int v)
{
int dx=(P[u].x<<)-P[v].x,dy=(P[u].y<<)-P[v].y; return !in(dx,dy);
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int ans=-; scanf("%d %d",&R,&C);
scanf("%d",&N); for(int i=;i<=N;++i)
scanf("%d %d",&P[i].x,&P[i].y); sort(P+,P+N+); map1.init(); for(int i=;i<=N;++i)
map1.insert(*P[i].x+P[i].y,i); for(int i=;i<=N;++i)
for(int j=i+;j<=N;++j)
if(judge(i,j))
ans=max(ans,dfs(i,j)); if(ans<)
ans=; printf("%d\n",ans); return ;
}

(中等) POJ 1054 The Troublesome Frog,记忆化搜索。的更多相关文章

  1. poj 3249(bfs+dp或者记忆化搜索)

    题目链接:http://poj.org/problem?id=3249 思路:dp[i]表示到点i的最大收益,初始化为-inf,然后从入度为0点开始bfs就可以了,一开始一直TLE,然后优化了好久才4 ...

  2. poj 1661 Help Jimmy(记忆化搜索)

    题目链接:http://poj.org/problem?id=1661 一道还可以的记忆化搜索题,主要是要想到如何设dp,记忆化搜索是避免递归过程中的重复求值,所以要得到dp必须知道如何递归 由于这是 ...

  3. poj 1085 Triangle War 博弈论+记忆化搜索

    思路:总共有18条边,9个三角形. 极大极小化搜索+剪枝比较慢,所以用记忆化搜索!! 用state存放当前的加边后的状态,并判断是否构成三角形,找出最优解. 代码如下: #include<ios ...

  4. poj 1088 动态规划+dfs(记忆化搜索)

    滑雪 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Description Mi ...

  5. poj 1579(动态规划初探之记忆化搜索)

    Function Run Fun Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17843   Accepted: 9112 ...

  6. POJ 3616 Milking Time ——(记忆化搜索)

    第一眼看是线段交集问题,感觉不会= =.然后发现n是1000,那好像可以n^2建图再做.一想到这里,突然醒悟,直接记忆化搜索就好了啊..太蠢了.. 代码如下: #include <stdio.h ...

  7. POJ 1661 Help Jimmy ——(记忆化搜索)

    典型的记忆化搜索问题,dfs一遍即可.但是不知道WA在哪里了= =,一直都没找出错误.因为思路是很简单的,肯定是哪里写挫了,因此不再继续追究了. WA的代码如下,希望日后有一天能找出错误= =: —— ...

  8. POJ 1054 The Troublesome Frog

    The Troublesome Frog Time Limit: 5000MS Memory Limit: 100000K Total Submissions: 9581 Accepted: 2883 ...

  9. Poj 1054 The Troublesome Frog / OpenJudge 2812 恼人的青蛙

    1.链接地址: http://poj.org/problem?id=1054 http://bailian.openjudge.cn/practice/2812 2.题目: 总时间限制: 10000m ...

随机推荐

  1. sql server中单引号拼接字符串(书写错误会出现错误"浮点值 XXXX 超出了计算机表示范围(8 个字节)。“XX”附近有语法错误。")

    " ' "(单引号)的运用:在sql server中,两个" ' "(单引号)在拼接字符串的情况下运用,就是表示拼接上了一个" ' "单引号 ...

  2. FTP 服务器

    先使用mstsc检验网络连通性\\192.168.196.177\OraCDuser:domai\userpassword: 1234UAT 和prod 网络隔绝

  3. CAPSPageMenu分页交互

    最近在开发过程中,我的前任在处理类似于新闻多板块的界面,在一个视图控制器里加载多个UITableView以显示不同类型的信息,并可通过头部按钮和左右滑动来切换不同的tableView这样的界面中,采取 ...

  4. WPF中静态引用资源与动态引用资源的区别

    WPF中静态引用资源与动态引用资源的区别   WPF中引用资源分为静态引用与动态引用,两者的区别在哪里呢?我们通过一个小的例子来理解. 点击“Update”按钮,第2个按钮的文字会变成“更上一层楼”, ...

  5. Spring Boot 系列教程6-全局异常处理

    @ControllerAdvice源码 package org.springframework.web.bind.annotation; import java.lang.annotation.Ann ...

  6. php 弹窗插件

    index.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// ...

  7. PHP商品倒计时 php实现当前用户在线人数

    //PHP商品倒计时 date_default_timezone_set("Asia/shanghai");//地区 //配置每天的活动时间段 $starttimestr = &q ...

  8. Arch: Configurations

    the original purpose is to show the steps needed to setup i3 in vbox.. easy. alright, it is a bit mi ...

  9. Android与路由器连接服务

    界面UI: package my.work.Library; import java.util.Timer; import java.util.TimerTask; import java.util. ...

  10. HDU 2668 Daydream

    用一个队列来维护 每次加入一个字符,如果字符没有在队列里,那么直接入队,并且检查更新队列大小. 如果加入的字符在队列里了,那么要一直弹出队首,直到弹出的字符和当前要插入的一样. #include< ...