|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +/** |
| 5 | + * ์๊ณ ๋ฆฌ์ฆ: dfs |
| 6 | + * ์๊ฐ๋ณต์ก๋: 1 โค h โค 30 ํฌ๊ธฐ๋ก bfs ์ฌ์ฉ๊ฐ๋ฅ |
| 7 | + * ์์ด๋์ด: |
| 8 | + * https://github.com/GreatAlgorithm-Study/AlgorithmStudy/issues/23 |
| 9 | + */ |
| 10 | +public class YJ_๋๋ฒ๊น
{ |
| 11 | + static final int MAX_N = 10; |
| 12 | + static final int MAX_H = 30; |
| 13 | + |
| 14 | + static boolean[][] sadari = new boolean[MAX_H+1][MAX_N+1]; |
| 15 | + static List<Connection> possibility = new ArrayList<>(); |
| 16 | + static int MIN = Integer.MAX_VALUE; |
| 17 | + |
| 18 | + public static void main(String[] args) throws IOException { |
| 19 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 20 | + //๊ณ ๊ฐ์ ์ n, ๋ฉ๋ชจ๋ฆฌ ์ ์ค ์ ์ ๊ฐ์ m, ์ทจ์ฝ ์ง์ ์ ๊ฐ์ h |
| 21 | + String[] first = br.readLine().split(" "); |
| 22 | + int n = Integer.parseInt(first[0]); |
| 23 | + int m = Integer.parseInt(first[1]); |
| 24 | + int h = Integer.parseInt(first[2]); |
| 25 | + |
| 26 | + //์ฌ๋ค๋ฆฌ ์ทจ์ฝ์ ํ์ |
| 27 | + for(int i=1; i<=m; i++){ |
| 28 | + String[] next = br.readLine().split(" "); |
| 29 | + int number = Integer.parseInt(next[0]); |
| 30 | + int memory = Integer.parseInt(next[1]); |
| 31 | + sadari[number][memory] = true; |
| 32 | + } |
| 33 | + |
| 34 | + //์ถ๊ฐ ๊ฐ๋ฅํ ์ ์ค์ ์์ฑ |
| 35 | + for(int i=1; i<=h; i++){ |
| 36 | + for(int j=1; j<n; j++){ |
| 37 | + if(!sadari[i][j]){ |
| 38 | + possibility.add(new Connection(i,j)); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + findMinConnection(0,0,h,n); |
| 44 | + //์ฐ๊ฒฐ๊ฐ๋ฅํ ์ ์ค์ ์ด ์์ ๊ฒฝ์ฐ |
| 45 | + if(MIN == Integer.MAX_VALUE){ |
| 46 | + MIN = -1; |
| 47 | + } |
| 48 | + System.out.print(MIN); |
| 49 | + } |
| 50 | + |
| 51 | + static class Connection { |
| 52 | + int a; |
| 53 | + int b; |
| 54 | + |
| 55 | + public Connection(int a, int b){ |
| 56 | + this.a = a; |
| 57 | + this.b = b; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + //current(=i):์ฐ๊ฒฐ๊ฐ๋ฅํ ์ ์ค์ ๋งํผ ์ํ ,count:์ถ๊ฐํ ์ ์ค์ ๊ฐฏ์ |
| 62 | + static void findMinConnection(int current, int count, int h, int n){ |
| 63 | + //์ถ๊ฐํ ์ ์ค์ ์ ์๊ฐ ์ง๊ธ๊น์ง ๊ตฌํ ๋ต๋ณด๋ค ์ข์์ง ์ ์๋ค๋ฉด?? โ
๋ ์ข์์ง๋ค๋ ๊ธฐ์ค์ด ๋ญ์ง? |
| 64 | + if(count >= MIN){ |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + //์ต์ด ๋๋ ์ฌ๊ทํธ์ถ๋ก ์ฐ๊ฒฐ๋ ์ ์ค์ ์ด ์ ๋๋ก ์ฌ๋ค๋ฆฌ๋ฅผ ํ๋์ง ํ์ธ |
| 69 | + if(isConnected(h,n)){ |
| 70 | + MIN = Math.min(MIN,count); |
| 71 | + } |
| 72 | + |
| 73 | + //๋ฌธ์ ์๊ตฌ์ฌํญ์ธ ์ ์ค์ ์ ๊ฐฏ์ ์ต๋ 3 |
| 74 | + //ํ์ฌ ์ํํ๋ ์ธ๋ฑ์ค๊ฐ ์ฐ๊ฒฐ ๊ฐ๋ฅํ ์ ์ค์ ์ ๋ชจ๋ ์ํํ๋ฉด ๋ ์ด์ ์ ์ค์ ์ด ์์ |
| 75 | + if(count == 3 || current == possibility.size()){ |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + //์ฐ๊ฒฐ ๊ฐ๋ฅํ ๋ชจ๋ ์ ์ค์ ์ ์ฌ๊ทํธ์ถ > โ
์ด๊ฑธ ์ ํ์ง? |
| 80 | + findMinConnection(current+1, count, h,n); |
| 81 | + //current == possibility.size()๋ก ์ธํด return ๋์ด ์ฌ๊ทํธ์ถ๋ ์คํ์ ํ๋์ฉ ๋ฐํ |
| 82 | + |
| 83 | + //์ฐ๊ฒฐ ๊ฐ๋ฅํ ์ ์ค์ ๊ฐ์ ธ์ค๊ธฐ(๋ง์ง๋ง๋ถํฐ ๊ฐ์ ธ์ด) |
| 84 | + int a = possibility.get(current).a; |
| 85 | + int b = possibility.get(current).b; |
| 86 | + //์ ์ค์ ์ฐ๊ฒฐ์ฒ๋ฆฌ |
| 87 | + sadari[a][b] = true; |
| 88 | + //์ฐ๊ฒฐํ ์ ์ค์ ์ด ์กฐ๊ฑด์ ๋ง์กฑํ๋์ง ์ฌ๊ทํธ์ถ |
| 89 | + findMinConnection(current+1, count+1, h,n); |
| 90 | + sadari[a][b] = false; |
| 91 | + } |
| 92 | + |
| 93 | + //๋ฒํธ๋ณ ์ฌ๋ค๋ฆฌ ์ค |
| 94 | + static int[] nums = new int[MAX_N+1]; |
| 95 | + |
| 96 | + //๋ชจ๋ ์ฌ๋ค๋ฆฌ ์ค์ ์ํํ๋ฉฐ i๋ฒ-i๊ณ ๊ฐ ์ฐ๊ฒฐ๋์ด์๋์ง ํ์ธ |
| 97 | + private static boolean isConnected (int h, int n){ |
| 98 | + //์ ์ค์ ์ด ์ฐ๊ฒฐ๋์ด ์์ผ๋ฉด ๋ฒ๊ทธ์ด๊ธฐ ๋๋ฌธ์ ๋ถ๊ฐ๋ฅ |
| 99 | + for(int a=1; a<=h; a++){ |
| 100 | + for(int b=2; b<n; b++){ //b๊ฐ 2๋ถํฐ ์์ํ๋ ์ด์ ? b=1๋ก ํ๋ฉด n+1 ๋ถํ์ํ ๋ฒ์๊น์ง ํ์ธํ๊ธฐ ๋๋ฌธ |
| 101 | + if(sadari[a][b] && sadari[a][b-1]){ |
| 102 | + return false; |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + //์ฌ๋ค๋ฆฌ ๋ฒํธ ์ง์ |
| 108 | + for(int i=1; i<= n; i++){ |
| 109 | + nums[i] = i; |
| 110 | + } |
| 111 | + |
| 112 | + //์ฌ๋ค๋ฆฌ ํ๋ ๋ฐฉ๋ฒ: ์ฐ๊ฒฐ์ง์ ๋ผ๋ฆฌ ๋ฒํธ๋ฅผ ์๋ก ๊ตํ |
| 113 | + for(int a=1; a<=h; a++) { |
| 114 | + for (int b = 1; b < n; b++) { |
| 115 | + if(sadari[a][b]){ |
| 116 | + int exchange = nums[b]; |
| 117 | + nums[b] = nums[b+1]; |
| 118 | + nums[b+1] = exchange; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + //์ฌ๋ค๋ฆฌํ๊ธฐ ์ข
๋ฃ ๊ฒ์ฆ: i๋ฒ-i๊ณ ๊ฐ ์ผ์น |
| 124 | + for(int i=1; i<=n; i++){ |
| 125 | + if(nums[i] != i){ |
| 126 | + return false; |
| 127 | + } |
| 128 | + } |
| 129 | + return true; |
| 130 | + } |
| 131 | +} |
0 commit comments