洛谷 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 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;
}

你可能感兴趣的:(洛谷刷题之旅,深度优先,算法,java,数据结构)