🌷🌼λͺ¨μ—¬λ΄μš” 개발의숲🌷🌼

[JAVA/μ½”λ”©ν…ŒμŠ€νŠΈ] 솑아지 μ°ΎκΈ° (BFS : μƒνƒœνŠΈλ¦¬νƒμƒ‰) λ³Έλ¬Έ

개발/μ•Œκ³ λ¦¬μ¦˜ λ¬Έμ œν’€μ΄ JAVA

[JAVA/μ½”λ”©ν…ŒμŠ€νŠΈ] 솑아지 μ°ΎκΈ° (BFS : μƒνƒœνŠΈλ¦¬νƒμƒ‰)

μš”μΌμ΄ 2021. 8. 1. 15:54
λ°˜μ‘ν˜•

μ„€λͺ…

ν˜„μˆ˜λŠ” 솑아지λ₯Ό μžƒμ–΄λ²„λ Έλ‹€. λ‹€ν–‰νžˆ μ†‘μ•„μ§€μ—λŠ” μœ„μΉ˜μΆ”μ κΈ°κ°€ 달렀 μžˆλ‹€.

ν˜„μˆ˜μ˜ μœ„μΉ˜μ™€ μ†‘μ•„μ§€μ˜ μœ„μΉ˜κ°€ μˆ˜μ§μ„ μƒμ˜ μ’Œν‘œ 점으둜 주어지면 ν˜„μˆ˜λŠ” ν˜„μž¬ μœ„μΉ˜μ—μ„œ μ†‘μ•„μ§€μ˜ μœ„μΉ˜κΉŒμ§€ λ‹€μŒκ³Ό 같은 λ°©λ²•μœΌλ‘œ μ΄λ™ν•œλ‹€.

μ†‘μ•„μ§€λŠ” 움직이지 μ•Šκ³  μ œμžλ¦¬μ— μžˆλ‹€.

ν˜„μˆ˜λŠ” 슀카이 콩콩을 타고 κ°€λŠ”λ° ν•œ 번의 μ ν”„λ‘œ μ•žμœΌλ‘œ 1, λ’€λ‘œ 1, μ•žμœΌλ‘œ 5λ₯Ό 이동할 수 μžˆλ‹€.

μ΅œμ†Œ λͺ‡ 번의 μ ν”„λ‘œ ν˜„μˆ˜κ°€ μ†‘μ•„μ§€μ˜ μœ„μΉ˜κΉŒμ§€ 갈 수 μžˆλŠ”μ§€ κ΅¬ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜μ„Έμš”.

μž…λ ₯

첫 번째 쀄에 ν˜„μˆ˜μ˜ μœ„μΉ˜ S와 μ†‘μ•„μ§€μ˜ μœ„μΉ˜ Eκ°€ 주어진닀. μ§μ„ μ˜ μ’Œν‘œ 점은 1λΆ€ν„° 10,000κΉŒμ§€μ΄λ‹€.

좜λ ₯

μ ν”„μ˜ μ΅œμ†ŒνšŸμˆ˜λ₯Ό κ΅¬ν•œλ‹€. 닡은 1이상이며 λ°˜λ“œμ‹œ μ‘΄μž¬ν•©λ‹ˆλ‹€.

μ˜ˆμ‹œ μž…λ ₯ 1 

5 14

μ˜ˆμ‹œ 좜λ ₯ 1

3

 

 

- 2022.01.25 풀이

import java.util.*;

public class Main {

    public static void solution(int S, int E) {

        // κ²°κ³Όκ°’
        int result = 1;

        Queue<Integer> Q = new LinkedList<>();
        // 쀑볡방지λ₯Ό μœ„ν•œ arrayList
        ArrayList<Integer> arrayList = new ArrayList();

        Q.offer(S);

        while(!Q.isEmpty()){

            int len = Q.size();

            for(int i=0; i<len; i++){

                int compare = Q.poll();

                if(compare+1 == E || compare+5 == E || compare-1 == E){
                    System.out.println(result);
                    return;
                }
                
                // 쀑볡방지
                if(!arrayList.contains(compare+1)){
                    Q.offer(compare+1);
                    arrayList.add(compare+1);
                }
                if(!arrayList.contains(compare+5)){
                    Q.offer(compare+5);
                    arrayList.add(compare+5);
                }
                if(!arrayList.contains(compare-1)){
                    Q.offer(compare-1);
                    arrayList.add(compare-1);
                }
            }
            result++;
        }

    }
    
    public static void main(String[] args) {

        Main main = new Main();
        Scanner kb = new Scanner(System.in);

        // ν˜„μˆ˜μ˜ μœ„μΉ˜
        int S = kb.nextInt();
        // μ†‘μ•„μ§€μ˜ μœ„μΉ˜
        int E = kb.nextInt();

        main.solution(S, E);
    }
}

- μ½”λ“œ

import java.util.*;

class Main{
	int answer = 0;
	int[] dis= {1, -1, 5};
	// ν•œλ²ˆ λ°©λ¬Έ ν–ˆλ˜κ²ƒμ„ μ €μž₯ - μ‹œκ°„λ³΅μž‘λ„ 쀄이기 μœ„ν•¨
	int[] ch;
	Queue<Integer> Q = new LinkedList<>();
	public int BFS(int s, int e) {
		
		ch = new int[10001];
		ch[s] = 1;
		Q.offer(s);
		int Level = 0;
		
		while(!Q.isEmpty()) {
			int len=Q.size();
			
			for(int i=0; i<len; i++) {
				int x = Q.poll();
				// 방법 1
				//if(x==e) {
				//	return Level;
				//}	
				for(int j=0; j<3; j++) {
						int nx = x+dis[j];
						// 방법 2
						if(nx==e) {
							return Level + 1;
						}
						// λ°©λ¬Έ μ•ˆν•œκ±°
						if(nx >=1 && nx<=10000 && ch[nx] == 0) {
							ch[nx] = 1;
							Q.offer(nx);
						}
					}	
				
			}
			Level ++;
		}
		
		return Level;
	}
	
	public static void main(String[] args) {
		Main T = new Main();
		Scanner in = new Scanner(System.in);
		int s = in.nextInt();
		int e = in.nextInt();
		System.out.println(T.BFS(s, e));
	}
	
}
Comments