nyoj 613 免费馅饼 广搜

免费馅饼

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
 
描述
都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼。说来gameboy的人品实在是太好了,这馅饼别处都不 掉,就掉落在他身旁的10米范围内。馅饼如果掉在了地上当然就不能吃了,所以gameboy马上卸下身上的背包去接。但由于小径两侧都不能站人,所以他只 能在小径上接。由于gameboy平时老呆在房间里玩游戏,虽然在游戏中是个身手敏捷的高手,但在现实中运动神经特别迟钝,每秒种只有在移动不超过一米的 范围内接住坠落的馅饼。现在给这条小径如图标上坐标:

为了使问题简化,假设在接下来的一段时间里,馅饼都掉落在0-10这11个位置。开始时gameboy站在5这个位置,因此在第一秒,他只能接到4,5,6这三个位置中其中一个位置上的馅饼。问gameboy最多可能接到多少个馅饼?(假设他的背包可以容纳无穷多个馅饼)
 
输入
输入数据有多组。每组数据的第一行为以正整数n(0<n<100000),表示有n个馅饼掉在这条小径上。在结下来的n行中,每行有两个整数x,T(0<T<100000),表示在第T秒有一个馅饼掉在x点上。同一秒钟在同一点上可能掉下多个馅饼。n=0时输入结束。
输出
每一组输入数据对应一行输出。输出一个整数m,表示gameboy最多可能接到m个馅饼。
提示:本题的输入数据量比较大,建议用scanf读入,用cin可能会超时。
样例输入
6
5 1
4 1
6 1
7 2
7 2
8 3
0
样例输出
4
来源

写错了一个字母,提交了12次才过    千万得注意啊!!!!!
分析:
       数据分析  建表 :
    
位置 第一秒 第二秒 第三秒
4 1    
5 1    
6 1    
7   2  
8     1
        广搜+递归:
      2、上右 t2=f[dis-1][time+1]
      3、下右 t3=f[dis+1][time+1]
 1 #include<stdio.h>
 2 #include<string.h>
 3 int a[11][100001],b[11][100001];
 4 int ans,max;
 5 int maxx(int a,int b)
 6 {
 7     return a>b?a:b;
 8 }
 9 int bfs(int di,int ti)
10 {
11     int t1=0,t2=0,t3=0;
12     if(b[di][ti]>=0)
13         return b[di][ti];
14     if(ti>max)
15         return 0;
16     t1=bfs(di,ti+1)+a[di][ti+1];
17     if(di>0)
18         t2=bfs(di-1,ti+1)+a[di-1][ti+1];
19     if(di<10)
20         t3=bfs(di+1,ti+1)+a[di+1][ti+1];
21     return b[di][ti]=maxx(t1,maxx(t2,t3));
22 }
23 int main()
24 {
25     int n,t,d;
26     while(scanf("%d",&n),n)
27     {
28         memset(a,0,sizeof(a));
29         memset(b,-1,sizeof(b));
30         max=-1;
31         while(n--)
32         {
33             scanf("%d %d",&d,&t);
34             if(t>max)
35                 max=t;
36             ++a[d][t];
37         }
38         ans=0;
39         printf("%d
",bfs(5,0));
40     }
41     return 0;
42 }
View Code

 java:

 1 //package rumen;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Main {
 6     /**
 7      * 动态规划
 8      * 类似于数塔问题,
 9      * 但是本题是倒着的数塔,可以有三个方向
10      */
11         public static void main(String[] args) {
12             Scanner scan=new Scanner(System.in);
13             while(true){
14                 int N=scan.nextInt();
15                 if(N==0)
16                     break;
17                 int[][] nn=getInput(scan,N);
18                 int T=0;
19                 for(int i=0;i<N;i++){
20                     if(nn[1][i]>T)
21                         T=nn[1][i];
22                 }
23                 int[][] bb=new int[T+1][11];
24                 for(int i=0;i<N;i++){
25                     bb[nn[1][i]][nn[0][i]]++;
26                 }
27                 //寻找最大路径
28                 for(int t=T-1;t>=0;t--){
29                     bb[t][0]+=Math.max(bb[t+1][0],bb[t+1][1]);
30                     bb[t][10]+=Math.max(bb[t+1][10],bb[t+1][9]);
31                     for(int i=1;i<10;i++){
32                         bb[t][i]+=Math.max(bb[t+1][i-1],Math.max(bb[t+1][i],bb[t+1][i+1]));
33                     }
34                 }
35                 //起始位置为5的最大能接到的馅饼数
36                 System.out.println(bb[0][5]);
37             }
38         }
39         
40         /**
41          * 接收输入
42          */
43         public static int[][] getInput(Scanner scan,int N) {
44              int[][] nn=new int[2][N];
45              for(int i=0;i<N;i++){
46                  //位置
47                  nn[0][i]=scan.nextInt();
48                  //时间
49                  nn[1][i]=scan.nextInt();
50              }
51              return nn;
52         }
53     }
54             
View Code
原文地址:https://www.cnblogs.com/asd1234/p/3351836.html