Medium POJ 1054 The troublesome Frog, the memory of the search.

Source: Internet
Author: User

Description

In Korea, the naughtiness of the Cheonggaeguri, a small frog, is legendary. This was a well-deserved reputation, because the frogs jump through your rice paddy at night, flattening rice plants. In the morning, after noting which plants has been flattened, you want to identify the path of the frog which did the MoS T damage. A frog always jumps through the paddy on a straight line, with every hop the same length:

Your rice paddy have plants arranged on the intersection points of a grid as shown in Figure-1, and the Troublesome frogs H Op completely through your paddy, starting outside the Paddy on one side and ending outside the paddy in the other side as Shown in Figure-2:

Many frogs can jump through the paddy and hopping from rice plant to rice plant. Every hop lands on a plant and flattens it, as in Figure-3. Note that some plants is landed on by more than one frog during the night. Of course, you can don't see the lines showing the paths of the frogs or any of the their hops outside of your paddy? For the SI Tuation in Figure-3, what can see is shown in Figure-4:

From Figure-4, you can reconstruct all the possible paths which the frogs of May has followed across your paddy. Interested in frogs which has landed on 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, which means that the three paths shown in Figure-3 is frog paths (there is also other possible frog paths). The vertical path down column 1 might has been a frog path with hop length 4 except there is only 2 plants flattened so We is not interested; and the diagonal path including the plants on Row 2 col. 3, Row 3 Col. 4, and Row 6 Col. 7 have three flat plants but there is no regular hop length which could has spaced the hops in this to and still landing on at least 3 plants, and hence It is a frog path. Note also that along the line a frog path follows there could be additional flattened plants which does not need to be landed On from that path (see the Plant @ (2, 6) on the horizontal path across Row 2 in Figure-4), and in fact some flattened plants could 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 are 7, obtained from the frog path across row 6.  The test instructions description of a very painful egg, is to give a graph, there are many points above, and then ask the longest trajectory is how much.  Just start thinking wrong direction, has been thinking from the overall angle how DP, and then Baidu found to be search, then think of the direct enumeration of two points, and then DFS is good, which plus memory, it can be. Note Card memory, so say to hash the point, hash value also to take a suitable, the first time on the tle ... The code is as follows:
//The ━━━━━━ of gods and Beasts ━━━━━━//┏┓┏┓//┏┛┻━━━━━━━┛┻┓//┃┃//┃━┃//████━████┃//┃┃//┃┻┃//┃┃//┗━┓┏━┛//┃┃//┃┃//┃┗━━━┓//┃┣┓//┃┏┛//┗┓┓┏━━━━━┳┓┏┛//┃┫┫┃┫┫//┗┻┛┗┻┛////━━━━━━ Feel the ━━━━━━ of Meng Meng//author:whywhy//Created time:2015 July 20 Monday 08:55 13 seconds//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 namespacestd;Const intmaxn=5005;intr,c; ShortDP[MAXN][MAXN];intN;structpoint{intx, y; BOOL operator< (ConstPoint &b)Const    {        if(x==b.x)returny<b.y; returnx<b.x; }}p[maxn];inlineBOOL inch(intXinty) {    returnX<=r && x>=1&& y<=c && y>=1;}Const intHash=1000003;structhashmap{intHead[hash],next[maxn],hcou; unsignedLong LongKEY[MAXN]; intREM[MAXN]; voidinit () {Hcou=0; memset (Head,-1,sizeof(head)); }    voidInsert (unsignedLong LongValintID) {inth=val%HASH;  for(inti=head[h];i!=-1; i=Next[i])if(val==Key[i])return; Rem[hcou]=ID; Key[hcou]=Val; Next[hcou]=Head[h]; HEAD[H]=hcou++; }    intFind (unsignedLong Longval) {        intH=val%HASH;  for(inti=head[h];i!=-1; i=Next[i])if(val==Key[i])returnRem[i]; return 0; }}map1;intDfsintUintv) {    if(Dp[u][v])returnDp[u][v]; intDx= (p[v].x<<1)-p[u].x,dy= (p[v].y<<1)-p[u].y; if(!inch(dx,dy))return 2; intRp=map1.find (dx*10000+dy); if(!RP) {Dp[u][v]=-1; return-1; } Dp[u][v]=dfs (V,RP) +1; returndp[u][v];} InlineBOOLJudgeintUintv) {    intDx= (p[u].x<<1)-p[v].x,dy= (p[u].y<<1)-p[v].y; return!inch(Dx,dy);}intMain () {//freopen ("In.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout);    intans=- -; scanf ("%d%d",&r,&b); scanf ("%d",&N);  for(intI=1; i<=n;++i) scanf ("%d%d",&p[i].x,&p[i].y); Sort (P+1, p+n+1);    Map1.init ();  for(intI=1; i<=n;++i) Map1.insert (10000*p[i].x+p[i].y,i);  for(intI=1; i<=n;++i) for(intj=i+1; j<=n;++j)if(judge (I,j)) ans=Max (Ans,dfs (i,j)); if(ans<3) ans=0; printf ("%d\n", ans); return 0;}
View Code

Medium POJ 1054 The troublesome Frog, the memory of the search.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.