알고리즘 풀이/백준

[백준][Java] 9205번 맥주 마시면서 걸어가기

배게 2018. 5. 19. 01:45
728x90

좌표들을 배열들을 통해 저장해놓고


move함수를 이용하여 움직여줌


한번 움직일 때마다 manHattan함수를 이용하여


축제 좌표까지 도달이 가능한지 체크함


가능한 경우 arrival 변수를 true로 저장하여


마지막에 출력


(여기서 arrival이 true, 즉 목적지에 도달할 수 있는


경우일 때에 나머지 for문을 안 돌리고 빠져나가는 방법은


모르겠어서 그냥 전부 출력)




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.util.Scanner;

public class Main {
	static int[] fXY;
	static boolean[] visited;
	static int[][] convXY;
	static boolean arrival;
	
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		int T = sc.nextInt();
		
		for(int i=0; i<T; i++) {
			int n = sc.nextInt(); // 편의점의 개수
			
			int[] hXY = new int[2];
			convXY = new int[n][2];
			fXY = new int [2];
			visited = new boolean[n];
			
			hXY[0] = sc.nextInt();
			hXY[1] = sc.nextInt();
			
			for(int j=0; j<n; j++) {
				convXY[j][0] = sc.nextInt();
				convXY[j][1] = sc.nextInt();
			}
			
			fXY[0] = sc.nextInt();
			fXY[1] = sc.nextInt();
			
			move(hXY);
			if(arrival) System.out.println("happy");
			else System.out.println("sad");
			
			arrival = false;
			
			/*int[] a={2000,1000};
			int[] b={2000,2000};
			if(manHattan(a,b)) {
				System.out.println("z");
			}*/
		}
		
	}
	
	public static void move (int[] startXY) {
//		System.out.println("move "+ startXY[0]+" "+startXY[1]);
		
		if(manHattan(startXY, fXY)) {
			arrival=true;
			return;
		}
		
		
		for(int i=0; i<convXY.length; i++) {
			if(manHattan(startXY, convXY[i]) && !visited[i]) {
//				System.out.println(i);
				visited[i]=true;
				move(convXY[i]);
			}
		}
		
		
		/*if(manHattan(startXY, fXY)) arrival=true;
			
		for(int i=0; i<convXY.length; i++) {
			if(visited[i]) return;
			else visited[i]=true;
			if(manHattan(startXY, convXY[i])) {
				move(convXY[i]);
			}
		}*/
		
	}
	
	public static boolean manHattan(int[]a, int[]b) {
		if(Math.abs(a[0]-b[0])+Math.abs(a[1]-b[1]) <=1000) return true;
		else return false;
	}
	
}