Recursive, dynamic programming, finding the shortest path, help Jimmy

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=1661

Problem Solving Report:

1. Every time a mouse comes to a plank, there are only two ways to go, and you can use recursion

#include <stdio.h>#include<string.h>#include<stdlib.h>#include<memory.h>#define_max 1010#defineINF 0x3f3f3f3fstructplatform{intlx//the coordinates of the left side of the plank    intRx//the coordinates of the right end of the plank    intH//the height of the plank from the ground} P[_max];intMax,n;intLeftmin[_max];//The shortest time from the left end of each plank to the groundintRightmin[_max];intMy_compare (Const void*e1,Const void*E2)//used in fast sorting, higher planks are placed in front{    structPlatform *p1,*P2; P1=(structplatform*) E1; P2=(structplatform*) E2; returnP2->h-p1->h;}intMintime (intLintFlag//jumping from a wooden board numbered L, flag indicates the direction to jump from the plank, flag=1 to the left{    inty=p[l].h; intI,x,ltime,rtime; if(flag) x=P[L].LX;//X is Jimmy's coordinates, which means the left end of the plank .    Elsex=P[l].rx;  for(i=l+1; i<=n; i++)//start looking for planks .    {        if(p[i].lx<=x&&p[i].rx>=x) Break; }    if(i<=n)//find the plank, but it will fall .    {        if(y-p[i].h>MAX)returnINF; }    Else//There's no wood underneath.    {        if(y>MAX)returnINF; Else returnY//no plank jumps straight down} ltime=y-p[i].h+x-P[I].LX; Rtime=y-p[i].h+p[i].rx-x; if(leftmin[i]==-1) Leftmin[i]=mintime (I,1); if(rightmin[i]==-1) Rightmin[i]=mintime (I,0); Ltime+=Leftmin[i]; Rtime+=Rightmin[i]; if(ltime<rtime)returnLtime; Else returnRtime;}intMain () {intI,cases,x,y; scanf ("%d",&cases);  while(cases--) {scanf ("%d%d%d%d",&n,&x,&y,&MAX); memset (Leftmin,-1,sizeof(leftmin)); memset (Rightmin,-1,sizeof(rightmin)); p[0].lx=x; p[0].rx=x; p[0].h=y;  for(i=1; i<=n; i++) {scanf ("%d%d%d",&p[i].lx,&p[i].rx,&p[i].h); } qsort (P,n+1,sizeof(structPlatform), my_compare); printf ("%d\n", Mintime (0,1)); }    return 0;}

Recursive, dynamic programming, finding the shortest path, help Jimmy

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.