洛谷 P1135 奇怪的电梯 P1135 Java

提供两种思路

第一种DFS

超时第九和第十点

import java.util.*;
import java.io.*;

public class Main{

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    static int N,A,B,INF;
    static int[] lift;
    static int[] distance;
    static boolean[] judge;

    public static void main(String[] args) throws IOException {
        String[] S = br.readLine().split(" ");
        N = Integer.parseInt(S[0]);
        A = Integer.parseInt(S[1]);
        B = Integer.parseInt(S[2]);
        INF = 0x3f3f3f3f;
        lift = new int[N+1];
        judge = new boolean[N+1];
        distance = new int[N+1];
        Arrays.fill(distance,INF);
        S = br.readLine().split(" ");
        for(int i = 0 ; i < S.length ; i++) {
            lift[i+1] = Integer.parseInt(S[i]);
        }
        DFS(A , 0);
        if(distance[B] == 0x3f3f3f3f) {
            out.write("-1");
        }
        else {
            out.write(distance[B] + "");
        }
        out.flush();
        out.close();
        br.close();
    }

    private static void DFS(int layer , int count) {
        if(layer == B) {
            distance[B] = Math.min(distance[B], count);
            return ;
        }
        if(layer + lift[layer] <= N && !judge[layer+lift[layer]]) {
            judge[layer+lift[layer]] = true;
            count++;
            layer += lift[layer];
            DFS(layer , count);
            count--;
            layer -= lift[layer];
            judge[layer+lift[layer]] = false;
        }
        if(layer - lift[layer] >= 1 && !judge[layer - lift[layer]]) {
            judge[layer-lift[layer]] = true;
            count++;
            layer -= lift[layer];
            DFS(layer , count);
            count--;
            layer += lift[layer];
            judge[layer-lift[layer]] = false;
        }
    }
}

第二种BFS

AC

import java.util.*;
import java.io.*;

public class Main{

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    static int N,A,B;
    static int[] lift;
    static boolean[] judge;

    public static void main(String[] args) throws IOException {
        String[] S = br.readLine().split(" ");
        N = Integer.parseInt(S[0]);
        A = Integer.parseInt(S[1]);
        B = Integer.parseInt(S[2]);
        lift = new int[N+1];
        judge = new boolean[N+1];
        S = br.readLine().split(" ");
        for(int i = 0 ; i < S.length ; i++) {
        	lift[i+1] = Integer.parseInt(S[i]);
        }
        BFS();
        out.flush();
        out.close();
        br.close();
    }
    
    private static void BFS() throws IOException {
    	Point first = new Point();
    	first.index = A;
    	first.layer = 0;
    	Deque<Point> deque = new LinkedList<>();
    	deque.offer(first);
    	Point result = first;
    	while(!deque.isEmpty()) {
    		result = deque.poll();
    		if(result.index == B) {
    			break;
    		}
    		if(result.index + lift[result.index] <= N && !judge[result.index + lift[result.index]]) {
    			Point node = new Point();
    			node.layer = result.layer + 1;
    			node.index = result.index + lift[result.index];
    			judge[result.index + lift[result.index]] = true;
    			deque.offer(node);
    		}
    		if(result.index - lift[result.index] >= 1 && !judge[result.index - lift[result.index]]) {
    			Point node = new Point();
    			node.layer = result.layer + 1;
    			node.index = result.index - lift[result.index];
    			judge[result.index - lift[result.index]] = true;
    			deque.offer(node);
    		}
    	}
    	if(result.index == B) {
    		out.write(result.layer + "");
    	}
    	else {
    		out.write("-1");
    	}
    }
}

class Point{
	int layer;
	int index;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值