13549번: 숨바꼭질 3

수빈이는 동생과 숨바꼭질을 하고 있다. 수빈이는 현재 점 N(0 ≤ N ≤ 100,000)에 있고, 동생은 점 K(0 ≤ K ≤ 100,000)에 있다. 수빈이는 걷거나 순간이동을 할 수 있다. 만약, 수빈이의 위치가 X일

www.acmicpc.net

 

 

 

 


 

 

 

 

  • 풀이

 

N이 K보다 작은 경우 +1, x2, -1작업을 해주고, N이 K보다 큰 경우는 -1의 작업만 해주었습니다.

이미 지나온 길을 가는 경우를 추가해서 불 필요한 작업을 없애 주었습니다.

 

원래는 재귀문으로 해당 코드를 구현했지만 100000 0의 입력이 주어지는 경우 100000번 메소드를 타고 들어가서 stackoverflow가 발생하여 재귀문이 아닌 Queue의 방식으로 구현했습니다. 

 

*참고*

실제로 java에서 재귀가 몇번까지 가능한지 확인해보니 파라미터가 1개인 경우 22812번, 파라미터가 2개경우19047번 가능하였습니다.

 

 

  • 코드

 

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class Main {
	static int answer, N, K;
	static boolean[] check;
	
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		SetData();
		System.out.println(answer);
	}
	
	private static void SetData() throws Exception {
		InputReader in = new InputReader(System.in);
		
		answer = Integer.MAX_VALUE;
		N = in.nextInt();
		K = in.nextInt();
		check = new boolean[100001];
		
		bfs(N, K, 0);
	}
	
	private static void bfs(int a, int b, int count) {
		Queue<Node> queue = new LinkedList<>();
		queue.add(new Node(a, 0));
		
		while(!queue.isEmpty()) {
			Node node = queue.poll();
			check[node.index] = true;
			if(node.index == b)
				answer = Math.min(answer, node.count);
			
			if(node.index < b) {
				if(node.index+1 <= 100000 && !check[node.index+1])
					queue.add(new Node(node.index+1, node.count+1));
				if(node.index*2 <= 100000 && !check[node.index*2])
					queue.add(new Node(node.index*2, node.count));
			}
			if(node.index - 1 >= 0 && !check[node.index-1])
				queue.add(new Node(node.index-1, node.count +1));
		}
	}
}

class Node {
	int index;
	int count;
	
	public Node(int index, int count) {
		this.index = index;
		this.count = count;
	}
}

class InputReader {
	private final InputStream stream;
	private final byte[] buf = new byte[8192];
	private int curChar, snumChars;

	public InputReader(InputStream st) {
		this.stream = st;
	}

	public int read() {
		if (snumChars == -1)
			throw new InputMismatchException();
		if (curChar >= snumChars) {
			curChar = 0;
			try {
				snumChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (snumChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

	public int nextInt() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public long nextLong() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public int[] nextIntArray(int n) {
		int a[] = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		return a;
	}

	public String nextLine() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isEndOfLine(c));
		return res.toString();
	}

	public boolean isSpaceChar(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

	private boolean isEndOfLine(int c) {
		return c == '\n' || c == '\r' || c == -1;
	}
}

+ Recent posts