diff --git a/.classpath b/.classpath index 1938c4d..5587881 100644 --- a/.classpath +++ b/.classpath @@ -1,6 +1,6 @@ - + diff --git a/build/classes/G_1068.class b/build/classes/G_1068.class new file mode 100644 index 0000000..8dad1f2 Binary files /dev/null and b/build/classes/G_1068.class differ diff --git a/build/classes/G_1238$1.class b/build/classes/G_1238$1.class new file mode 100644 index 0000000..1398045 Binary files /dev/null and b/build/classes/G_1238$1.class differ diff --git a/build/classes/G_1238.class b/build/classes/G_1238.class new file mode 100644 index 0000000..f63d7e8 Binary files /dev/null and b/build/classes/G_1238.class differ diff --git a/build/classes/G_14502.class b/build/classes/G_14502.class new file mode 100644 index 0000000..a5185d6 Binary files /dev/null and b/build/classes/G_14502.class differ diff --git a/build/classes/G_16947.class b/build/classes/G_16947.class new file mode 100644 index 0000000..b3f6c98 Binary files /dev/null and b/build/classes/G_16947.class differ diff --git a/build/classes/G_18111.class b/build/classes/G_18111.class new file mode 100644 index 0000000..96be4cf Binary files /dev/null and b/build/classes/G_18111.class differ diff --git a/build/classes/G_2206$PointN.class b/build/classes/G_2206$PointN.class new file mode 100644 index 0000000..1a214f0 Binary files /dev/null and b/build/classes/G_2206$PointN.class differ diff --git a/build/classes/G_2206.class b/build/classes/G_2206.class new file mode 100644 index 0000000..82a1fa9 Binary files /dev/null and b/build/classes/G_2206.class differ diff --git a/build/classes/G_7576.class b/build/classes/G_7576.class new file mode 100644 index 0000000..86acda1 Binary files /dev/null and b/build/classes/G_7576.class differ diff --git a/build/classes/Programmers/P2_43165.class b/build/classes/Programmers/P2_43165.class new file mode 100644 index 0000000..5dbf94a Binary files /dev/null and b/build/classes/Programmers/P2_43165.class differ diff --git a/build/classes/Programmers/P3_43164.class b/build/classes/Programmers/P3_43164.class new file mode 100644 index 0000000..caa648c Binary files /dev/null and b/build/classes/Programmers/P3_43164.class differ diff --git a/build/classes/Programmers/P3_60059.class b/build/classes/Programmers/P3_60059.class new file mode 100644 index 0000000..80f41c8 Binary files /dev/null and b/build/classes/Programmers/P3_60059.class differ diff --git "a/build/classes/S10_23/\352\260\200\353\245\264\354\271\250.class" "b/build/classes/S10_23/\352\260\200\353\245\264\354\271\250.class" new file mode 100644 index 0000000..549ddfb Binary files /dev/null and "b/build/classes/S10_23/\352\260\200\353\245\264\354\271\250.class" differ diff --git "a/build/classes/S10_23/\353\260\224\353\221\221\354\225\214.class" "b/build/classes/S10_23/\353\260\224\353\221\221\354\225\214.class" new file mode 100644 index 0000000..fe42dd0 Binary files /dev/null and "b/build/classes/S10_23/\353\260\224\353\221\221\354\225\214.class" differ diff --git "a/build/classes/S10_23/\354\265\234\354\206\214\353\271\204\354\232\251\352\265\254\355\225\230\352\270\2602$1.class" "b/build/classes/S10_23/\354\265\234\354\206\214\353\271\204\354\232\251\352\265\254\355\225\230\352\270\2602$1.class" new file mode 100644 index 0000000..8f3d514 Binary files /dev/null and "b/build/classes/S10_23/\354\265\234\354\206\214\353\271\204\354\232\251\352\265\254\355\225\230\352\270\2602$1.class" differ diff --git "a/build/classes/S10_23/\354\265\234\354\206\214\353\271\204\354\232\251\352\265\254\355\225\230\352\270\2602$Node.class" "b/build/classes/S10_23/\354\265\234\354\206\214\353\271\204\354\232\251\352\265\254\355\225\230\352\270\2602$Node.class" new file mode 100644 index 0000000..5674c8e Binary files /dev/null and "b/build/classes/S10_23/\354\265\234\354\206\214\353\271\204\354\232\251\352\265\254\355\225\230\352\270\2602$Node.class" differ diff --git "a/build/classes/S10_23/\354\265\234\354\206\214\353\271\204\354\232\251\352\265\254\355\225\230\352\270\2602.class" "b/build/classes/S10_23/\354\265\234\354\206\214\353\271\204\354\232\251\352\265\254\355\225\230\352\270\2602.class" new file mode 100644 index 0000000..3c77a64 Binary files /dev/null and "b/build/classes/S10_23/\354\265\234\354\206\214\353\271\204\354\232\251\352\265\254\355\225\230\352\270\2602.class" differ diff --git "a/build/classes/S10_23/\355\224\274\354\236\220\352\265\275\352\270\260.class" "b/build/classes/S10_23/\355\224\274\354\236\220\352\265\275\352\270\260.class" new file mode 100644 index 0000000..a6281cd Binary files /dev/null and "b/build/classes/S10_23/\355\224\274\354\236\220\352\265\275\352\270\260.class" differ diff --git a/build/classes/S1_14940.class b/build/classes/S1_14940.class new file mode 100644 index 0000000..f940fd5 Binary files /dev/null and b/build/classes/S1_14940.class differ diff --git a/build/classes/S_0909/S_4949.class b/build/classes/S_0909/S_4949.class new file mode 100644 index 0000000..72a8956 Binary files /dev/null and b/build/classes/S_0909/S_4949.class differ diff --git a/build/classes/S_0916/G_2179$1.class b/build/classes/S_0916/G_2179$1.class new file mode 100644 index 0000000..d038e00 Binary files /dev/null and b/build/classes/S_0916/G_2179$1.class differ diff --git a/build/classes/S_0916/G_2179$StringNum.class b/build/classes/S_0916/G_2179$StringNum.class new file mode 100644 index 0000000..db7f938 Binary files /dev/null and b/build/classes/S_0916/G_2179$StringNum.class differ diff --git a/build/classes/S_0916/G_2179.class b/build/classes/S_0916/G_2179.class new file mode 100644 index 0000000..da8c1cf Binary files /dev/null and b/build/classes/S_0916/G_2179.class differ diff --git a/build/classes/S_0916/G_7569$PointNum.class b/build/classes/S_0916/G_7569$PointNum.class new file mode 100644 index 0000000..1f99020 Binary files /dev/null and b/build/classes/S_0916/G_7569$PointNum.class differ diff --git a/build/classes/S_0916/G_7569.class b/build/classes/S_0916/G_7569.class new file mode 100644 index 0000000..853b8ee Binary files /dev/null and b/build/classes/S_0916/G_7569.class differ diff --git a/build/classes/S_0916/Programmers_64064.class b/build/classes/S_0916/Programmers_64064.class new file mode 100644 index 0000000..9fb3b35 Binary files /dev/null and b/build/classes/S_0916/Programmers_64064.class differ diff --git a/build/classes/S_0916/Swea_2948.class b/build/classes/S_0916/Swea_2948.class new file mode 100644 index 0000000..90a435b Binary files /dev/null and b/build/classes/S_0916/Swea_2948.class differ diff --git a/build/classes/S_0923/G_1043.class b/build/classes/S_0923/G_1043.class new file mode 100644 index 0000000..9476b1b Binary files /dev/null and b/build/classes/S_0923/G_1043.class differ diff --git a/build/classes/S_0923/G_14500.class b/build/classes/S_0923/G_14500.class new file mode 100644 index 0000000..c04809b Binary files /dev/null and b/build/classes/S_0923/G_14500.class differ diff --git a/build/classes/S_0923/G_1766.class b/build/classes/S_0923/G_1766.class new file mode 100644 index 0000000..f0a2f2d Binary files /dev/null and b/build/classes/S_0923/G_1766.class differ diff --git a/build/classes/S_0923/G_18428.class b/build/classes/S_0923/G_18428.class new file mode 100644 index 0000000..284d66d Binary files /dev/null and b/build/classes/S_0923/G_18428.class differ diff --git a/build/classes/S_0930/G_1174.class b/build/classes/S_0930/G_1174.class new file mode 100644 index 0000000..cc17838 Binary files /dev/null and b/build/classes/S_0930/G_1174.class differ diff --git a/build/classes/S_0930/G_1655.class b/build/classes/S_0930/G_1655.class new file mode 100644 index 0000000..040ac68 Binary files /dev/null and b/build/classes/S_0930/G_1655.class differ diff --git a/build/classes/S_0930/G_16928.class b/build/classes/S_0930/G_16928.class new file mode 100644 index 0000000..0ed7ee8 Binary files /dev/null and b/build/classes/S_0930/G_16928.class differ diff --git a/build/classes/S_0930/G_18809$PointTime.class b/build/classes/S_0930/G_18809$PointTime.class new file mode 100644 index 0000000..b7a5753 Binary files /dev/null and b/build/classes/S_0930/G_18809$PointTime.class differ diff --git a/build/classes/S_0930/G_18809.class b/build/classes/S_0930/G_18809.class new file mode 100644 index 0000000..09e6e39 Binary files /dev/null and b/build/classes/S_0930/G_18809.class differ diff --git a/build/classes/S_1014/G_14499.class b/build/classes/S_1014/G_14499.class new file mode 100644 index 0000000..537db73 Binary files /dev/null and b/build/classes/S_1014/G_14499.class differ diff --git a/build/classes/S_1014/G_1976.class b/build/classes/S_1014/G_1976.class new file mode 100644 index 0000000..744467f Binary files /dev/null and b/build/classes/S_1014/G_1976.class differ diff --git a/build/classes/S_1014/G_2629.class b/build/classes/S_1014/G_2629.class new file mode 100644 index 0000000..21b4919 Binary files /dev/null and b/build/classes/S_1014/G_2629.class differ diff --git a/build/classes/S_1014/S_18870$1.class b/build/classes/S_1014/S_18870$1.class new file mode 100644 index 0000000..34c5ee2 Binary files /dev/null and b/build/classes/S_1014/S_18870$1.class differ diff --git a/build/classes/S_1014/S_18870.class b/build/classes/S_1014/S_18870.class new file mode 100644 index 0000000..56c6587 Binary files /dev/null and b/build/classes/S_1014/S_18870.class differ diff --git a/build/classes/S_1030/G_1580$PointAB.class b/build/classes/S_1030/G_1580$PointAB.class new file mode 100644 index 0000000..f21ba97 Binary files /dev/null and b/build/classes/S_1030/G_1580$PointAB.class differ diff --git a/build/classes/S_1030/G_1580.class b/build/classes/S_1030/G_1580.class new file mode 100644 index 0000000..81be22d Binary files /dev/null and b/build/classes/S_1030/G_1580.class differ diff --git a/build/classes/S_11725.class b/build/classes/S_11725.class new file mode 100644 index 0000000..5d846dd Binary files /dev/null and b/build/classes/S_11725.class differ diff --git a/build/classes/S_1240.class b/build/classes/S_1240.class new file mode 100644 index 0000000..9efe1d9 Binary files /dev/null and b/build/classes/S_1240.class differ diff --git a/build/classes/S_1260.class b/build/classes/S_1260.class new file mode 100644 index 0000000..3669f5d Binary files /dev/null and b/build/classes/S_1260.class differ diff --git a/build/classes/S_14501.class b/build/classes/S_14501.class new file mode 100644 index 0000000..d5f354d Binary files /dev/null and b/build/classes/S_14501.class differ diff --git a/build/classes/S_1874.class b/build/classes/S_1874.class new file mode 100644 index 0000000..317ed53 Binary files /dev/null and b/build/classes/S_1874.class differ diff --git a/build/classes/S_1946$1.class b/build/classes/S_1946$1.class new file mode 100644 index 0000000..c99b568 Binary files /dev/null and b/build/classes/S_1946$1.class differ diff --git a/build/classes/S_1946.class b/build/classes/S_1946.class new file mode 100644 index 0000000..469b017 Binary files /dev/null and b/build/classes/S_1946.class differ diff --git a/build/classes/S_2178.class b/build/classes/S_2178.class new file mode 100644 index 0000000..1a726a2 Binary files /dev/null and b/build/classes/S_2178.class differ diff --git a/build/classes/S_2847.class b/build/classes/S_2847.class new file mode 100644 index 0000000..dcf2a01 Binary files /dev/null and b/build/classes/S_2847.class differ diff --git a/build/classes/S_9095.class b/build/classes/S_9095.class new file mode 100644 index 0000000..1d155b3 Binary files /dev/null and b/build/classes/S_9095.class differ diff --git a/src/G_1068.java b/src/G_1068.java new file mode 100644 index 0000000..10d2e5b --- /dev/null +++ b/src/G_1068.java @@ -0,0 +1,54 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.*; + +// 11588kb 80ms +public class G_1068 { + static int N,M; + static ArrayList[] tree; + static int answerList[]; + static int top; + public static void main(String[] args) throws Exception { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + N = Integer.parseInt(br.readLine()); + String [] s = br.readLine().split(" "); + tree =new ArrayList[N]; + for (int i = 0; i < N; i++) { + tree[i] = new ArrayList(); + } + answerList = new int[N]; + for (int i = 0; i < N; i++) { + int temp = Integer.parseInt(s[i]); + if(temp== -1) top = i ; + else tree[temp].add(i); + } + M = Integer.parseInt(br.readLine()); + + get(); + System.out.println(answerList[top]); + } + + private static void get() { + for (int i = 0; i < N; i++) { + for (int j = tree[i].size()-1; j >=0 ; j--) { + if(tree[i].get(j) == M) tree[i].remove(j); + } + + } + if(M != top) find(top,-1); + } + + private static void find(int x, int par) { + if(tree[x].isEmpty()) answerList[x] = 1; + + for (int xx :tree[x]) { + if(par == xx) continue; + find(xx,x); + answerList[x] += answerList[xx]; + + + } + } + +} \ No newline at end of file diff --git a/src/G_1238.java b/src/G_1238.java new file mode 100644 index 0000000..6e3f0f8 --- /dev/null +++ b/src/G_1238.java @@ -0,0 +1,77 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.PriorityQueue; + +//파티 +public class G_1238 { + + static int N,M,X; //N 개의 숫자, M개 단방향도로 개수 , X 돌아오고 다시 갈 수 있는 곳 + static ArrayList graph[]; + static int answer ; + static int dis[]; + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String[] s = br.readLine().split(" "); + N = Integer.parseInt(s[0]); + M = Integer.parseInt(s[1]); + X = Integer.parseInt(s[2]); + + graph = new ArrayList[N+1]; + for (int i = 1; i <=N ; i++) { + graph[i] = new ArrayList<>(); + } + for (int i = 0; i < M; i++) { + s = br.readLine().split(" "); + int a = Integer.parseInt(s[0]); + int b = Integer.parseInt(s[1]); + int wei = Integer.parseInt(s[2]); + graph[a].add(new int[]{b,wei}); //그래프만들기 + } + answer = Integer.MIN_VALUE; //정답 최솟값으로 + + dis= new int[N+1]; + for (int i = 1; i <=N ; i++) { //1부터 N까지 + int go = dj(i,X); //가보고 + int back = dj(X,i); //돌아오기 + answer = Math.max(answer,go+back); //정답 갱신 + } +// System.out.println(Arrays.toString(dis)); + System.out.println(answer); + } + + private static int dj(int start, int end) { + Arrays.fill(dis,Integer.MAX_VALUE); + dis[start] = 0; //시작점 0 + PriorityQueue q = new PriorityQueue<>(new Comparator() { + @Override + public int compare(int[] o1, int[] o2) { + return Integer.compare(o1[1],o2[1]); + } + }); + q.add(new int[]{start,0}); + + while(!q.isEmpty()){ //다익스트라 + int[] cur = q.poll(); + int node = cur[0]; + int wei = cur[1]; + if(wei > dis[node]) continue; + + for (int[] next: graph[node]) { + int nxtN = next[0]; + int nxtW = next[1]; + + if (wei + nxtW < dis[nxtN]) { + dis[nxtN] = wei + nxtW; + q.add(new int[]{nxtN, dis[nxtN]}); + + } + } + } + return dis[end]; //X 까지 거리 계산 + } + + +} \ No newline at end of file diff --git a/src/G_14502.java b/src/G_14502.java new file mode 100644 index 0000000..f1f850f --- /dev/null +++ b/src/G_14502.java @@ -0,0 +1,91 @@ +import java.awt.*; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.ArrayList; + +public class G_14502 { + static ArrayList blank,virus; //빈칸이랑 바이러스 저장 + static int N,M; + static int map[][]; + static int answer; + static int dx[] = {-1,1,0,0}, dy[] ={0,0,-1,1}; + public static void main(String[] args) throws Exception{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + String[] s = br.readLine().split(" "); + N = Integer.parseInt(s[0]); + M = Integer.parseInt(s[1]); + map = new int[N][M]; + blank = new ArrayList(); + virus = new ArrayList(); + answer = 0; + + //맵 입력받기 + for (int i = 0; i < N; i++) { + s= br.readLine().split(" "); + for (int j = 0; j < M; j++) { + map[i][j] = Integer.parseInt(s[j]); + if(map[i][j] == 0 )blank.add(new Point(j,i)); //빈칸 추가 + if(map[i][j] == 2) virus.add(new Point(j,i)); //바이러스 추가 + } + } + find(0,0,new Point[3]); //찾기 + System.out.println(answer); //정답출력 + } + + private static void find(int cnt,int start, Point[] arr) { + if(cnt ==3 ){ + int[][] map = copyMap(arr); //빈칸 3개 찾으면 맵 복사 벽세우기 + int safezone = count(map); //바이러스 퍼트리고 카운트 세기 + if(safezone > answer) answer = Math.max(answer,safezone); //안전지대 갱신 + return; + } + for (int i = start; i < blank.size() ; i++) { + arr[cnt] = blank.get(i); + find(cnt+1,i+1,arr); + } + } + + private static int count(int[][] map) { //바이러스 bfs로 퍼트리고 카운트 세기 + ArrayDeque dq = new ArrayDeque(); + for (int i = 0; i < virus.size(); i++) { + dq.offer(virus.get(i)); + } + while(!dq.isEmpty()){ + Point p = dq.poll(); + for (int i = 0; i < 4; i++) { + int x = p.x + dx[i]; + int y = p.y + dy[i]; + if(x>=0 && x=0 && y[] route; + static boolean visited[],cycle[]; + static int distance[]; + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + N = Integer.parseInt(br.readLine()); + route = new ArrayList[N+1]; + cycle = new boolean[N+1]; + distance = new int[N+1]; + + for (int i = 1; i <= N; i++) { + route[i] = new ArrayList(); + } + for (int i = 1; i <= N; i++) { + String s[] = br.readLine().split(" "); + int a = Integer.parseInt(s[0]); + int b = Integer.parseInt(s[1]); + route[a].add(b); + route[b].add(a); + } + + for (int i = 1; i <= N; i++) { + if(find(i,-1,i)){ + break; + } + Arrays.fill(cycle,false); + } + + bfs(); + + for (int i = 1; i <= N; i++) { + System.out.print(distance[i] + " "); + } + + } + + private static void bfs() { + ArrayDeque dq = new ArrayDeque(); + visited = new boolean[N+1]; + + for (int i = 1; i <=N ; i++) { + if(cycle[i]){ + dq.add(i); + visited[i] = true; + } + } + while(!dq.isEmpty()){ + int p = dq.poll(); + for (int ro:route[p]) { + if(!visited[ro]){ + visited[ro] = true; + dq.add(ro); + distance[ro] = distance[p]+1; + } + } + } + + + } + + private static boolean find(int now,int par,int start) { + cycle[now] = true; + for (int next : route[now]) { + if(!cycle[next]){ + if(find(next, now,start)) return true; + + } + else if(next == start && par != next) return true; + } + cycle[now] = false; + return false; + } + + +} diff --git a/src/G_18111.java b/src/G_18111.java new file mode 100644 index 0000000..0ac016f --- /dev/null +++ b/src/G_18111.java @@ -0,0 +1,48 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; + +public class G_18111 { + static int N,M,B; + static int [][]map; + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String[] s = br.readLine().split(" "); + N = Integer.parseInt(s[0]); + M = Integer.parseInt(s[1]); + B = Integer.parseInt(s[2]); + int time = Integer.MAX_VALUE; // 걸리는 시간 + int high = Integer.MAX_VALUE; // 높이 + + map = new int[N][M]; + for (int i = 0; i h){ //h보다 현재 높이가 클 경우 + remove += (map[i][j]- h); //h만큼 깎아주기 + }else{ //같거나 크면 + add += (h- map[i][j]); //현재 높이만큼 더해주기 + } + } + } + //제거한 블록과 가지고 있던 블록개수가 추가한 것 보다 크거나 같을 때 + // 평탄화 가능함 + if(remove+B >= add){ + int tempTime = remove * 2 + add; //시간 계산해놓고 + if(time >= tempTime){ // 만약에 정답 시간이 더 클 경우 업데이트 작은 시간으로 같을때도 높이 갱신 + time = tempTime; //시간 업데이트 + high = h; //높이 업데이트 + } + } + } + System.out.println(time + " " +high); //출력 + } + +} diff --git a/src/G_2206.java b/src/G_2206.java new file mode 100644 index 0000000..1f9d81e --- /dev/null +++ b/src/G_2206.java @@ -0,0 +1,92 @@ +import java.awt.*; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.Arrays; + +public class G_2206 { + + static int N, M; //N 과 M 크기 + static Point start; //시작점 + static int map[][]; //맵 + static boolean visited[][][]; //방문기록 3차원 + static int dx[] = {-1,1,0,0} , dy[] = {0,0,-1,1}; //이동하기 + static int answer; //정답 + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String[] s = br.readLine().split(" "); + + N = Integer.parseInt(s[0]); + M = Integer.parseInt(s[1]); + map = new int[N][M]; + visited = new boolean[N][M][2]; + answer = -1; //초기값 -1 + + for (int i = 0; i < N; i++) { + s = br.readLine().split(""); + for (int j = 0; j < M; j++) { + map[i][j] = Integer.parseInt(s[j]); + } + } + + start = new Point(0,0); + ArrayDeque dq = new ArrayDeque(); + + //시작점,이동횟수,1 은 부셔버릴 횟수 + dq.add(new PointN(start, 0,1)); + visited[start.y][start.x][1] = true; + + while(!dq.isEmpty()){ + PointN p = dq.poll(); //좌표 꺼내서 + if(p.x == M-1 && p.y == N-1){ //끝에 도달하면 + if(map[p.y][p.x] == 0){ + answer = p.broke+1; // 브레이크 + break; + } + else{ + break; //끝에 도달했는데 1이여도 브레이크 정답 갱신 안하고 + } + } + + for (int i = 0; i < 4; i++) { + int x = p.x + dx[i]; + int y = p.y + dy[i]; + + if(x >=0 && x< M && y >= 0 && y p ; //토마토 좌표 다 저장하기 + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String[] s = br.readLine().split(" "); + n = Integer.parseInt(s[1]); + m = Integer.parseInt(s[0]); + toma = new int[n][m]; + p = new ArrayList<>(); + for (int i = 0; i dq = new ArrayDeque<>(); + for (int i = 0; i < p.size(); i++) { //토마토 좌표를 데크에 다 넣어주기 + dq.add(p.get(i)); + } + + while(!dq.isEmpty()){ + Point point = dq.poll(); + for (int i = 0; i < 4; i++) { + int xx = point.x + dx[i]; + int yy = point.y + dy[i]; + if(xx>=0 && xx=0 && yy> grp; //그래프 저장 + static ArrayList answer; //정답리스트 + static int totalTickets; //총길이 + static int first=0; //답 여러개 경우 하나만 출력하기 위해서 사용 + public ArrayList solution(String[][] tickets) { + grp = new HashMap<>(); + answer= new ArrayList(); + for (int i = 0; i < tickets.length; i++) { + String a = tickets[i][0]; + String b = tickets[i][1]; + if(!grp.containsKey(a)){ + grp.put(a, new ArrayList()); + } + grp.get(a).add(b); //그래프 추가 + } + for (ArrayList values: grp.values()) { + Collections.sort(values); //이름순으로 방문하기 위해 정렬 + } + + totalTickets = tickets.length+1; //정답 길이는 티켓 길이 +1 + ArrayList dd = new ArrayList(); + dd.add("ICN"); //처음에 인천넣어주기 + find("ICN", dd); //찾기 + + return answer; + } + + private void find(String v, ArrayList data) { + if(totalTickets == data.size()){ //티켓길이랑 data 길이가 같으면 + if(first != 0) return; //첫번째가 아니면 리턴 + answer= (ArrayList) data.clone(); //정답에 데이터 복사 + first++; //복사되었으니 first 하나 증가 + return; + } + if(grp.containsKey(v)){ //해당 키가 있으면 + ArrayList nodes = grp.get(v); //꺼내기 + for(int i =0; i= 0; j--) { //원복하려고하는데 같은 이름일 경우 remove 사용하면 앞에꺼 삭제되어서 꼬임 + if(data.get(j).equals(city)){ //그래서 뒤에꺼 제거 + data.remove(j); + break; + } + } + nodes.add(i,city); //원래자리에 도시다시 넣기 + } + } + } +} diff --git a/src/Programmers/P3_60059.java b/src/Programmers/P3_60059.java new file mode 100644 index 0000000..9a6c0c9 --- /dev/null +++ b/src/Programmers/P3_60059.java @@ -0,0 +1,90 @@ +package Programmers; + +import java.awt.*; +import java.util.*; + + +class P3_60059 { + public int[][] copyMap(int key[][]){ //맵복사 + int M = key.length; + int temp[][] = new int[M][M]; + for(int i=0; i find(int[][] key){ //열쇠 찾아서 리스트로 반환 + ArrayList temp = new ArrayList(); + int M = key.length; + for(int i=0; i keys , int [][] map){ + int N = map.length; + for(int i =-20; i<=20; i++){ //처음부터 끝까지 맵 크기 + 열쇠크기 + for(int j =-20; j<=20; j++){ + for(int z = 0; z=0 && x=0 && y=0 && x=0 && y keys = new ArrayList(); + //4번 돌리면서 확인 + for(int asd=0 ; asd<4 ; asd++){ + for(int i= 0 ; i 로 현재 키 값들 반환 + // System.out.println(keys); + if(Check(keys,lock)) return true; //키 값으로 맵 전체 탐색 + key = copyMap(tempKey); //키맵 업데이트 + + } + + return false; + } +} \ No newline at end of file diff --git "a/src/S10_23/\352\260\200\353\245\264\354\271\250.java" "b/src/S10_23/\352\260\200\353\245\264\354\271\250.java" new file mode 100644 index 0000000..d9083df --- /dev/null +++ "b/src/S10_23/\352\260\200\353\245\264\354\271\250.java" @@ -0,0 +1,96 @@ +package S10_23; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.HashSet; +import java.util.Set; + +// 37308kb 624ms +public class 가르침 { + static Set commonWord,learnWord; + static int n,k; + static int learn[]; + static int answer; + static Set words []; + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String[] s = br.readLine().split(" "); + n = Integer.parseInt(s[0]); + k = Integer.parseInt(s[1]); + if(k <5) { + System.out.println(0); + return; + } + words = new HashSet[n]; + for (int i = 0; i < n; i++) { + words[i] = new HashSet(); + } + for (int i = 0; i < n; i++) { + char[] charArray = br.readLine().toCharArray(); + for (int j = 0; j < charArray.length; j++) { + words[i].add(charArray[j]); + } + + } + + learn = new int[123]; + commonWord = new HashSet(); + commonWord.add('a'); + commonWord.add('c'); + commonWord.add('t'); + commonWord.add('n'); + commonWord.add('i'); + + for (char a :commonWord) { + learn[a] = 1; + } + + learnWord = new HashSet(); + for (int i = 97; i < 123; i++) { + learnWord.add((char)i); + } + learnWord.removeAll(commonWord); + Object[] array = learnWord.toArray(); + + find(new char[k-5],0,0,array); + System.out.println(answer); + } + + private static void find(char temp[],int cnt,int index,Object[] arr) { + if(k-5 == cnt){ + for (char w :temp) { + learn[w] = 1; + } + int a = check(); + if(answer < a) answer = Math.max(answer,a); + for (char w :temp) { + learn[w] = 0; + } + return; + } + for (int i = index; i < arr.length; i++) { + char w = (char) arr[i]; + temp[cnt] = w; + find(temp,cnt+1,i + 1,arr); + } + + } + + private static int check() { + int temp = 0; + for (int i = 0; i < words.length; i++) { + boolean flag = true; + for (char w : words[i]) { + if(learn[w] == 0){ + flag = false; + break; + } + } + if(flag == true){ + temp++; + } + } + return temp; + } +} diff --git "a/src/S10_23/\353\260\224\353\221\221\354\225\214.java" "b/src/S10_23/\353\260\224\353\221\221\354\225\214.java" new file mode 100644 index 0000000..33bc6ec --- /dev/null +++ "b/src/S10_23/\353\260\224\353\221\221\354\225\214.java" @@ -0,0 +1,131 @@ +package S10_23; + +import java.awt.*; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.ArrayList; + +//106212kb 468ms +public class 바둑알 { + //N : y M : x + static int N, M; + //영위치 저장 + static ArrayList zeroPoint; + //맵저장 + static int map[][]; + //임시 바둑알세기 + static ArrayList tempCnt; + //방문처리 + static boolean visited[][]; + static int dx[] ={0,0,-1,1} , dy[]={-1,1,0,0}; + static int answer; + public static void main(String[] args) throws Exception { + //입력 + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String[] s = br.readLine().split(" "); + N = Integer.parseInt(s[0]); + M = Integer.parseInt(s[1]); + + //초기화 + zeroPoint = new ArrayList(); + map = new int[N][M]; + answer = 0; + + //0위치 추가 + for (int i = 0; i < N; i++) { + s = br.readLine().split(" "); + for (int j = 0; j < M; j++) { + map[i][j] = Integer.parseInt(s[j]); + if (map[i][j] == 0) zeroPoint.add(new Point(j, i)); + } + } + //찾으러가기 + find(0, 0, new Point[2]); + + //정답출력 + System.out.println(answer); + } + //조합생성 + private static void find(int cnt, int idx, Point[] point) { + //2개 뽑기 + if (cnt == 2) { + int temp = check(point); + if(answer (); + visited = new boolean[N][M]; + //0인 위치 흰 바둑알 두개 + map[point[0].y][point[0].x] = 1; + map[point[1].y][point[1].x] = 1; + + //흰 바둑알 둔 위치에서 상하좌우탐색 + for (int i = 0; i < 2; i++) { + for (int j = 0; j < 4; j++) { + int x = point[i].x + dx[j]; + int y = point[i].y + dy[j]; + if(x>=0 && y>=0 && x a).sum(); + } + + private static void bfs(int j, int i) { + visited[i][j] = true; + ArrayDeque dq = new ArrayDeque(); + dq.add(new Point(j,i)); + + //검은바둑알세기 + int cnt = 1; + + + while (!dq.isEmpty()){ + Point p = dq.poll(); + //흰바둑알이면 아무것도 하지않아 + if(map[p.y][p.x] == 1) continue; + + //상하좌우돌면서 + for (int k = 0; k < 4; k++) { + int x = p.x + dx[k]; + int y = p.y + dy[k]; + if(x>=0 && x=0 && y graph[]; + static ArrayList data; + + public static void main(String[] args) throws Exception{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringBuilder sb = new StringBuilder(); + n = Integer.parseInt(br.readLine()); + m = Integer.parseInt(br.readLine()); + graph = new ArrayList[n+1]; + data = new ArrayList(); + for (int i = 1; i < n+1; i++) { + graph[i] = new ArrayList(); + } + for (int i = 0; i < m; i++) { + String[] s = br.readLine().split(" "); + int a = Integer.parseInt(s[0]); + int b = Integer.parseInt(s[1]); + int c = Integer.parseInt(s[2]); + graph[a].add(new Node(b,c)); + } + String []s = br.readLine().split(" "); + start = Integer.parseInt(s[0]); + end = Integer.parseInt(s[1]); + + dij(sb); + } + + private static void dij(StringBuilder sb) { + PriorityQueue pq = new PriorityQueue(new Comparator(){ + @Override + public int compare(Node o1, Node o2) { + return Integer.compare(o1.weight,o2.weight); + } + }); + pq.add(new Node(start,0)); + int dist[] = new int[n+1]; + Arrays.fill(dist,MAX_VAL); + dist[start] = 0; + int route[] = new int[n+1]; + while(!pq.isEmpty()){ + Node cur = pq.poll(); + int no = cur.no; + int weight = cur.weight; + + if(dist[no] < weight) continue; + for (Node next :graph[no]) { + + if( next.weight + dist[no] < dist[next.no]){ + dist[next.no] = next.weight + dist[no]; + route[next.no] = no; + pq.add(new Node(next.no,dist[next.no])); + } + } + + } + + sb.append(dist[end]).append("\n"); + System.out.println(Arrays.toString(route)); + + Stack st = new Stack(); + for (int i = end; i!=start ; i=route[i]) { + + st.push(i); + } + st.push(start); + sb.append(st.size()).append("\n"); + while(!st.isEmpty()){ + sb.append(st.pop()).append(" "); + } + sb.append("\n"); + System.out.println(sb); + + } + + private static class Node { + int no, weight; + + @Override + public String toString() { + return "Node{" + + "no=" + no + + ", weight=" + weight + + '}'; + } + + public Node(int no, int weight) { + this.no = no; + this.weight = weight; + } + } +} diff --git "a/src/S10_23/\355\224\274\354\236\220\352\265\275\352\270\260.java" "b/src/S10_23/\355\224\274\354\236\220\352\265\275\352\270\260.java" new file mode 100644 index 0000000..0e98b86 --- /dev/null +++ "b/src/S10_23/\355\224\274\354\236\220\352\265\275\352\270\260.java" @@ -0,0 +1,42 @@ +package S10_23; + +import java.io.BufferedReader; +import java.io.InputStreamReader; + +public class 피자굽기 { + static int d,n; + static int pizza[], oven []; + + public static void main(String[] args) throws Exception{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String[] s = br.readLine().split(" "); + + d = Integer.parseInt(s[0]); + n = Integer.parseInt(s[1]); + oven = new int[d]; + pizza = new int[n]; + + s = br.readLine().split(" "); + oven[0] = Integer.parseInt(s[0]); + for (int i = 1; i < oven.length; i++) { + oven[i] = Math.min(Integer.parseInt(s[i]),oven[i-1]); + } + s = br.readLine().split(" "); + for (int i = 0; i < pizza.length; i++) { + pizza[i] = Integer.parseInt(s[i]); + } + + int depth = d-1; + + for (int p : pizza) { + while(depth >=0 && p>oven[depth]) depth--; + if(depth<0) { + System.out.println(0); + return; + } + depth--; + } + System.out.println(depth+2); + } + +} diff --git a/src/S1_14940.java b/src/S1_14940.java new file mode 100644 index 0000000..35a1568 --- /dev/null +++ b/src/S1_14940.java @@ -0,0 +1,77 @@ +import java.awt.*; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayDeque; + +public class S1_14940 { + static int n , m ; + static int map[][]; + static int dx[] ={1,-1,0,0} ,dy[] ={0,0,-1,1}; //방향 + static Point p; //시작 위치 + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String[] s = br.readLine().split(" "); + n = Integer.parseInt(s[0]); + m = Integer.parseInt(s[1]); + map = new int[n][m]; + for (int i = 0; i < n; i++) { + s = br.readLine().split(" "); + for (int j = 0; j < m; j++) { + map[i][j] = Integer.parseInt(s[j]); + if(map[i][j] == 2){ + map[i][j] = 0; + p = new Point(j,i); + } + } + } + ArrayDeque dq = new ArrayDeque(); + dq.add(p); + boolean [][] visited = new boolean[n][m]; + + for (int i = 0; i =0 && xx =0 && yy stack = new Stack<>(); + boolean chk = true; + + for (int i =0; i< s.length();i++) { + char ch = s.charAt(i); + if (ch == '[' || ch == '(') { + stack.push(ch); + } else { + if (ch == ')') { + if (!stack.isEmpty() && stack.peek() == '(') { + stack.pop(); + } else { + chk = false; + break; + } + } + else if (ch == ']') { + + if (!stack.isEmpty() && stack.peek() == '[') { + stack.pop(); + } else { + chk = false; + break; + } + + } + } + + if (!chk) break; + } + if (!stack.isEmpty()) chk = false; + + sb.append(chk ? "yes" : "no").append("\n"); + } + System.out.println(sb); + } +} diff --git a/src/S_0916/G_2179.java b/src/S_0916/G_2179.java new file mode 100644 index 0000000..3c23ebf --- /dev/null +++ b/src/S_0916/G_2179.java @@ -0,0 +1,91 @@ +package S_0916; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + +public class G_2179 { + static int N; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + N = Integer.parseInt(br.readLine()); + ArrayList list = new ArrayList(); + ArrayList list2 = new ArrayList(); + for (int i = 0; i < N; i++) { + list.add(new StringNum(br.readLine(), i)); //입력 받기 문자열이랑, 인덱스 + } + Collections.sort(list); //문자열 기준 정렬 + int max = 0; + for (int i = 0; i < list.size()-1; i++) { + int cnt = 0; //카운트 세기 + StringNum a = list.get(i); + StringNum b = list.get(i + 1); + for (int j = 0; j < Math.min(a.s.length(),b.s.length()); j++) { + if (a.s.charAt(j) == b.s.charAt(j)) { + cnt++; //두 단어 같으면 cnt ++ + } + else break; + } + if(cnt > max){ //기존 값보다 더 크면 이전거 지우고 리스트에 추가 + max = cnt; + list2.clear(); + list2.add(a); + list2.add(b); + } + else if(cnt == max){ //같으면 둘다 추가 + list2.add(a); + list2.add(b); + } + } + + + Collections.sort(list2, new Comparator() { //인덱스 기준 정렬 + @Override + public int compare(StringNum o1, StringNum o2) { + return Integer.compare(o1.n,o2.n); + } + }); + String S,T; + StringNum s = list2.get(0); //인덱스 가장 빠른애 뽑고 + String pre = s.s.substring(0, max); //0 ~ max 값까지인 문자열로 만들고 + S = s.s; + T = null; + for (int i = 0; i < list2.size(); i++) { + if(s.n == list2.get(i).n) continue; //근데 비교안하고 넣어서 똑같은거 생길 수 있으니깐 인덱스 같으면 넘기기 + if (list2.get(i).s.substring(0, max).equals(pre)) { //맥스길이까지 짤라서 비교 + T = list2.get(i).s; //이제 같으면 T에 추가하고 break; + break; + } + } + System.out.println(S); + System.out.println(T); + } + + private static class StringNum implements Comparable{ + String s; + int n; + + public StringNum(String s, int n) { + this.s = s; + this.n = n; + } + + + @Override + public String toString() { + return "StringNum{" + + "s='" + s + '\'' + + ", n=" + n + + '}'; + } + + @Override + public int compareTo(StringNum o) { + return this.s.compareTo(o.s); + } + } + + +} \ No newline at end of file diff --git a/src/S_0916/G_7569.java b/src/S_0916/G_7569.java new file mode 100644 index 0000000..09d50d3 --- /dev/null +++ b/src/S_0916/G_7569.java @@ -0,0 +1,95 @@ +package S_0916; +import java.awt.*; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.ArrayList; + +public class G_7569 { + static int N,M,H; + static int dx[] = {0,0,-1,1}, dy[] = {1, -1, 0, 0} ,backgo[] ={-1,1}; + static int map[][][]; + static ArrayList p; //토마토 위치 x,y,h 가진애들 + static class PointNum extends Point{ + int height; + + + public PointNum(Point p, int height) { + super(p); + this.height = height; + } + + public PointNum(int x, int y, int height) { + super(x, y); + this.height = height; + } + + @Override + public String toString() { + return super.toString()+ "height : "+ height; + } + } + public static void main(String[] args) throws Exception{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String[] s = br.readLine().split(" "); + M = Integer.parseInt(s[0]); + N = Integer.parseInt(s[1]); + H = Integer.parseInt(s[2]); + + map = new int[N][M][H]; + p = new ArrayList(); + for (int z = 0; z < H; z++) { + for (int i = 0; i < N; i++) { + s = br.readLine().split(" "); + for (int j = 0; j < M; j++) { + map[i][j][z] = Integer.parseInt(s[j]); + if(map[i][j][z] == 1) p.add(new PointNum(j,i,z)); //1위치 저장 + } + } + } + ArrayDeque dq = new ArrayDeque(); + for (int i = 0; i < p.size(); i++) { //1인애들 데크에 넣기 + dq.add(p.get(i)); + } + + while(!dq.isEmpty()){ + PointNum toma = dq.poll(); //꺼내서 + for (int i = 0; i < 4; i++) { //상하좌우 비교 + int x = toma.x + dx[i]; + int y = toma.y + dy[i]; + if(x >=0 &&x=0 && y=0 && h> data; + public void perm(boolean visited[],int cnt,String [] arr,int len, String []user_id ){ + if(cnt == len ){ + List collect = Arrays.stream(arr).collect(Collectors.toList()); + data.add((ArrayList) collect); + + return; + } + + for(int i=0 ; i>(); + + //순열 돌리기 + perm(new boolean[user_id.length], 0, new String[user_id.length], user_id.length, user_id); + + //정답 해시셋 + HashSet real = new HashSet(); + + //한줄씩 비교 + for (ArrayList list :data) { + //비교한 결과 저장하는 리스트 + ArrayList temp = new ArrayList(); + + //벤아이디 배열길이 만큼 반복 + for (int i = 0; i < banned_id.length; i++) { + //단어 길이 세기 + int cnt =0; + //유저아이디 첫번째랑 벤아이디 첫번째랑 문자열 길이 다르면 break + if(list.get(i).length() != banned_id[i].length()) break; + + //같으면 문자열비교 + for (int j = 0; j < banned_id[i].length(); j++) { + //둘이 같거나 + if(list.get(i).charAt(j) == banned_id[i].charAt(j)){ + cnt++; + } + //별이면 cnt ++ + else if(list.get(i).charAt(j) != banned_id[i].charAt(j) && banned_id[i].charAt(j) == '*'){ + cnt++; + } + //아니면 break + else break; + } + //길이 같으면 list에 추가 + if(cnt == banned_id[i].length()){ + temp.add(list.get(i)); + } + } + //둘이 길이 같으면 + if(temp.size() == banned_id.length){ + //정렬 후 헤시셋에 저장 + Collections.sort(temp); + real.add(temp); + } + } + return real.size(); + } + + +} \ No newline at end of file diff --git a/src/S_0916/Swea_2948.java b/src/S_0916/Swea_2948.java new file mode 100644 index 0000000..54ef9ea --- /dev/null +++ b/src/S_0916/Swea_2948.java @@ -0,0 +1,50 @@ +package S_0916; + + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.HashSet; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.HashSet; + +public class Swea_2948 { + static int T, N,M; + static String arrN[], arrM[]; + public static void main(String[] args) throws Exception{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + T = Integer.parseInt(br.readLine()); + StringBuilder sb = new StringBuilder(); + for (int test_case = 1; test_case <=T ; test_case++) { + String[] s = br.readLine().split(" "); + int cnt = 0; + N = Integer.parseInt(s[0]); + M = Integer.parseInt(s[1]); + arrN = new String[N]; + arrM = new String[M]; + s = br.readLine().split(" "); + for (int i = 0; i < arrN.length; i++) { + arrN[i] = s[i]; + } + + s = br.readLine().split(" "); + for (int i = 0; i < arrM.length; i++) { + arrM[i] = s[i]; + } + + + HashSet setN= new HashSet<>(Arrays.asList(arrN)); + HashSet setM= new HashSet<>(Arrays.asList(arrM)); + + setN.retainAll(setM); // 두 집합의 교집합을 구함 + + cnt=setN.size(); + sb.append("#").append(test_case).append(" ").append(cnt).append("\n"); + } + System.out.println(sb); + + } +} diff --git a/src/S_0923/G_1043.java b/src/S_0923/G_1043.java new file mode 100644 index 0000000..b778c25 --- /dev/null +++ b/src/S_0923/G_1043.java @@ -0,0 +1,71 @@ +package S_0923; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayList; + +//11856kb 88ms +public class G_1043 { + static int N,M; + static ArrayList party[]; //파티배열 + static ArrayList KnowsPeople; //거짓말아는사람들 + static boolean[] visited; //파티 방문 체크 + + public static void main(String[] args) throws Exception{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String[] s = br.readLine().split(" "); + N = Integer.parseInt(s[0]); + M = Integer.parseInt(s[1]); + + party = new ArrayList[M + 1]; + for (int i = 1; i <= M; i++) { + party[i] = new ArrayList<>(); + } + + //거짓말 아는 사람들 추가 + KnowsPeople = new ArrayList<>(); + s = br.readLine().split(" "); + for (int i = 1; i <= Integer.parseInt(s[0]); i++) { + KnowsPeople.add(Integer.parseInt(s[i])); + } + //파티 입력 추가 + for (int i = 1; i <= M ; i++) { + s = br.readLine().split(" "); + for (int j = 1; j <= Integer.parseInt(s[0]); j++) { + party[i].add(Integer.parseInt(s[j])); + } + } + + visited = new boolean[M + 1]; + //거짓말아는 사람 하나 꺼내서 모든 파티에 있는지 확인 + for(int person : KnowsPeople){ + for(int i=1;i<=M;i++){ + //거짓말 아는 사람 있으면서 i 파티 방문처리가 안되어있으면 + if(party[i].contains(person) && !visited[i]){ + dfs(i); + } + } + } + + int answer=0; + //방문 안한파티카운트 세기 + for(int i=1;i<=M;i++){ + if(!visited[i]) answer++; + } + System.out.println(answer); + } + + private static void dfs(int partyNum) { + //현재 파티 방문처리 + visited[partyNum] = true; + //현재 파티에서 또 사람들 꺼내서 + for(int person : party[partyNum]){ + for(int j=1;j<=M;j++){ + //다른파티에 현재파티 사람들이 있거나 방문 안했으면 재귀 + if(party[j].contains(person) && !visited[j]){ + dfs(j); + } + } + } + } +} \ No newline at end of file diff --git a/src/S_0923/G_14500.java b/src/S_0923/G_14500.java new file mode 100644 index 0000000..03e0e8f --- /dev/null +++ b/src/S_0923/G_14500.java @@ -0,0 +1,195 @@ +package S_0923; + +import java.io.BufferedReader; +import java.io.InputStreamReader; + +// 38304kb 596ms +//테트로미노 +public class G_14500 { + static int N, M,answer; + static int map[][]; + public static void main(String[] args) throws Exception{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + String[] s = br.readLine().split(" "); + N = Integer.parseInt(s[0]); + M = Integer.parseInt(s[1]); + + map = new int[N][M]; + for (int i = 0; i < N; i++) { + s = br.readLine().split(" "); + for (int j = 0; j < M; j++) { + map[i][j] = Integer.parseInt(s[j]); + } + } + //맵 다 돌면서 블록 다 넣어보기 가장 큰거 정답으로 했음 + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + //5개 블록 넣어본다 + int 일번 = 일자(j,i); + int 이번 = 네모(j,i); + int 삼번 = 니은자(j,i); + int 사번 = 이상한거(j,i); + int 오번 = 반(j,i); +// System.out.print("i = " + i + " j = "+ j + " "); +// System.out.println(일번 + " "+ 이번 + " " + 삼번 +" "+ 사번 + " " + 오번); + answer = Math.max(Math.max(Math.max(Math.max(일번,이번),Math.max(삼번,사번)),오번),answer); + } + } + System.out.println(answer); + + } + + private static int 반(int x, int y) { + int temp = map[y][x]; + if(check(x-1,y) && check(x+1,y) && check(x,y+1)){ + temp += map[y][x - 1]; + temp += map[y][x + 1]; + temp += map[y + 1][x]; + } + int temp2 = map[y][x]; + if(check(x+1,y) && check(x,y-1) && check(x,y+1)){ + temp2 += map[y][x + 1]; + temp2 += map[y - 1][x]; + temp2 += map[y + 1][x]; + } + + int temp3 = map[y][x]; + if(check(x-1,y) && check(x,y-1) && check(x,y+1)){ + temp3 += map[y][x - 1]; + temp3 += map[y - 1][x]; + temp3 += map[y + 1][x]; + } + int temp4 = map[y][x]; + if(check(x-1,y) && check(x+1,y) && check(x,y-1)){ + temp4 += map[y][x - 1]; + temp4 += map[y][x + 1]; + temp4 += map[y - 1][x]; + } + return Math.max(Math.max(temp,temp2),Math.max(temp3, temp4)); + } + + + private static int 이상한거(int x, int y) { + int temp = 0; + + temp += map[y][x]; + if(check(x-1,y-1)){ + temp += map[y][x-1]; + temp += map[y-1][x-1]; + if(check(x,y+1)) temp+= map[y+1][x]; + } + int temp2 = 0; + + temp2 += map[y][x]; + if(check(x+1,y-1)){ + temp2 += map[y][x+1]; + temp2 += map[y-1][x+1]; + if(check(x,y+1)) temp2+= map[y+1][x]; + } + + int max = Math.max(temp,temp2); + temp = 0; + + temp += map[y][x]; + if(check(x+1,y-1)){ + temp += map[y-1][x]; + temp += map[y][x+1]; + if(check(x-1,y-1)) temp+= map[y-1][x-1]; + } + temp2 = 0; + + temp2 += map[y][x]; + if(check(x-1,y-1)){ + temp2 += map[y][x-1]; + temp2 += map[y-1][x]; + if(check(x+1,y-1)) temp2+= map[y-1][x+1]; + } + int max2 = Math.max(temp,temp2); + return Math.max(max,max2); + } + + private static int 니은자(int x, int y) { + int 가로 = 0; + int 세로 = 0; + for (int i = 0; i < 3; i++) { + if(!check(x+i,y)) break; + 가로+= map[y][x+i]; + } + + for (int i = 0; i < 3; i++) { + if(!check(x,y+i)) break; + 세로+= map[y+i][x]; + } + int a = 가로,b = 가로,c = 가로,d = 가로; + if(check(x,y+1)){ + a += map[y+1][x]; + } + if(check(x,y-1)){ + b += map[y-1][x]; + + }if(check(x+2,y-1)){ + c += map[y-1][x+2]; + }if(check(x+2,y+1)){ + d += map[y+1][x+2]; + } + int max = Math.max(Math.max(a,b),Math.max(c,d)); + a = 세로; + b = 세로; + c = 세로; + d = 세로; + if(check(x+1,y)){ + a += map[y][x+1]; + } + + if(check(x-1,y)){ + b += map[y][x-1]; + + } + if(check(x-1,y+2)){ + c += map[y+2][x-1]; + } + if(check(x+1,y+2)){ + d += map[y+2][x+1]; + } + int max2 = Math.max(Math.max(a,b),Math.max(c,d)); + + return Math.max(max,max2); + + } + + private static int 네모(int x, int y) { + int temp = 0; + for (int i = 0; i < 2; i++) { + for (int j = 0; j < 2; j++) { + if(!check(x+j,y+i)) return 0; + temp += map[y+i][x+j]; + } + } + return temp; + } + + private static int 일자(int x, int y) { + int temp = 0,temp2=0; + + for (int j = 0; j < 4; j++) { + if (!check(x + j, y)) { + break; + } + temp += map[y][x+j]; + } + for (int j = 0; j < 4; j++) { + if (!check(x, y + j)) { + break; + } + temp2 += map[y+j][x]; + } + return Math.max(temp,temp2); + } + private static boolean check(int x,int y){ + if(x>=0 && x=0 && y[] graph; + static int N,M; + static int arr[]; + + public static void main(String[] args) throws Exception{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String s [] = br.readLine().split(" "); + + N = Integer.parseInt(s[0]); + M = Integer.parseInt(s[1]); + + graph = new ArrayList[N + 1]; + for (int i = 1; i <=N; i++) { + graph[i] = new ArrayList(); + } + //위상정렬 맵 입력받고 진입차수 세기 + arr = new int[N+1]; + for (int i = 0; i < M; i++) { + s = br.readLine().split(" "); + int a = Integer.parseInt(s[0]); + int b = Integer.parseInt(s[1]); + graph[a].add(b); + graph[b].add(a); + arr[b]++; + } + StringBuilder sb = new StringBuilder(); + //우선순위 큐로 만듬 3번 조건 -쉬운 문제먼저 풀기 + PriorityQueue dq = new PriorityQueue<>(); + for (int i = 1; i <=N ; i++) { + if(arr[i] ==0){ + dq.add(i); + + } + } + + //하나씩 꺼내서 진입차수 0 되는거 추가시켜주기 + while (!dq.isEmpty()){ + int idx = dq.poll(); + sb.append(idx).append(" "); + for (int next :graph[idx]) { + + if(--arr[next] == 0){ + dq.add(next); + } + + } + } + sb.append("\n"); + System.out.println(sb); + } +} diff --git a/src/S_0923/G_18428.java b/src/S_0923/G_18428.java new file mode 100644 index 0000000..b9d8066 --- /dev/null +++ b/src/S_0923/G_18428.java @@ -0,0 +1,93 @@ +package S_0923; + + +import java.util.*; +import java.io.*; +import java.awt.*; +//12580kb 116ms +//감시피하기 +public class G_18428 { + static int N ; + static String map[][]; //맵 + static ArrayList S ; //학생위치 + + static String answer; + static int dx[] = {0,0,-1,1} , dy[] = {-1,1,0,0}; + public static void main(String[] args) throws Exception{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + N = Integer.parseInt(br.readLine()); + map = new String[N][N]; + S = new ArrayList(); + for (int i = 0; i < N; i++) { + String[] s = br.readLine().split(" "); + for (int j = 0; j < N; j++) { + map[i][j] = s[j]; + if(map[i][j].equals("S")){ + S.add(new Point(j, i)); //학생들 추가 + } + } + } + + answer = "NO"; + find(map,0); + + System.out.println(answer); + } + + + private static void find(String [][] map,int cnt) { + if(cnt == 3){ //cnt==3개되면 + if(yesCheck(map)){ //체크 + answer = "YES"; + } + return; + } + for (int i = 0; i < N; i++) { //맵 전부 돌면서 + for (int j = 0; j < N; j++) { + if(map[i][j].equals("X")){ //X일 때 + map[i][j] = "B"; //B로 만들고 + find(map, cnt + 1); //cnt+1 해서 재귀 태움 + map[i][j] = "X"; //원복 + } + } + } + } + + private static boolean yesCheck(String[][] map) { + + for (int i = 0; i < S.size(); i++) { + Point p = S.get(i); //학생들하나씩 꺼내가면서 + for (int j = 0; j < 4; j++) { + int x = p.x; + int y = p.y; + while (check(x, y)) { //범위 안에 있으면 + x = x + dx[j]; //끝까지 가보기 + y = y + dy[j]; + if(check(x,y) && map[y][x].equals("X")) continue; //X면 넘어가고 + if (check(x, y) && map[y][x].equals("T")) { //T면 안되고 + return false; + } + else if(check(x,y) && (map[y][x].equals("B") || map[y][x].equals("S")))break; //또 학생만나면 break; + + } + } + } + return true; + } + private static void Map(String map[][]){ + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + System.out.print(map[i][j] + " "); + } + System.out.println(); + } + System.out.println(); + } + + private static boolean check(int x, int y){ + if(x>=0 && y>=0 && x arr = new ArrayList<>(); + + public static void make(String num) { + if (!num.equals("")) { + arr.add(Long.parseLong(num)); + } + + for (int i = 0; i <= 9; i++) { + if (num.length() == 0 || num.charAt(num.length() - 1) - '0' > i) { + make(num + (char)('0' + i)); + } + } + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + make(""); + + Collections.sort(arr); + int n = sc.nextInt(); + + if (n > arr.size()) + System.out.println(-1); + else + System.out.println(arr.get(n-1)); + } +} \ No newline at end of file diff --git a/src/S_0930/G_1655.java b/src/S_0930/G_1655.java new file mode 100644 index 0000000..799009f --- /dev/null +++ b/src/S_0930/G_1655.java @@ -0,0 +1,42 @@ +package S_0930; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.Collections; +import java.util.PriorityQueue; + +//35132kb 384ms +public class G_1655 { + static int N; + public static void main(String[] args) throws Exception{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + N = Integer.parseInt(br.readLine()); + StringBuilder sb = new StringBuilder(); + + PriorityQueue high = new PriorityQueue<>(Collections.reverseOrder()); + PriorityQueue low = new PriorityQueue<>(); + + while (N-- > 0) { + + int temp = Integer.parseInt(br.readLine()); + //사이즈 0 이면 하이에 추가 || 하이 꼭대기보다 작거나 같으면 + if(high.size() == 0 || high.peek() >= temp){ + high.add(temp); + } + else{ + low.add(temp); + } + //크기가 2이상 차이나면 1차이 나게 만들어주기 + if(high.size() > low.size()+1){ + low.add(high.poll()); + } + + else if(low.size() > high.size()){ + high.add(low.poll()); + } + + sb.append(high.peek()).append("\n"); + } + System.out.println(sb); + } +} diff --git a/src/S_0930/G_16928.java b/src/S_0930/G_16928.java new file mode 100644 index 0000000..e640b34 --- /dev/null +++ b/src/S_0930/G_16928.java @@ -0,0 +1,55 @@ +package S_0930; + +import java.awt.*; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.HashSet; + +//11680kb 80ms +public class G_16928 { + static boolean visited []; + static int N,M; // N은 사다리 개수 , M 은 뱀의 수 + static HashSet nm; //뱀이랑 사다리 좌표 저장 + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String[] s = br.readLine().split(" "); + N = Integer.parseInt(s[0]); + M = Integer.parseInt(s[1]); + + nm = new HashSet(); + visited = new boolean[101]; + visited[1] = true; + + for (int i = 0; i < N + M; i++) { + s = br.readLine().split(" "); + int x = Integer.parseInt(s[0]); + int y = Integer.parseInt(s[1]); + nm.add(new Point(x,y)); //뱀 사다리 좌표 저장 + } + ArrayDeque dq = new ArrayDeque(); + dq.add(new Point(1,0)); //x는 위치 y는 카운트 값 저장 + while (!dq.isEmpty()) { + Point p = dq.poll(); + if(p.x == 100){ + System.out.println(p.y); //100일 때 카운트센거 출력 + return; + } + for (int i = 1; i <= 6; i++) { + int x = p.x + i; //1~6 + if(x >100) continue; //100아래일떄 + if(!visited[x]){ //방문안한곳있으면 + visited[x] = true; //방문처리 + Point next = new Point(x,p.y+1); //카운트 하나 세주고 + for (Point nxt: nm) { //nm에서 하나씩 꺼내보면서 좌표 같으면 + if(nxt.x == x){ + next.x = nxt.y; //바꿔주고 + break; + } + } + dq.add(next); //데크에 넣어주기 + } + } + } + } +} \ No newline at end of file diff --git a/src/S_0930/G_18809.java b/src/S_0930/G_18809.java new file mode 100644 index 0000000..dad8aa8 --- /dev/null +++ b/src/S_0930/G_18809.java @@ -0,0 +1,170 @@ +package S_0930; + +import java.awt.*; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.ArrayList; + +//299696kb 988ms +public class G_18809 { + //맵 + static int map[][]; + //N 세로 M 가로 g그린 개수, r 레드 개수 , 정답 + static int N, M, g, r,answer; + //꽃 설치 가능한곳 + static ArrayList can; + //조합 visited + static boolean visited[]; + //조합 뽑은거 저장 배열 + static Point[] green , red; + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String[] s = br.readLine().split(" "); + //입력 + N = Integer.parseInt(s[0]); + M = Integer.parseInt(s[1]); + g = Integer.parseInt(s[2]); + r = Integer.parseInt(s[3]); + + //초기화 + answer= 0 ; + map = new int[N][M]; + can = new ArrayList(); + green = new Point[g]; + red = new Point[r]; + + //맵입력받고 꽃설치 가능한 위치 저장 + for (int i = 0; i < N; i++) { + s = br.readLine().split(" "); + for (int j = 0; j < M; j++) { + map[i][j] = Integer.parseInt(s[j]); + if(map[i][j] == 2) can.add(new Point(j, i)); + } + } + + visited = new boolean[can.size()]; + Green(0,0); + System.out.println(answer); + } + + private static void Green(int cnt, int idx) { + //다 뽑으면 레드 뽑으러 가기 + if(cnt == g){ + Red(0,0); + return ; + } + //조합 뽑기 + for (int i = idx; i answer) answer = Math.max(answer, make); + return; + } + //조합 뽑기 + for (int i = idx; i < can.size(); i++) { + if(visited[i]) continue; + visited[i] = true; + red[cnt] = can.get(i); + Red(cnt + 1, i+ 1); + visited[i] = false; + } + } + + + private static int make() { + int dx[] = {-1,1,0,0}; + int dy[] = {0,0,-1,1}; + int temp [][] = new int[N][M]; + + //꽃 visited [y][x] = x,y,time,color + PointTime visited[][] = new PointTime[N][M]; + + //맵 복사 + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + temp[i][j] = map[i][j]; + } + } + ArrayDeque dq = new ArrayDeque(); + //그린 뽑아서 데크추가 + visited 추가 + for (Point g : green) { + dq.add(new PointTime(g,0,3)); + visited[g.y][g.x] = new PointTime(g.x, g.y, 0, 3); + } + //빨강 뽑아서 데크추가 + visited 추가 + for (Point r:red) { + dq.add(new PointTime(r,0,4)); + visited[r.y][r.x] = new PointTime(r.x, r.y, 0, 4); + } + //3 은 그린 4는 빨강 5는 꽃 핌 + while (!dq.isEmpty()) { + //뽑은다음에 + PointTime p = dq.poll(); + + //5면 꽃핀거니깐 continue; + if(temp[p.y][p.x] ==5) continue; + + //상하좌우 탐색 + for (int i = 0; i < 4; i++) { + int x = p.x + dx[i]; + int y = p.y + dy[i]; + //범위 안에 있을 때 + if (x >= 0 && x < M && y >= 0 && y < N) { + //아직 방문 안했고 물 아니면 + if(visited[y][x] == null && temp[y][x] != 0){ + //time +1 해서 방문처리 후 데크 추가 + visited[y][x] = new PointTime(x,y, p.time+1, p.color); + dq.add(new PointTime(x, y, p.time + 1, p.color)); + } + //방문했던 곳인데 꽃색깔 다르고 시간 1 차이나면 꽃핌 + else if(visited[y][x] != null &&visited[y][x].color != p.color && visited[y][x].time == p.time+1 ){ + temp[y][x] =5; + } + } + } + + } + //꽃세기 + int flowersCnt = 0; + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + if(temp[i][j] == 5){ + flowersCnt++; + } + } + } + return flowersCnt; + } + + public static class PointTime extends Point { + int time; + int color; + + public PointTime(Point p, int time, int color) { + super(p); + this.time = time; + this.color = color; + } + + public PointTime(int x, int y, int time, int color) { + super(x, y); + this.time = time; + this.color = color; + } + } + +} + diff --git a/src/S_1014/G_14499.java b/src/S_1014/G_14499.java new file mode 100644 index 0000000..7cb55f0 --- /dev/null +++ b/src/S_1014/G_14499.java @@ -0,0 +1,107 @@ +package S_1014; + +import java.awt.*; +import java.io.BufferedReader; +import java.io.InputStreamReader; + +//12148kb 92ms +public class G_14499 { + static int n, m, k; + static Point start; + static int dx[] = {0, 1, -1, 0, 0}, dy[] = {0, 0, 0, -1, 1}; + static int map[][]; + static int dice[]; + static StringBuilder sb; + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String[] s = br.readLine().split(" "); + sb = new StringBuilder(); + n = Integer.parseInt(s[0]); + m = Integer.parseInt(s[1]); + //왜그런지 모르겠는데 주사위 x, y 반대? + int x = Integer.parseInt(s[3]); + int y = Integer.parseInt(s[2]); + k = Integer.parseInt(s[4]); + start = new Point(x, y); + map = new int[n][m]; + dice = new int[6]; + for (int i = 0; i < n; i++) { + s = br.readLine().split(" "); + for (int j = 0; j < m; j++) { + map[i][j] = Integer.parseInt(s[j]); + } + } + //주사위를 어떻게할까? + s = br.readLine().split(" "); + for (int i = 0; i < k; i++) { + int command = Integer.parseInt(s[i]); + make(command); + } + System.out.println(sb); + } + + private static void make(int command) { + + int x = start.x + dx[command]; + int y = start.y + dy[command]; + //범위 안에 있을때 + if (check(x, y)) { + //주사위 굴리기 + roll(command); + //map == 0 일때 랑 아닐때 + if (map[y][x] == 0) { + map[y][x] = dice[5]; + } else { + dice[5] = map[y][x]; + map[y][x] = 0; + } + //윗면 sb에 추가하고 좌표 바꾸기 + sb.append(dice[0]).append("\n"); + start = new Point(x, y); + } + + } + + //주사위 굴리기 + private static void roll(int command) { + //서 + if (command == 1) { + diceSwap(0,2); + diceSwap(1,5); + diceSwap(1,2); + } + //남 + else if (command == 2) { + diceSwap(0, 1); + diceSwap(1, 5); + diceSwap(2, 5); + } + //북 + else if (command == 3) { + diceSwap(0, 3); + diceSwap(3, 5); + diceSwap(4, 5); + } + //남 + else if (command == 4) { + diceSwap(0, 4); + diceSwap(3, 4); + diceSwap(4, 5); + } + + } + //스왑 + private static void diceSwap(int to, int from) { + int temp = dice[to]; + dice[to] = dice[from]; + dice[from] = temp; + } + + //체크 + private static boolean check(int x, int y) { + if (x >= 0 && x < m && y >= 0 && y < n) { + return true; + } + return false; + } +} diff --git a/src/S_1014/G_1976.java b/src/S_1014/G_1976.java new file mode 100644 index 0000000..2c70544 --- /dev/null +++ b/src/S_1014/G_1976.java @@ -0,0 +1,55 @@ +package S_1014; + + +import java.io.BufferedReader; +import java.io.InputStreamReader; +//17464kb 152ms +public class G_1976 { + static int n, m; + static int map[][]; + static int tPlan[]; //여행계획 + static boolean visited[]; //해당도시 방문처리 + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + n = Integer.parseInt(br.readLine()); + m = Integer.parseInt(br.readLine()); + map = new int[n + 1][n+1]; + visited = new boolean[n + 1]; + //인접행렬 입력받기 + for (int i = 1; i <= n; i++) { + String[] s = br.readLine().split(" "); + for (int j = 1; j <=n ; j++) { + map[i][j] = Integer.parseInt(s[j-1]); + } + } + String[] s= br.readLine().split(" "); + tPlan = new int[s.length]; + //여행 계획 입력받기 + for (int i = 0; i < m; i++) { + tPlan[i] = Integer.parseInt(s[i]); + } + //처음껄로 dfs + find(tPlan[0]); + boolean flag = true; + //여행계획중에 방문 안한게 있으면 FALSE + for (int t : tPlan) { + if (!visited[t]) { + flag = false; + } + } + System.out.println(flag ? "YES" : "NO"); + } + + private static void find(int idx) { + visited[idx] = true; + + for (int i = 1; i <=n ; i++) { + //idx에서 i로 갈수 있는 도시가 있고 방문하지 않았다면 + if(map[idx][i] == 1 && !visited[i]){ + find(i); + } + } + } + +} diff --git a/src/S_1014/G_2629.java b/src/S_1014/G_2629.java new file mode 100644 index 0000000..d0deaee --- /dev/null +++ b/src/S_1014/G_2629.java @@ -0,0 +1,58 @@ +package S_1014; + +import java.io.BufferedReader; +import java.io.InputStreamReader; + +// 13872kb 124ms +public class G_2629 { + static int n,m; //n 추 개수 m 구슬 개수 + static int []chu; //츄 + static boolean [][]dp; //dp + public static void main(String[] args) throws Exception{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + n = Integer.parseInt(br.readLine()); + chu = new int[n]; + String[] s = br.readLine().split(" "); + + for (int i = 0; i < n; i++) { + chu[i] = Integer.parseInt(s[i]); + } + //추무게 최대 4만 + dp = new boolean[n+1][40001]; + dp[0][0] = true; + + for (int i = 1; i <= n; i++) { + //이전 추 꺼내서 + int temp = chu[i - 1]; + //무게만큼 돌면서 + for (int j = 0; j <=40000; j++) { + //이전추에 해당하는 무게가 참이다 + if(dp[i-1][j]){ + //지금고려한 i에 j에도 참 표시 + dp[i][j] = true; + //더한 값 4만보다 작으면 표시 + if(j+temp <=40000){ + dp[i][j+temp] =true; + } + //뺸 값 0보다 크면 표시 + int temp2 = Math.abs(j-temp); + if(temp2 >=0){ + dp[i][temp2] = true; + } + } + } + } + m = Integer.parseInt(br.readLine()); + s = br.readLine().split(" "); + //m만큼 돌면서 + for(int k=0;k40000||!dp[n][data]) + System.out.print("N "); + else + System.out.print("Y "); + } + } +} diff --git a/src/S_1014/S_18870.java b/src/S_1014/S_18870.java new file mode 100644 index 0000000..3c748ca --- /dev/null +++ b/src/S_1014/S_18870.java @@ -0,0 +1,58 @@ +package S_1014; + + +import java.awt.*; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.Comparator; +import java.util.PriorityQueue; + +//273372kb 1632ms +public class S_18870 { + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + //최대 100만번 배열꺼내서 출력하는데 스트링빌더 안쓰면 시간초과?? + StringBuilder sb = new StringBuilder(); + + //value 값으로 정렬 + PriorityQueue pq = new PriorityQueue(new Comparator() { + @Override + public int compare(Point o1, Point o2) { + return Integer.compare(o1.x,o2.x); + } + }); + //정답리스트 + int answerList[]; + //N 입력받기 + int N = Integer.parseInt(br.readLine()); + answerList = new int[N]; + + String[] s = br.readLine().split(" "); + for (int i = 0; i < N; i++) { + pq.add(new Point(Integer.parseInt(s[i]),i)); + } + //cnt + int cnt = 0; + int beforeInt = Integer.MIN_VALUE; + //작은수로 정렬된 것에서 같거나 다르거나 + while(!pq.isEmpty()){ + //가장 작은 수 부터 꺼내면서 + Point point = pq.poll(); + //같지 않으면 cnt 증가 후 가장 작은 수 갱신 + if(beforeInt != point.x){ + beforeInt = point.x; + answerList[point.y] = cnt++; + + } + //같으면 현재 카운트에서 -1한 값 저장 + else{ + answerList[point.y] = cnt-1; + } + } + for (int i = 0; i < N; i++) { + sb.append(answerList[i]).append(" "); + } + System.out.println(sb); + } + +} \ No newline at end of file diff --git a/src/S_1030/G_1580.java b/src/S_1030/G_1580.java new file mode 100644 index 0000000..4390898 --- /dev/null +++ b/src/S_1030/G_1580.java @@ -0,0 +1,103 @@ +package S_1030; + +import java.awt.*; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayDeque; + +public class G_1580 { + //N과 M입력받기 + static int N,M; + static char[][] map; + static PointAB pointab; + static int[][][][] visited; + static int[] dx = {-1,-1,-1,0,0,0,1,1,1}, dy = {-1,0,1,-1,0,1,-1,0,1}; + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + String[] s = br.readLine().split(" "); + N = Integer.parseInt(s[0]); + M = Integer.parseInt(s[1]); + + map = new char[N][M]; + visited = new int[N][M][N][M]; + + //a 위치 b 위치 이동횟수 저장하는 PointAB + pointab = new PointAB(new Point(0, 0), new Point(0, 0), 0); + + //입력받기 + for (int i = 0; i < N; i++) { + map[i] = br.readLine().toCharArray(); + for (int j = 0; j < M; j++) { + //a 위치 b위치 pointab에 저장 + if(map[i][j] == 'A') pointab.a = new Point(j, i); + if(map[i][j] == 'B') pointab.b = new Point(j, i); + } + } + + ArrayDeque dq = new ArrayDeque(); + dq.add(pointab); + visited[pointab.a.y][pointab.a.x][pointab.b.y][pointab.b.x] = 1; + + while (!dq.isEmpty()) { + //a, b 꺼냄 + PointAB cur = dq.poll(); + Point a = cur.a; + Point b = cur.b; + + //a가 b로 b가 a에 초기 위치로 갔다 -> 정답출력 + if(a.x == pointab.b.x && a.y == pointab.b.y && + b.x == pointab.a.x && b.y == pointab.a.y){ + System.out.println(cur.cnt); + return; + } + + //상하좌우, 대각선, 가만히있는거 9가지 이동 + for (int i = 0; i < 9; i++) { + + int ax = a.x + dx[i]; + int ay = a.y + dy[i]; + //범위안에있지않고 이동할 곳이 X면 continue + if(!check(ax, ay)) continue; + + for (int j = 0; j < 9; j++) { + + int bx = b.x + dx[j]; + int by = b.y + dy[j]; + //b가 이동하는것도 똑같이 체크한 후 + if(!check(bx, by)) continue; + //a가 이동한 곳과 b가 이동한 곳이 같다 continue + if(ax == bx && ay == by) continue; + //a가 이동할 곳이 이전 b가 있던 위치, b가 이동할 곳이 a가 이전에 있던 위치면 continue; + if(ax == b.x && ay == b.y && by == a.y && bx == a.x) continue; + + //아닐 경우 방문 처리후 dq에 추가 + if(visited[ay][ax][by][bx] == 0){ + visited[ay][ax][by][bx] = 1; + dq.add(new PointAB(new Point(ax, ay), new Point(bx, by), cur.cnt + 1)); + } + } + } + } + System.out.println(-1); + } + + public static boolean check(int x, int y){ + if(x >=0 && x=0 && y[] list; + static boolean visited[]; + static int[] answer; + public static void main(String[] args) throws Exception{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + n = Integer.parseInt(br.readLine()); + StringBuilder sb = new StringBuilder(); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + list = new ArrayList[n+1]; + visited = new boolean[n + 1]; + answer = new int[n + 1]; + for (int i = 0; i < n+1; i++) { + list[i] = new ArrayList(); + } + for (int i = 0; i < n-1; i++) { + String[] s = br.readLine().split(" "); + int a = Integer.parseInt(s[0]); + int b = Integer.parseInt(s[1]); + list[a].add(b); + list[b].add(a); + } + //트리 루트 1로 정했음 +// System.out.println(Arrays.toString(list)); + LinkedList q = new LinkedList(); +// q.add(1); +// bfs(q); +// for (int i = 2; i < n+1; i++) { +// System.out.println(answer[i]); +// } + dfs(1); + for (int i = 2; i < n+1; i++) { + sb.append(answer[i]).append("\n"); + } + bw.append(sb); + bw.close(); + } + +// private static void bfs(LinkedList queue) { +// while (!queue.isEmpty()) { +// int cur = queue.poll(); +// visited[cur] = true; +// for (int a : list[cur]) { +// if(answer[a] == 0) answer[a] = cur; +// if (!visited[a]) { +// queue.add(a); +// } +// } +// } +// } + private static void dfs(int vertex){ + if (!visited[vertex]) { + visited[vertex] = true; + for (int v : list[vertex]) { + if(answer[v] == 0) answer[v] =vertex; + if (!visited[v]) { + dfs(v); + } + } + } + + } + +} diff --git a/src/S_1240.java b/src/S_1240.java new file mode 100644 index 0000000..a4b42a4 --- /dev/null +++ b/src/S_1240.java @@ -0,0 +1,82 @@ +import java.awt.*; +import java.io.*; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Arrays; + +public class S_1240 { + static int n,m; + static ArrayList[] list; + static int answer=0; + static boolean visited[]; + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String[] s = br.readLine().split(" "); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + StringBuilder sb = new StringBuilder(); + + n = Integer.parseInt(s[0]); + m = Integer.parseInt(s[1]); + list = new ArrayList[n + 1]; + visited = new boolean[n + 1]; + for (int i = 0; i < n+1; i++) { + list[i] = new ArrayList(); + } + for (int i = 0; i < n-1; i++) { + s = br.readLine().split(" "); + int a = Integer.parseInt(s[0]); + int b = Integer.parseInt(s[1]); + int c = Integer.parseInt(s[2]); + list[a].add(new Point(b, c)); + list[b].add(new Point(a, c)); + } + for (int i = 0; i < m; i++) { + s = br.readLine().split(" "); + int a = Integer.parseInt(s[0]); + int b = Integer.parseInt(s[1]); + bfs(a, b); +// sb.append(answer).append("\n"); + Arrays.fill(visited, false); + answer= 0 ; + } +// bw.write(sb.toString()); +// bw.flush(); +// bw.close(); + } + + private static void bfs(int a, int b) { + ArrayDeque dq = new ArrayDeque(); + dq.add(new Point(a,0)); + int ans[] = new int[n + 1]; + while (!dq.isEmpty()) { + Point p = dq.poll(); + visited[p.x] = true; + + for (Point temP: list[p.x]) { + if (!visited[temP.x]) { + ans[temP.x] = temP.y+ans[p.x]; + if (temP.x == b) { + System.out.println(ans[b]); + return ; + } + dq.add(temP); + } + + } + } + } + + +// private static void dfs(int a, int b, int d) { +// if (a == b) { +// answer = d; +// return; +// } +// visited[a] = true; +// for (Point p : list[a]) { +// if (!visited[p.x]) { +// dfs(p.x,b,d+p.y); +// } +// } +// } +} diff --git a/src/S_1260.java b/src/S_1260.java new file mode 100644 index 0000000..418a84a --- /dev/null +++ b/src/S_1260.java @@ -0,0 +1,68 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.*; + +public class S_1260 { + static int n,m,v; + static List[] map; + static boolean []visited; + public static void main(String[] args)throws Exception { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String[] s = br.readLine().split(" "); + n = Integer.parseInt(s[0]); + m = Integer.parseInt(s[1]); + v = Integer.parseInt(s[2]); + + visited = new boolean[n+1]; + map = new ArrayList[n + 1]; + for (int i = 0; i (); + } + for (int i = 0; i < m; i++) { + s = br.readLine().split(" "); + int a = Integer.parseInt(s[0]); + int b = Integer.parseInt(s[1]); + map[a].add(b); + map[b].add(a); + } + for (int i = 0; i < map.length; i++) { + Collections.sort(map[i]); + } + dfs(v); + System.out.println(); + Arrays.fill(visited, false); + bfs(v); + + } + + private static void bfs(int v) { + ArrayDeque q = new ArrayDeque(); + q.add(v); + System.out.print(v+" "); + visited[v] = true; + while(!q.isEmpty()){ + int temp = q.pollFirst(); + for (int s :map[temp]) { + if(!visited[s]){ + System.out.print(s+" "); + visited[s] = true; + q.addLast(s); + } + } + } + + } + + private static void dfs(int v) { + System.out.print(v+" "); + visited[v] = true; + for (int i = 0; i < map[v].size(); i++) { + if (!visited[map[v].get(i)]) { + visited[map[v].get(i)] = true; + dfs(map[v].get(i)); + } + } + } + +} diff --git a/src/S_14501.java b/src/S_14501.java new file mode 100644 index 0000000..8600c11 --- /dev/null +++ b/src/S_14501.java @@ -0,0 +1,79 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.Arrays; + +public class S_14501 { + static int N,answer; + static int data[][]; + static int day[],profit[]; + + public static void main(String[] args) throws Exception{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + N = Integer.parseInt(br.readLine()); + + day = new int[N]; + profit = new int[N]; + int [] dp = new int[N+1]; + for (int i = 0; i < N; i++) { + String[] s = br.readLine().split(" "); + int a = Integer.parseInt(s[0]); //날 + int b = Integer.parseInt(s[1]); //수익 + + day[i] = a; + profit[i] = b; + + } + answer = 0; + + for (int i = 0; i <= N; i++) { + for (int j = 0; j < i; j++) { + dp[i] = Math.max(dp[i], dp[j]); + if (j + day[j] == i) { //현재 날짜 기준(i) == 이전 날짜 중(j) + 이전 날짜에서 일했을 경우 걸릴 시간 + dp[i] = Math.max(dp[j] + profit[j], dp[i]); //수익난거 더한거와 선택안했을 경우 가장 큰 값 선택 + } + } + } +// System.out.println(Arrays.toString(dp)); + System.out.println(dp[N]); + + + } +} +//public class Main { 재귀 풀이 +// static int N,answer; +// static int data[][]; +// public static void main(String[] args) throws Exception{ +// BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); +// N = Integer.parseInt(br.readLine()); +// data = new int[N][2]; +// +// for (int i = 0; i < N; i++) { +// String[] s = br.readLine().split(" "); +// int a = Integer.parseInt(s[0]); //날 +// int b = Integer.parseInt(s[1]); //수익 +// data[i][0] = a; +// data[i][1] = b; +// } +// answer = 0; +// find(0,0); +// +// System.out.println(answer); +// +// +// } +// +// private static void find(int day, int profit) { +// if(day >= N){ +// if(answer < profit){ +// answer = Math.max(profit,answer); +// } +// return; +// } +// if(day + data[day][0]<=N ){ +// find(day+data[day][0], profit+ data[day][1]); +// } +// find(day+1,profit); +// +// +// } +//} diff --git a/src/S_1874.java b/src/S_1874.java new file mode 100644 index 0000000..c2a91b6 --- /dev/null +++ b/src/S_1874.java @@ -0,0 +1,39 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.Stack; + +// 29008ms 296kb +public class S_1874 { + static int n; + static Stack st; + public static void main(String[] args) throws Exception{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringBuilder sb = new StringBuilder(); + n = Integer.parseInt(br.readLine()); + st = new Stack<>(); + boolean flag = true; + int first = 0; + for (int i = 0; i < n; i++) { + int temp = Integer.parseInt(br.readLine()); + + if (first < temp) { + for (int j = first+1; j <= temp; j++) { + st.push(j); + sb.append("+").append("\n"); + } + first = temp; + + } else if (st.peek() != temp) { + flag = false; + break; + } + st.pop(); + sb.append("-").append("\n"); + + } + if(flag) System.out.println(sb); + else System.out.println("NO"); + + + } +} diff --git a/src/S_1946.java b/src/S_1946.java new file mode 100644 index 0000000..0a94258 --- /dev/null +++ b/src/S_1946.java @@ -0,0 +1,50 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; + +public class S_1946 { + static int T, N ; + static ArrayList data; + static ArrayDeque real; + public static void main(String[] args) throws Exception{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + T = Integer.parseInt(br.readLine()); + + for (int _ = 0; _ < T; _++) { + N = Integer.parseInt(br.readLine()); + data = new ArrayList(); + for (int i = 0; i < N; i++) { + String[] s = br.readLine().split(" "); + int a = Integer.parseInt(s[0]); + int b = Integer.parseInt(s[1]); + data.add(new int[]{a,b}); + } + + Collections.sort(data, new Comparator() { + @Override + public int compare(int[] o1, int[] o2) { + return Integer.compare(o1[0],o2[0]); + } + }); + real = new ArrayDeque(data); + int cnt =1; + + if(!real.isEmpty()){ + int top = real.poll()[1]; + while(!real.isEmpty()){ + if(real.peek()[1]< top){ + cnt+=1; + top = real.peek()[1]; + } + real.poll(); + } + + } + System.out.println(cnt); + } + } + +} diff --git a/src/S_2178.java b/src/S_2178.java new file mode 100644 index 0000000..99cd96c --- /dev/null +++ b/src/S_2178.java @@ -0,0 +1,56 @@ +import java.awt.*; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayDeque; + +// 13648kb 152ms +public class S_2178 { + static int[] dy = {-1, 1, 0, 0}; + static int[] dx = {0, 0, 1, -1}; + static int N,M; + static int map[][], map2[][]; + static boolean visited[][]; + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String[] s = br.readLine().split(" "); + N = Integer.parseInt(s[0])+1; + M = Integer.parseInt(s[1])+1; + + map = new int[N][M]; + map2 = new int[N][M]; + visited = new boolean[N][M]; + for (int i = 1; i < N; i++) { + s = br.readLine().split(""); + for (int j = 1; j < M; j++) { + map[i][j] = Integer.parseInt(s[j-1]); + } + } + bfs(1,1); + System.out.println(map2[N-1][M-1]+1); + + } + + private static void bfs(int x, int y) { + Point p = new Point(x, y); + visited[y][x] = true; + ArrayDeque dq=new ArrayDeque(); + dq.add(p); + while (!dq.isEmpty()) { + Point point = dq.pollFirst(); + int tempDx = point.x; + int tempDy = point.y; + for (int i = 0; i < 4; i++) { + int tempDx2 = tempDx + dx[i]; + int tempDy2 = tempDy + dy[i]; + if ( 1 <= tempDx2 && tempDx2 < M && 1 <= tempDy2 && tempDy2 < N && map[tempDy2][tempDx2] ==1) { + if (!visited[tempDy2][tempDx2]) { + map2[tempDy2][tempDx2] += map[tempDy2][tempDx2] + map2[tempDy][tempDx]; + visited[tempDy2][tempDx2] = true; + dq.add(new Point(tempDx2, tempDy2)); + } + } + } + } + } +} diff --git a/src/S_2847.java b/src/S_2847.java new file mode 100644 index 0000000..e5da080 --- /dev/null +++ b/src/S_2847.java @@ -0,0 +1,43 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; + +public class S_2847{ + + static int N,r,c; + static int answer; + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String[] s = br.readLine().split(" "); + N = Integer.parseInt(s[0]); + r = Integer.parseInt(s[1]); + c = Integer.parseInt(s[2]); + answer = 0; + find(0,0,(int)Math.pow(2,N),0); + System.out.println(answer); + br.close(); + } + + private static void find(int x, int y, int size, int cnt) { + if ((x + size - 1 < r || x > r) &&(y + size - 1 < c || y > c)) return; + if(size ==2){ + int temp = 0; + for (int i = y; i