SW练习_跳跃路径_动态规划

本文通过实例探讨了如何使用动态规划解决一种特殊的跳跃路径问题。通过建立状态转移方程,逐步解析每一步的决策过程,从而找到最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.company;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;

/**
 * meng3.wei 2020.05.08
 (输入)
3
3
1 1
2 2
3 3
3
1 1
2 2
3 1
4
1 1
3 1
2 4
4 4

(输出)
#1 2
#2 -1
#3 1

 */
public class 跳跃路径 {
    static int T;
    static int N;
    static Point[] points;
    static int[] result;
    static int[] pCount;//存储开始节点到这个节点的路径个数
    static int MAX=1000000007;
    static boolean logger=false;
    public static void main(String[] args)throws Exception {
        //System.setIn(new FileInputStream("D:\\sw_case\\35\\test_input.txt"));
        //System.setIn(new FileInputStream("D:\\sw_case\\35\\sample_input_2.txt"));
        //System.setIn(new FileInputStream("D:\\sw_case\\35\\sample_input.txt"));
        System.setIn(new FileInputStream("D:\\sw_case\\35\\eval_input.txt"));
        BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
        T=Integer.parseInt(reader.readLine());
        result=new int[T];

        for (int i = 0; i < T; i++) {
            N=Integer.parseInt(reader.readLine());
            pCount=new int[N];
            points=new Point[N];
            String str[];
            for (int j = 0; j <N ; j++) {
                str=reader.readLine().split(" ");
                points[j]=new Point(Integer.parseInt(str[0]),Integer.parseInt(str[1]));
            }
            Arrays.sort(points,1,N-1);//要把第一个节点和最后一个节点排除掉

            if(points[N-1].x <=points[0].x || points[N-1].y <=points[0].y){
                result[i]=-1;
               continue;
            }
            pCount[0]=1;
            for (int j = 1; j < points.length; j++) {//到达当前节点的路径个数=sum(每个可以达到当前节点的路径个数)
                for (int k = 0; k < j; k++) {
                    if(points[j].x>points[k].x
                    &&points[j].y>points[k].y
                    ){//k是起始节点 j是结束节点
                        pCount[j]=(pCount[j]+pCount[k])%MAX;
                    }
                }
            }
            result[i]=pCount[N-1];
        }
        for (int i = 0; i < T; i++) {
            System.out.printf("#%d %d\n",(i+1),result[i]);
        }



        reader.close();

    }

}
class Point implements Comparable<Point>{
    public int x;
    public int y;
    public Point(int x,int y){
        this.x=x;
        this.y=y;
    }


    @Override
    public int compareTo(Point o2) {
        if(this.x==o2.x){
            return this.y-o2.y;
        }else{
            return this.x-o2.x;
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值