Poj 1925 Spiderman (DP)

Source: Internet
Author: User

Question Link

Spiderman sticks the Spiderman to a back-end building from the leftmost building. Then, the Spiderman hangs over and sends the spider silk until it reaches the last building. Ask at least a few splits.

Idea: Skip a building from x-axis J. I must meet the following requirements: (P [I]. x-j) * (p [I]. x-j) <= P [I]. y * P [I]. y-(P [I]. y-P [0]. y) * (p [I]. y-P [0]. Y ). from X coordinate J to X coordinate 2 * P [I] After building I. x-J.

So the state transition equation is: DP [2 * P [I]. x-J] = min (DP [2 * P [I]. x-J], d [J] + 1 ). Because this person follows an arc, the end point of each swing must be the height of the first building.

First, traverse the building, and then traverse the location. The second layer loops look for where he wants to swing, because it is symmetric.

 1 //1925 2 #include <cstdio> 3 #include <string> 4 #include <cstring> 5 #include <iostream> 6  7 using namespace std ; 8  9 struct node10 {11     long long x ;12     long long  y ;13 }p[5120] ;14 int dp[2051532] ;15 16 int main()17 {18     int K ,N;19     scanf("%d",&K) ;20     while(K--)21     {22         scanf("%d",&N) ;23         for(int i = 0 ; i < N ; i++)24             scanf("%I64d %I64d",&p[i].x,&p[i].y) ;25         memset(dp,100,sizeof(dp)) ;26         dp[p[0].x] = 0 ;27         for(int i = 1 ; i < N ; i++)28             for(int j = p[i].x-1 ; j >= p[0].x ; j--)29         {30             long long  dis = (p[i].x-j)*(p[i].x-j)+(p[i].y-p[0].y)*(p[i].y-p[0].y) ;31             if(dis > (long long)(p[i].y*p[i].y))32                 break ;33             dp[2*p[i].x-j] = min(dp[2*p[i].x-j],dp[j]+1) ;34         }35         int ans = 99999999 ;36         for(int i = p[N-1].x ; i <= 2*p[N-1].x ; i++)37             ans = min(ans,dp[i]) ;38         if(ans >= 99999999)39             ans = -1 ;40         printf("%d\n",ans) ;41     }42     return 0 ;43 }
View code

 

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.