hdu1072 记忆化bfs(java)

本文提供了一道关于在带有定时炸弹的地图上寻找最短路径的编程题解。通过BFS算法结合特定规则判断人物如何在地图上移动以到达目的地,同时考虑定时炸弹的影响。

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

题目链接:打开链接

题目大意:

在n×m的地图上,0表示墙,1表示空地,2表示人,3表示目的地,
4表示有定时炸弹重启器。定时炸弹的时间是6,人走一步所需要的时间是1。每次可以上、下、左、右移动一格。当人走到4时如果炸弹的时间不是0,
可以重新设定炸弹的时间为6。如果人走到3而炸弹的时间不为0时,成功走出。求人从2走到3的最短时间


解题思路:不要被炸弹重置误导了,第一次搜索到炸弹时 吧当前时间重置过后 吧到这个炸弹时间标记为已经访问过。经过可以重复走 但是相同时间走到炸弹位置是没有任何意义的。


java代码:



        import java.util.LinkedList;
        import java.util.Queue;
        import java.util.Scanner;

public class hdu1072{

    /**
     * @param args
     * 在n×m的地图上,0表示墙,1表示空地,2表示人,3表示目的地,
     * 4表示有定时炸弹重启器。定时炸弹的时间是6,人走一步所需要的时间是1。每次可以上、下、左、右移动一格。当人走到4时如果炸弹的时间不是0,
     * 可以重新设定炸弹的时间为6。如果人走到3而炸弹的时间不为0时,成功走出。求人从2走到3的最短时间
     */
    static int dx[] = {-1,0,1,0 };
    static int dy[] = { 0,-1,0,1 };
    static int n,m,f_x,f_y;
    static int[][] map=new int [9][9];
    static int vis[][] =new int[9][9];
    class Node{
        int x;
        int y;
        int to_time;
        int now_time;
        public Node(){

        }
        public Node(int x,int y,int to_time,int now_time){
            this.x=x;
            this.y=y;
            this.to_time=to_time;
            this.now_time=now_time;
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scan=new Scanner(System.in);
        int t;
        int  s_x = 0,s_y=0;
        t=scan.nextInt();
        while(t>0){
            for(int i=0;i<9;i++){
                for(int j=0;j<9;j++){
                    vis[i][j]=0;
                }
            }
            n=scan.nextInt();
            m=scan.nextInt();
            for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                    map[i][j]=scan.nextInt();
                    if(map[i][j]==2){
                        s_x=i;
                        s_y=j;
                    }
                    if(map[i][j]==3){
                        f_x=i;
                        f_y=j;
                    }
                }
            }
            System.out.println(bfs(s_x,s_y));
            t--;
        }
    }
    private static int bfs(int x, int y) {
        // TODO Auto-generated method stub
        Queue<Node> queue=new LinkedList<Node>();
        hdu1072 hdu=new hdu1072();
        Node node1=hdu.new Node();
        vis[x][y]=6;
        node1.x=x;
        node1.y=y;
        node1.to_time=0;
        node1.now_time=6;
        queue.offer(node1);
        while(queue.size()!=0){
            Node b=queue.poll();
            if(b.x==f_x&&b.y==f_y){
                return b.to_time;
            }
            for(int i=0;i<4;i++){
                int ssx=b.x+dx[i];
                int ssy=b.y+dy[i];
                if(judge(ssx,ssy)){
                    if(map[ssx][ssy]==4){
                        if(b.now_time==1)
                            continue;
                        int to_time=b.to_time+1;
                        int nowtime=6;
                        if(nowtime==0||nowtime<=vis[ssx][ssy]){
                            continue;
                        }
                        vis[ssx][ssy]=nowtime;
                        Node c=hdu.new Node(ssx,ssy,to_time,nowtime);
                        queue.offer(c);
                    }else{
                        int to_time=b.to_time+1;
                        int nowtime=b.now_time-1;
                        if(nowtime==0||nowtime<=vis[ssx][ssy]){
                            continue;
                        }
                        vis[ssx][ssy]=nowtime;
                        Node c=hdu.new Node(ssx,ssy,to_time,nowtime);
                        queue.offer(c);
                    }
                }
            }
        }
        return -1;
    }
    private static boolean judge(int x, int y) {
        // TODO Auto-generated method stub
        if(x<0||y<0||x>=n||y>=m){
            return false;
        }
        if(map[x][y]==0){
            return false;
        }
        return true;
    }

}


HDU1160是一个经典的动态规划问题,题目描述如下: 给定一组老鼠的体重和速度,要求找出最长的老鼠序列,使得序列中每只老鼠的体重和速度都严格递增。 我们可以使用动态规划来解决这个问题。具体步骤如下: 1. 将老鼠按体重或速度排序。 2. 使用动态规划数组`dp`,其中`dp[i]`表示以第`i`只老鼠结尾的最长序列长度。 3. 使用一个数组`pre`来记录每个位置的前驱老鼠,以便最后可以重建序列。 以下是使用Java实现的代码: ```java import java.util.*; class Mouse implements Comparable<Mouse> { int weight, speed, index; Mouse(int weight, int speed, int index) { this.weight = weight; this.speed = speed; this.index = index; } public int compareTo(Mouse other) { if (this.weight != other.weight) { return this.weight - other.weight; } else { return this.speed - other.speed; } } } public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); List<Mouse> mice = new ArrayList<>(); while (scanner.hasNext()) { int weight = scanner.nextInt(); int speed = scanner.nextInt(); mice.add(new Mouse(weight, speed, mice.size())); } scanner.close(); Collections.sort(mice); int n = mice.size(); int[] dp = new int[n]; int[] pre = new int[n]; int maxLen = 1, endIndex = 0; Arrays.fill(pre, -1); Arrays.fill(dp, 1); for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { if (mice.get(i).weight > mice.get(j).weight && mice.get(i).speed > mice.get(j).speed) { if (dp[j] + 1 > dp[i]) { dp[i] = dp[j] + 1; pre[i] = j; } } } if (dp[i] > maxLen) { maxLen = dp[i]; endIndex = i; } } List<Integer> sequence = new ArrayList<>(); while (endIndex != -1) { sequence.add(mice.get(endIndex).index + 1); endIndex = pre[endIndex]; } System.out.println(maxLen); for (int i = sequence.size() - 1; i >= 0; i--) { System.out.println(sequence.get(i)); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值