Poj1054 the troublesome frog [DP]

Source: Internet
Author: User

Hard to write the previous article. It's too tired for a semester.

#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(const void* a,const void* b){Grid* one;Grid* two;one=(Grid* ) a;two=(Grid* ) b;return ((one->x)==(two->x))?(one->y)-(two->y):(one->x)-(two->x);}int getstep(Grid grs,int dx,int dy){int step=0;while(grs.x<=n&&grs.x>=1 &&grs.y>=1&&grs.y<=m){if(!bsearch(&grs,grid,num,sizeof(Grid),cmp)){step=0;break;}else{grs.x+=dx,grs.y+=dy;step++;}}return step;}int main(){ //ios::sync_with_stdio(false);    //freopen("/home/rainto96/in.txt","r",stdin);while(scanf("%d%d",&m,&n)!=EOF){scanf("%d",&num);for(int i=0;i<=num;i++){int xx,yy;scanf("%d%d",&xx,&yy);grid[i].x=yy,grid[i].y=xx;}qsort(grid,num,sizeof(Grid),cmp);int maxn=2;for(int i=0;i<num-1;i++){for(int j=i+1;j<num;j++){                    int dx,dy,px,py;                    dx=grid[j].x-grid[i].x;                    dy=grid[j].y-grid[i].y;                    px=grid[i].x-dx;                    py=grid[i].y-dy;                    if(py>=1&&py<=m&&px>=1&&px<=n)                        continue;                    if(grid[i].x+maxn*dx>n)                        break;                    py = grid[i].y + maxn * dy;                if ( py > m || py < 1)                  continue;                     int step=getstep(grid[i],dx,dy);                    maxn=max(maxn,step);}}maxn=(maxn==2?0:maxn);printf("%d\n",maxn);}return 0;}


I learned two-dimensional binary, but it's actually just one sentence.

Usage: void * bsearch (const void * Key, const void * base, size_t nelem, size_t width, INT (* fcmp) (const void *, const *); Syntax: # include <stdlib. h> void * bsearch (const void * Key, const void * Buf, size_t num, size_t size, INT (* compare) (const void *, const void *); parameter: first: the keyword to be searched. Second: the array to be searched. Third: specify the number of elements in the array. Fourth: the length of each element (in characters ). Fifth: pointer to the comparison function. Function: The function uses the semi-lookup method to match the parameter key from the array element Buf [0] To the Buf [num-1. If the first parameter of the Compare function is smaller than the second parameter, a negative value is returned. If the value is equal to zero, a positive value is returned. Elements in the array Buf should be arranged in ascending order. The Return Value of the function bsearch () is a match. If no match is found, null is returned.


This problem seems complicated, but its purpose is very simple: to help farmers find the most harmful frog. That is, to find

A path of frogs crossing a rice field. The trampled rice on this path is not less than the trampled water on any other frog path.
Rice count. Of course, there may be no frog paths in the entire rice field. The key to the problem is: finding the completeness of rice field crossing
Path of the frog. Any frog path across rice fields, including at least three trampled rice trees. Assume that the first two
The trampled rice trees are (x1, Y1) and (X2, Y2) respectively, so:
Export Order dx = X2-X1, dy = Y2-Y1; X0 = X1-dx, Y0 = Y1-dy; X3 = x2 + dx, Y3 = y2 + dy
The Frog (x0, y0) is located outside the rice field. After jumping from this position, the frog enters the rice field and the rice field in the trampled position (x1, Y1 ).
✓ (X3, Y3) is located in the rice field, where the first 3rd rice trees are trampled by frogs.
Using xi = x0 + I using dx, YI = Y1 + I using DY (I using 3), if (XI, Yi) is located within the rice field, then (XI, Yi) rice on will be
Frog step on
According to the above rules, as long as you know the first two trampled rice trees on a frog path, you can find
His rice. To find all the paths of frogs, take any two rice (x1, Y1), (X2,
Y2), determine whether (x1, Y1), (X2, Y2) can be used as the first two rice to be trampled on a frog path.
Solution
The most basic element in the Problem description is the trampled rice. Select an appropriate data structure in the program,
To express this basic element. Is this data structure suitable based on whether the element can be expressed in a program?
It is represented by a word or phrase, that is, a variable.
Struct plant {// describe a trampled rice
Int X; // The row number of the rice
Int y; // The column number of the rice
}
The main calculation of this problem is: select two rice trees (x1, Y1) and (X2, Y2) from the trampled rice ). Determine if they are
The first two flags to be trampled on a frog path. (X1, Y1), (X2, Y2) are the only methods used to determine the slogan
The direction and step size start from (X2, Y2) and walk in the rice field along this direction and step size. Each step is taken to determine the location
Whether the rice on the top (X, Y) is trampled until it leaves the rice field. If (X, Y) is not trampled on at a certain step,
The assumption that (x1, Y1) and (X2, Y2) are the first rice to be trampled on a frog path is not true. This sentence
The broken algorithm must be used repeatedly in the problem solving process. Its efficiency becomes the key to determining the overall computing efficiency.
The crop uses a plant array plants [5001] to indicate all the trampled rice.
Sort the elements in plants in ascending (or descending) Order of row/column numbers
Histogram uses the binary method to check whether plants has elements with values of (x, y): Compares the elements of (x, y) with plants,
(1) equal, indicating that the element is found; (2) smaller than the middle element of plants, continue searching in the First Half of plants; (3)
It is larger than the middle element of plants, and continues searching in the lower half of plants.
The preceding method is used to determine whether the rice at the position (x, y) where each step is taken is trampled. A maximum of log2n can be compared,
N is the total number of Rice trampled in the rice field.

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.