标签:des style blog http io color ar os for
Input
Output
Sample Input
2 6 7 21 0 0 0 0 0 0 0 0 0 0 0 13 0 0 0 0 0 0 0 0 7 0 15 0 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 6 7 20 0 0 0 0 0 0 0 0 0 0 0 13 0 0 0 0 0 0 0 0 7 0 15 0 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0
Sample Output
37 28
题目大意:尽可能多的摘花生;但是保证以下两点:
1、不要超时间(时间计算:每移动一下或者摘一次花生为一个单位时间)
2、要按照从多到少的顺序摘花生。
代码及分析(Runtime Error):
1 #include<stdio.h> 2 #include<algorithm> 3 #include<math.h> 4 using namespace std; 5 struct pea{ 6 int gains; 7 int x; 8 int y; 9 }peanut[21*21]; 10 //(x,y)记录花生的位置,y平行于大路;gains记录花生结的果实 11 bool cmp(pea a,pea b){ 12 return a.gains>b.gains; 13 } 14 int main() 15 { 16 int M,N,K,T; 17 //M,N表示花生地的长(平行于x)和宽(平行于y); 18 //K表示时间限制;T表示测试组数; 19 scanf("%d",&T); 20 while(T--){ 21 scanf("%d%d%d",&M,&N,&K); 22 int i,j,k,gains=0;//gains表示收获的花生; 23 for(i=0,k=0;i<M;i++) 24 for(j=0;j<N;j++,k++){//输入花生地的信息 25 scanf("%d",&peanut[k].gains); 26 peanut[k].x=i,peanut[k].y=j; 27 } 28 sort(peanut,peanut+k,cmp);//将花生按照果实多少排序 29 int timeused=peanut[0].x+1+1; //第一次用时; 30 if(timeused+timeused+peanut[i].x+1<=K){ 31 //判断第一次会不会超时, 32 for(i=0;timeused+peanut[i].x+1<=K;i++){//如果不超时,则自动搜寻下一个目标。。。 33 gains=gains+peanut[i].gains; 34 timeused=timeused+(abs(peanut[i+1].y-peanut[i].y)+1+abs(peanut[i+1].x-peanut[i].x+1)); 35 } 36 printf("%d\n",gains); 37 } 38 } 39 return 0; 40 }
解题心得:这还是体力活,虽然题没有AC,但是的确学到了很多东西。
标签:des style blog http io color ar os for
原文地址:http://www.cnblogs.com/yanglingwell/p/4077154.html