| ||||||||||
Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
从郭伟老师那里过来的,小白记忆型递归,大佬轻喷。#include<stdio.h> struct platform { int platform_l; int platform_r; int platform_h; }; int n,x,y,max; struct platform p[1001]; int rem[1002][3]; int mintwo(int a,int b) { if(a>b) return b; return a; } int existence(int num,int local) { int i; for(i=num+1;i<=n;i++) { if( (p[i].platform_l<=local&&p[i].platform_r>=local) && p[num].platform_h-p[i].platform_h<=max ) { return i; } } return 0; } int mintime(int num,int side) { int x_local; int z,x,y; int name; if(side==1) x_local=p[num].platform_l; if(side==2) x_local=p[num].platform_r; name=existence(num,x_local); if(rem[num][side]!=-1) return rem[num][side]; if(name==0) { if(p[num].platform_h>max) rem[num][side]=1000000; else rem[num][side]=p[num].platform_h; return rem[num][side]; } else { x=mintime(name,1)+(p[num].platform_h-p[name].platform_h)+(x_local-p[name].platform_l); y=mintime(name,2)+(p[num].platform_h-p[name].platform_h)+(p[name].platform_r-x_local); rem[num][side]=mintwo(x,y); return rem[num][side]; } } void main() { int i,j; int t; struct platform temp; scanf("%d",&t); while(t) { scanf("%d%d%d%d",&n,&x,&y,&max); p[0].platform_l=x; p[0].platform_r=x; p[0].platform_h=y; for(i=1;i<=n;i++) { scanf("%d%d%d",&p[i].platform_l,&p[i].platform_r,&p[i].platform_h); } //排序 for(i=1;i<=n-1;i++) { for(j=1;j<=n-i;j++) { if(p[j].platform_h<p[j+1].platform_h) { temp=p[j]; p[j]=p[j+1]; p[j+1]=temp; } } } for(i=0;i<1002;i++) { for(j=0;j<3;j++) { rem[i][j]=-1; } } //左是一右是二 printf("%d\n",mintime(0,1)); t--; } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator