diff --git "a/common/boj/wk1-1_\353\205\271\354\203\211 \354\230\267 \354\236\205\354\235\200 \354\225\240\352\260\200 \354\240\244\353\213\244\354\247\200_BOJ4485/\353\205\271\354\203\211 \354\230\267 \354\236\205\354\235\200 \354\225\240\352\260\200 \354\240\244\353\213\244\354\247\200_\352\260\225\355\232\250\354\247\204.java" "b/common/boj/wk1-1_\353\205\271\354\203\211 \354\230\267 \354\236\205\354\235\200 \354\225\240\352\260\200 \354\240\244\353\213\244\354\247\200_BOJ4485/Main1.java" similarity index 99% rename from "common/boj/wk1-1_\353\205\271\354\203\211 \354\230\267 \354\236\205\354\235\200 \354\225\240\352\260\200 \354\240\244\353\213\244\354\247\200_BOJ4485/\353\205\271\354\203\211 \354\230\267 \354\236\205\354\235\200 \354\225\240\352\260\200 \354\240\244\353\213\244\354\247\200_\352\260\225\355\232\250\354\247\204.java" rename to "common/boj/wk1-1_\353\205\271\354\203\211 \354\230\267 \354\236\205\354\235\200 \354\225\240\352\260\200 \354\240\244\353\213\244\354\247\200_BOJ4485/Main1.java" index b097283..52cdfde 100644 --- "a/common/boj/wk1-1_\353\205\271\354\203\211 \354\230\267 \354\236\205\354\235\200 \354\225\240\352\260\200 \354\240\244\353\213\244\354\247\200_BOJ4485/\353\205\271\354\203\211 \354\230\267 \354\236\205\354\235\200 \354\225\240\352\260\200 \354\240\244\353\213\244\354\247\200_\352\260\225\355\232\250\354\247\204.java" +++ "b/common/boj/wk1-1_\353\205\271\354\203\211 \354\230\267 \354\236\205\354\235\200 \354\225\240\352\260\200 \354\240\244\353\213\244\354\247\200_BOJ4485/Main1.java" @@ -4,7 +4,7 @@ import java.io.OutputStreamWriter; import java.util.PriorityQueue; -public class Main { +public class Main1 { static int N; static int[][] map; diff --git "a/common/boj/wk1-1_\353\205\271\354\203\211 \354\230\267 \354\236\205\354\235\200 \354\225\240\352\260\200 \354\240\244\353\213\244\354\247\200_BOJ4485/\353\205\271\354\203\211 \354\230\267 \354\236\205\354\235\200 \354\225\240\352\260\200 \354\240\244\353\213\244\354\247\200_\354\225\210\355\203\234\354\234\244.java" "b/common/boj/wk1-1_\353\205\271\354\203\211 \354\230\267 \354\236\205\354\235\200 \354\225\240\352\260\200 \354\240\244\353\213\244\354\247\200_BOJ4485/Main2.java" similarity index 99% rename from "common/boj/wk1-1_\353\205\271\354\203\211 \354\230\267 \354\236\205\354\235\200 \354\225\240\352\260\200 \354\240\244\353\213\244\354\247\200_BOJ4485/\353\205\271\354\203\211 \354\230\267 \354\236\205\354\235\200 \354\225\240\352\260\200 \354\240\244\353\213\244\354\247\200_\354\225\210\355\203\234\354\234\244.java" rename to "common/boj/wk1-1_\353\205\271\354\203\211 \354\230\267 \354\236\205\354\235\200 \354\225\240\352\260\200 \354\240\244\353\213\244\354\247\200_BOJ4485/Main2.java" index 1ef9367..cd4fe7e 100644 --- "a/common/boj/wk1-1_\353\205\271\354\203\211 \354\230\267 \354\236\205\354\235\200 \354\225\240\352\260\200 \354\240\244\353\213\244\354\247\200_BOJ4485/\353\205\271\354\203\211 \354\230\267 \354\236\205\354\235\200 \354\225\240\352\260\200 \354\240\244\353\213\244\354\247\200_\354\225\210\355\203\234\354\234\244.java" +++ "b/common/boj/wk1-1_\353\205\271\354\203\211 \354\230\267 \354\236\205\354\235\200 \354\225\240\352\260\200 \354\240\244\353\213\244\354\247\200_BOJ4485/Main2.java" @@ -5,7 +5,7 @@ import java.util.PriorityQueue; import java.util.Queue; -public class Main { +public class Main2 { static int N, min; static int[][] map; diff --git "a/common/boj/wk1-1_\353\205\271\354\203\211 \354\230\267 \354\236\205\354\235\200 \354\225\240\352\260\200 \354\240\244\353\213\244\354\247\200_BOJ4485/\353\205\271\354\203\211 \354\230\267 \354\236\205\354\235\200 \354\225\240\352\260\200 \354\240\244\353\213\244\354\247\200_\354\240\225\354\204\270\352\266\214.java" "b/common/boj/wk1-1_\353\205\271\354\203\211 \354\230\267 \354\236\205\354\235\200 \354\225\240\352\260\200 \354\240\244\353\213\244\354\247\200_BOJ4485/\353\205\271\354\203\211\354\230\267\354\236\205\354\235\200\354\225\240\352\260\200\354\240\244\353\213\244\354\247\200_\354\240\225\354\204\270\352\266\214.java" similarity index 100% rename from "common/boj/wk1-1_\353\205\271\354\203\211 \354\230\267 \354\236\205\354\235\200 \354\225\240\352\260\200 \354\240\244\353\213\244\354\247\200_BOJ4485/\353\205\271\354\203\211 \354\230\267 \354\236\205\354\235\200 \354\225\240\352\260\200 \354\240\244\353\213\244\354\247\200_\354\240\225\354\204\270\352\266\214.java" rename to "common/boj/wk1-1_\353\205\271\354\203\211 \354\230\267 \354\236\205\354\235\200 \354\225\240\352\260\200 \354\240\244\353\213\244\354\247\200_BOJ4485/\353\205\271\354\203\211\354\230\267\354\236\205\354\235\200\354\225\240\352\260\200\354\240\244\353\213\244\354\247\200_\354\240\225\354\204\270\352\266\214.java" diff --git "a/common/boj/wk2-1 \354\273\250\353\262\240\354\235\264\354\226\264 \353\262\250\355\212\270 \354\234\204\354\235\230 \353\241\234\353\264\207_BOJ20055/Main20055.java" "b/common/boj/wk2-1 \354\273\250\353\262\240\354\235\264\354\226\264 \353\262\250\355\212\270 \354\234\204\354\235\230 \353\241\234\353\264\207_BOJ20055/Main20055.java" new file mode 100644 index 0000000..02e4de9 --- /dev/null +++ "b/common/boj/wk2-1 \354\273\250\353\262\240\354\235\264\354\226\264 \353\262\250\355\212\270 \354\234\204\354\235\230 \353\241\234\353\264\207_BOJ20055/Main20055.java" @@ -0,0 +1,90 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.StringTokenizer; + +public class Main20055 { + static int head,tail,zeroCnt,step,N,M; + // head 올리는 위치 + // tail 내리는 위치 + // zeroCnt 내구도 0 개수 + // step 현재 단계 + static boolean[] robots; + static int[] hp; + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()," ");//N,K입력 + N = Integer.parseInt(st.nextToken()); // 2 <= N <=100 + tail = N-1;// tail 초기값 = N-1 + M=N*2;//어차피 2N으로 쓰기 떄문에 + + int K = Integer.parseInt(st.nextToken()); + + hp = new int[M];//내구도 저장할 배열 + robots = new boolean[M]; // 해당 위치 로봇 여부 체크 배열 + + st = new StringTokenizer(br.readLine()," "); + + for (int i = 0; i =K){ + break; + } + } + System.out.println(step); + + } + public static int filter(int num){ + if(num<0) return M+num; + if(num>=M) return num%M; + return num; + } + public static void movebelt(){ + head = filter(--head); + tail = filter(--tail); + } + public static void robotOff(){ + robots[tail] = false; + } + public static void robotMove(){ + int start = filter(tail-1); //검사 시작칸 맨 끝에선 이미 내렸으니깐. tail-1부터 시작 + int scope = N-1;//검사할 칸 수, 맨 끝칸 제외니깐 N-1; + for (int i = 0; i < scope; i++) { + int pos = filter(start-i); + int next =filter(pos + 1); + //로봇이 있고, 다음칸에 로봇이 없고 내구도가 0이 아니라면 + if (robots[pos] && !robots[next] && hp[next] > 0) { + //현재 위치 비우고 다음칸으로 이동, + robots[pos] = false; + if (next != tail)//다음칸이 tail이라면 로봇 바로 내리기 떄문에 패스 + robots[next] = true; + // 내구도-1, 0됐으면 zeroCnt +1 + if (--hp[next] == 0)//내구도 0되면 카운트 +1 + { + System.out.println("hihih"); + zeroCnt++; + } + } + ; + } + } + public static void robotOn(){ + if(hp[head]>0) { + if(--hp[head]==0){ + System.out.println("wwwww"); + zeroCnt++; + }else{ + robots[head] = true; + } + }; + } +} diff --git "a/common/boj/wk2-1 \354\273\250\353\262\240\354\235\264\354\226\264 \353\262\250\355\212\270 \354\234\204\354\235\230 \353\241\234\353\264\207_BOJ20055/wk2-1 \354\273\250\353\262\240\354\235\264\354\226\264 \353\262\250\355\212\270 \354\234\204\354\235\230 \353\241\234\353\264\207_\354\225\210\355\203\234\354\234\244.java" "b/common/boj/wk2-1 \354\273\250\353\262\240\354\235\264\354\226\264 \353\262\250\355\212\270 \354\234\204\354\235\230 \353\241\234\353\264\207_BOJ20055/wk2-1 \354\273\250\353\262\240\354\235\264\354\226\264 \353\262\250\355\212\270 \354\234\204\354\235\230 \353\241\234\353\264\207_\354\225\210\355\203\234\354\234\244.java" new file mode 100644 index 0000000..0237757 --- /dev/null +++ "b/common/boj/wk2-1 \354\273\250\353\262\240\354\235\264\354\226\264 \353\262\250\355\212\270 \354\234\204\354\235\230 \353\241\234\353\264\207_BOJ20055/wk2-1 \354\273\250\353\262\240\354\235\264\354\226\264 \353\262\250\355\212\270 \354\234\204\354\235\230 \353\241\234\353\264\207_\354\225\210\355\203\234\354\234\244.java" @@ -0,0 +1,86 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.StringTokenizer; + +public class Main { + static int head,tail,zeroCnt,step,N,M; + // head 올리는 위치 + // tail 내리는 위치 + // zeroCnt 내구도 0 개수 + // step 현재 단계 + static boolean[] robots; + static int[] hp; + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()," ");//N,K입력 + N = Integer.parseInt(st.nextToken()); // 2 <= N <=100 + tail = N-1;// tail 초기값 = N-1 + M=N*2;//어차피 2N으로 쓰기 떄문에 + + int K = Integer.parseInt(st.nextToken()); + + hp = new int[M];//내구도 저장할 배열 + robots = new boolean[M]; // 해당 위치 로봇 여부 체크 배열 + + st = new StringTokenizer(br.readLine()," "); + + for (int i = 0; i =K){ + break; + } + } + System.out.println(step); + + } + public static int filter(int num){ + if(num<0) return M+num; + if(num>=M) return num%M; + return num; + } + public static void movebelt(){ + head = filter(--head); + tail = filter(--tail); + } + public static void robotOff(){ + robots[tail] = false; + } + public static void robotMove(){ + int start = filter(tail-1); //검사 시작칸 맨 끝에선 이미 내렸으니깐. tail-1부터 시작 + int scope = N-1;//검사할 칸 수, 맨 끝칸 제외니깐 N-1; + for (int i = 0; i < scope; i++) { + int pos = filter(start-i); + int next =filter(pos + 1); + //로봇이 있고, 다음칸에 로봇이 없고 내구도가 0이 아니라면 + if (robots[pos] && !robots[next] && hp[next] > 0) { + //현재 위치 비우고 다음칸으로 이동, + robots[pos] = false; + if (next != tail)//다음칸이 tail이라면 로봇 바로 내리기 떄문에 패스 + robots[next] = true; + // 내구도-1, 0됐으면 zeroCnt +1 + if (--hp[next] == 0)//내구도 0되면 카운트 +1 + { + zeroCnt++; + } + }; + } + } + public static void robotOn(){ + if(hp[head]>0) { + if(--hp[head]==0){ + zeroCnt++; + } + robots[head] = true; + }; + } +} diff --git "a/common/boj/wk2-2 \352\263\240\354\270\265\352\261\264\353\254\274_BOJ1207/Main1027.java" "b/common/boj/wk2-2 \352\263\240\354\270\265\352\261\264\353\254\274_BOJ1207/Main1027.java" new file mode 100644 index 0000000..78095e3 --- /dev/null +++ "b/common/boj/wk2-2 \352\263\240\354\270\265\352\261\264\353\254\274_BOJ1207/Main1027.java" @@ -0,0 +1,55 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +public class Main1027 { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int N = Integer.parseInt(br.readLine()); //1 N =50 + int[] heights = new int[N]; //높이를 저장하는 1차원 배열 + StringTokenizer st = new StringTokenizer(br.readLine(), ); //높이 입력 + + for (int i = 0; i N; i++) { + heights[i] = Integer.parseInt(st.nextToken()); + } + + boolean[][] matrix = new boolean[N][N]; //서로 볼 수 있는지 기록하는 2차원 배열 0 자기자신 볼수 있으면 1, 없으면 -1 + int maxCnt = 0; + //i=0 인 것부터 + for (int i = 0; i< N; i++) { + int tempCnt = 0; + int h = heights[i]; //높이 + //자신의 오른쪽 건물 순차적으로 선정 + for (int j = i + 1; j < N; j++) { + int jh = heights[j]; //오른쪽 건물의 높이 + double a = getSlope(i,h,j,jh);//기울기 계산 + boolean canSee = true; //볼 수 있는지 체크하는 변수 + //두 건물 사이에 있는 건물 순회 + for (int k = i+1; k < j ; k++) { + int kh = heights[k]; + double m = getSlope(i,h,k,kh);//기울기 계산 + if(m >= a ){ //두 건물 사이에 기울기가 더 크거나 같은 건물이 있으면 false, break + canSee = false; + break; + } + } + if(canSee) {//볼 수 있으면 tempCnt 증가, matrix 표시 + tempCnt++; + matrix[i][j] = true; + }; + } + //자신의 왼쪽 건물과의 결과 중 1 인 것만큼 증가 + for (int j = i-1; j >= 0 ; j--) { + if(matrix[j][i]) tempCnt++; + } + maxCnt = tempCnt > maxCnt ? tempCnt : maxCnt; //이전까지 최대 개수보다 크면 갱신 + } + System.out.println(maxCnt); + } + //두점을 잇는 선분의 기울기 구하는 함수 + public static double getSlope(int x1,int y1, int x2, int y2){ + return (y1-y2) / (double) (x1-x2); + } + +} diff --git "a/common/boj/wk2-2 \352\263\240\354\270\265\352\261\264\353\254\274_BOJ1207/\352\263\240\354\270\265\352\261\264\353\254\274_\354\225\210\355\203\234\354\234\244.java" "b/common/boj/wk2-2 \352\263\240\354\270\265\352\261\264\353\254\274_BOJ1207/\352\263\240\354\270\265\352\261\264\353\254\274_\354\225\210\355\203\234\354\234\244.java" new file mode 100644 index 0000000..30ac61c --- /dev/null +++ "b/common/boj/wk2-2 \352\263\240\354\270\265\352\261\264\353\254\274_BOJ1207/\352\263\240\354\270\265\352\261\264\353\254\274_\354\225\210\355\203\234\354\234\244.java" @@ -0,0 +1,45 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +public class Main{ + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int N = Integer.parseInt(br.readLine()); //1 N =50 + int[] heights = new int[N]; //높이를 저장하는 1차원 배열 + int[] numberInSites = new int[N]; // 해당인덱스에서 볼 수 있는 건물의 수를 저장하는 배열 + StringTokenizer st = new StringTokenizer(br.readLine(), " "); //높이 입력 + + for (int i = 0; i < N; i++) { + heights[i] = Integer.parseInt(st.nextToken()); + } + + int maxCnt = 0; + //i=0 인 것부터 + for (int i = 0; i< N; i++) { + double maxSlope = -Integer.MIN_VALUE; // 최대 기울값 저장 위한 변수 + int h = heights[i]; //높이 + + //자신의 오른쪽 건물 순차적으로 선정 + for (int j = i + 1; j < N; j++) { + int jh = heights[j]; //오른쪽 건물의 높이 + double a = getSlope(i,h,j,jh);//기울기 계산 + //기울기가 최대값보다 크면 최대값 갱신 및 현재 카운트 증가, + if(maxSlope < a){ + maxSlope = a; + numberInSites[i]++; + numberInSites[j]++; + } + + } + maxCnt = numberInSites[i] > maxCnt ? numberInSites[i] : maxCnt; //이전까지 최대 개수보다 크면 갱신 + } + System.out.println(maxCnt); + } + //두점을 잇는 선분의 기울기 구하는 함수 + public static double getSlope(int x1,int y1, int x2, int y2){ + return (y1-y2) / (double) (x1-x2); + } + +} diff --git "a/common/boj/wk3_1_\354\242\213\353\213\244_BOJ1253/Main.java" "b/common/boj/wk3_1_\354\242\213\353\213\244_BOJ1253/Main.java" new file mode 100644 index 0000000..80c8db7 --- /dev/null +++ "b/common/boj/wk3_1_\354\242\213\353\213\244_BOJ1253/Main.java" @@ -0,0 +1,38 @@ +package wk3_1_좋다_BOJ1253; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.StringTokenizer; + +public class Main { + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int N = Integer.parseInt(br.readLine()); // 1<= N <= 2000 + int[] numbers = new int[N];// N개의 숫자를 저장할 배열 + + StringTokenizer st = new StringTokenizer(br.readLine()," "); + HashMap numberCnt = new HashMap<>();//N개의 숫자중 값이 같은 수의 개수를 저장하는 키-밸류 + HashMap visited = new HashMap<>();//이미 한 번 처리한 숫자 값을 저장할 키-밸류 + + for (int i = 0; i < N; i++) { + numbers[i] = Integer.parseInt(st.nextToken()); // +- 1_000_000_000, 숫자 입력 + Integer cur = numberCnt.get(numbers[i]); // 앞에 같은 값을 가지는 수가 몇개 나왔는지 검사 + cur = cur == null ? 0 : cur; + numberCnt.put(numbers[i],cur+1);// 해당하는 값의 숫자++ + } + + int goodCnt = 0; + for (int i = 0; i < N; i++) { + for (int j = i+1; j < N; j++) { + int sum = numbers[i]+numbers[j]; + Integer cnt = numberCnt.get(sum); + if(cnt != null && visited.get(sum) == null) { + goodCnt += cnt; + visited.put(sum, true); + } + } + } + System.out.println(goodCnt); + } +} diff --git "a/common/boj/wk3_2_\354\225\214\355\214\214\353\262\263_BOJ1987/Main.java" "b/common/boj/wk3_2_\354\225\214\355\214\214\353\262\263_BOJ1987/Main.java" new file mode 100644 index 0000000..00fd019 --- /dev/null +++ "b/common/boj/wk3_2_\354\225\214\355\214\214\353\262\263_BOJ1987/Main.java" @@ -0,0 +1,61 @@ +package wk3_2_알파벳_BOJ1253; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Set; +import java.util.StringTokenizer; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +public class Main{ + static int[] dr = { -1, 1, 0, 0 };// 사방탐색 dr + static int[] dc = { 0, 0, -1, 1 };// 사방탐색 dc + static int[][] matrix;//대문자 저장 + static int R,C;//현재 위치 + static int max = 0;//최대 깊이 + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + R = Integer.parseInt(st.nextToken()); + C = Integer.parseInt(st.nextToken()); + + matrix = new int[R][C]; + + for (int i = 0; i < R; i++) { + String str = br.readLine(); + for (int j = 0; j < C; j++) { + matrix[i][j] = str.charAt(j) - 'A'; // 'A'부터 시작이므로 int 로저장 + } + } + + dfs(1, 0, 0,1 << matrix[0][0]);// 비트마스킹 방문처리 dfs 시작 + + System.out.println(max); + } + + static void dfs(int cnt, int r, int c, int visited) { + if (max == 26) return; // 대문자 개수 26개 이므로 26이 최대값 + max = cnt > max ? cnt : max; // + for (int j = 0; j < 4; j++) { // 4방탐색 + int nr = r + dr[j]; + int nc = c + dc[j]; + + if (nr >= 0 && nr < R && nc >= 0 && nc < C) { // 범위 체크 + int nChar = matrix[nr][nc]; // 다음 대문자 + if ((visited & (1 << nChar)) == 0) { // 다음 대문자 방문 확인 + //연산 결과를 전달하기 때문에 방문체크를 해제할 필요가 없다. + dfs(cnt + 1, nr, nc, visited | 1 << nChar); + } + } + } + } +} + diff --git "a/common/boj/wk3_2_\354\225\214\355\214\214\353\262\263_BOJ1987/Main_1.java" "b/common/boj/wk3_2_\354\225\214\355\214\214\353\262\263_BOJ1987/Main_1.java" new file mode 100644 index 0000000..129ff66 --- /dev/null +++ "b/common/boj/wk3_2_\354\225\214\355\214\214\353\262\263_BOJ1987/Main_1.java" @@ -0,0 +1,73 @@ +package wk3_2_알파벳_BOJ1253; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Set; +import java.util.StringTokenizer; +//메모리 초과 +public class Main_1 { + static int[] dr = {0,1,0,-1}; //4방 탐색위한 dr + static int[] dc = {1,0,-1,0}; //4방 탐색위한 dc + static char NULL = '\u0000'; // char 배열선언시 초기값 + + static class State{ // BFS에서 현재 탐색중인 상태를 저장하기 위한 클래스 + int r,c; // 좌표 + Set way = new HashSet<>(); // 현재 위치 포함 지금까지 밟은 Character 저장 + State(int r,int c,Set way){ + this.r=r; + this.c=c; + this.way=way; + } + } + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + int R = Integer.parseInt(st.nextToken()); // 1<= R <=20 + int C = Integer.parseInt(st.nextToken()); // 1<= C <=20 + + char[][] map = new char[R+2][C+2]; // 문자 저장할 map 2차원 배열 선언, 범위 검사 하기 싫어서 padding(+2) + for (int r = 1; r <= R; r++) {// r=1 에서부터 시작 + String str = br.readLine();//한 줄 입력 + for (int c = 1; c <= C; c++) {//c=1에서부터 시작 + map[r][c] = str.charAt(c-1);// 저장 + } + } + Queue q = new LinkedList<>(); + + Set startSet = new HashSet<>(); + startSet.add(map[1][1]); + State start = new State(1,1,startSet);// 좌측 상단 1,1 에서 시작 + + q.add(start); + + int dist= 0; + //bfs + while(!q.isEmpty()) { + dist++; // 거리 카운트 + for (int r = 0; r < q.size(); r++) {// 현재 들어가 있는 큐는 같은 거리에 있음 + State current = q.poll();// 현재 밟은 자리 + + for (int i = 0; i < 4; i++) {// 현재 밟은 자리로부터 4방 탐색 + int nr = current.r + dr[i]; + int nc = current.c + dc[i]; + char nChar = map[nr][nc];// 다음 밟을 자리의 대문자 + if(nChar != NULL && !current.way.contains(nChar)) {//어차피 입력은 전부 대문자라서 NULL 검사 할 필요 없음, 현재까지 밟은 거랑 같은지 검사 + //처음 밟는 대문자일 때 + Set nWay = new HashSet<>(current.way);//현재까지 밟은 기록 복사(참조 X) + nWay.add(nChar);//처음 밟는 문자 추가 + q.add(new State(nr,nc,nWay));// 다음 큐에 저장 + } + } + } + } + + System.out.println(dist);//출력 + + + } +} diff --git "a/common/boj/wk3_2_\354\225\214\355\214\214\353\262\263_BOJ1987/Main_2.java" "b/common/boj/wk3_2_\354\225\214\355\214\214\353\262\263_BOJ1987/Main_2.java" new file mode 100644 index 0000000..6aa1ee3 --- /dev/null +++ "b/common/boj/wk3_2_\354\225\214\355\214\214\353\262\263_BOJ1987/Main_2.java" @@ -0,0 +1,75 @@ +package wk3_2_알파벳_BOJ1253; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Queue; +import java.util.Set; +import java.util.StringTokenizer; + +public class Main_2 { + static int[] dr = {0,1,0,-1}; //4방 탐색위한 dr + static int[] dc = {1,0,-1,0}; //4방 탐색위한 dc + static char NULL = '\u0000'; // char 배열선언시 초기값 + + static class State{ // BFS에서 현재 탐색중인 상태를 저장하기 위한 클래스 + int r,c; + boolean[] way = new boolean[26]; // 현재 위치 포함 지금까지 밟은 Character 저장 + State(int r,int c,boolean[] way){ + this.r=r; + this.c=c; + this.way=way; + } + } + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + int R = Integer.parseInt(st.nextToken()); // 1<= R <=20 + int C = Integer.parseInt(st.nextToken()); // 1<= C <=20 + + char[][] map = new char[R+2][C+2]; // 문자 저장할 map 2차원 배열 선언, 범위 검사 하기 싫어서 padding(+2) + for (int r = 1; r <= R; r++) {// r=1 에서부터 시작 + String str = br.readLine();//한 줄 입력 + for (int c = 1; c <= C; c++) {//c=1에서부터 시작 + map[r][c] = str.charAt(c-1);// 저장 + } + } + + Queue q = new LinkedList<>(); + + boolean[] startWay = new boolean[26]; + startWay[map[1][1]-'A'] = true; + State start = new State(1,1,startWay);// 좌측 상단 1,1 에서 시작 + + q.add(start); + + int dist= 0; + //bfs + while(!q.isEmpty()) { + dist++; // 거리 카운트 + for (int r = 0; r < q.size(); r++) {// 현재 들어가 있는 큐는 같은 거리에 있음 + State current = q.poll();// 현재 밟은 자리 + + for (int i = 0; i < 4; i++) {// 현재 밟은 자리로부터 4방 탐색 + int nr = current.r + dr[i]; + int nc = current.c + dc[i]; + char nChar = map[nr][nc];// 다음 밟을 자리의 대문자 + if(nChar != NULL && !current.way[nChar-'A']) {//어차피 입력은 전부 대문자라서 NULL 검사 할 필요 없음, 현재까지 밟은 거랑 같은지 검사 + //처음 밟는 대문자일 때 + boolean[] nWay = Arrays.copyOf(current.way, 26);//현재까지 밟은 기록 복사(참조 X) + nWay[nChar-'A'] = true; //현재 위치 대문자 밟음 처리 + q.add(new State(nr,nc,nWay));// 다음 큐에 저장 + } + } + } + } + + System.out.println(dist);//출력 + + + } +}