프로그래머스 level2 타겟넘버(BFS)

하우르·2021년 3월 19일
0
post-custom-banner
import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;

public class Main {
	static final int MAX = 100000;
	static class Number {
		int n, distance;

		public Number(int n, int distance) {
			this.n = n;
			this.distance = distance;
		}
	}
	public static int BFS(int[] numbers, int target) {
		Queue<Number> will_visit = new LinkedList<Number>();
		int num=-1;
		int count=0;
		will_visit.add(new Number(0, 0));
		while (true) {
			if(will_visit.isEmpty())
				return count;
			Number current_point = will_visit.remove();
			if (current_point.n == target && current_point.distance == numbers.length)
				count++;

			if(current_point.distance != numbers.length)
			{
				will_visit.add(new Number (current_point.n+numbers[current_point.distance], current_point.distance+1));
				will_visit.add(new Number(current_point.n-numbers[current_point.distance], current_point.distance+1));
			}

		}
	}
	public static void main(String[] args) throws NumberFormatException, IOException {
		int[] numbers = {1, 1, 1, 1, 1};
		int target = 3;
		int count = BFS(numbers,target);
		System.out.print("count"+count);

	}
profile
주니어 개발자
post-custom-banner

0개의 댓글