Skip to content

Commit f88732e

Browse files
committed
Update
1 parent 6745b7c commit f88732e

14 files changed

+1090
-0
lines changed

12/1.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
public static String str;
6+
public static int summary = 0;
7+
8+
public static void main(String[] args) {
9+
Scanner sc = new Scanner(System.in);
10+
str = sc.next();
11+
12+
// 왼쪽 부분의 자릿수의 합 더하기
13+
for (int i = 0; i < str.length() / 2; i++) {
14+
summary += str.charAt(i) - '0';
15+
}
16+
17+
// 오른쪽 부분의 자릿수의 합 빼기
18+
for (int i = str.length() / 2; i < str.length(); i++) {
19+
summary -= str.charAt(i) - '0';
20+
}
21+
22+
// 왼쪽 부분과 오른쪽 부분의 자릿수 합이 동일한지 검사
23+
if (summary == 0) System.out.println("LUCKY");
24+
else System.out.println("READY");
25+
}
26+
}

12/2.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
public static String str;
6+
public static ArrayList<Character> result = new ArrayList<Character>();
7+
public static int value = 0;
8+
9+
public static void main(String[] args) {
10+
Scanner sc = new Scanner(System.in);
11+
str = sc.next();
12+
13+
// 문자를 하나씩 확인하며
14+
for (int i = 0; i < str.length(); i++) {
15+
// 알파벳인 경우 결과 리스트에 삽입
16+
if (Character.isLetter(str.charAt(i))) {
17+
result.add(str.charAt(i));
18+
}
19+
// 숫자는 따로 더하기
20+
else {
21+
value += str.charAt(i) - '0';
22+
}
23+
}
24+
25+
// 알파벳을 오름차순으로 정렬
26+
Collections.sort(result);
27+
28+
// 알파벳을 차례대로 출력
29+
for (int i = 0; i < result.size(); i++) {
30+
System.out.print(result.get(i));
31+
}
32+
33+
// 숫자가 하나라도 존재하는 경우 가장 뒤에 출력
34+
if (value != 0) System.out.print(value);
35+
System.out.println();
36+
}
37+
}

12/3.java

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
5+
public int solution(String s) {
6+
int answer = s.length();
7+
// 1개 단위(step)부터 압축 단위를 늘려가며 확인
8+
for (int step = 1; step < s.length() / 2 + 1; step++) {
9+
String compressed = "";
10+
String prev = s.substring(0, step); // 앞에서부터 step만큼의 문자열 추출
11+
int cnt = 1;
12+
// 단위(step) 크기만큼 증가시키며 이전 문자열과 비교
13+
for (int j = step; j < s.length(); j += step) {
14+
// 이전 상태와 동일하다면 압축 횟수(count) 증가
15+
String sub = "";
16+
for (int k = j; k < j + step; k++) {
17+
if (k < s.length()) sub += s.charAt(k);
18+
}
19+
if (prev.equals(sub)) cnt += 1;
20+
// 다른 문자열이 나왔다면(더 이상 압축하지 못하는 경우라면)
21+
else {
22+
compressed += (cnt >= 2)? cnt + prev : prev;
23+
sub = "";
24+
for (int k = j; k < j + step; k++) {
25+
if (k < s.length()) sub += s.charAt(k);
26+
}
27+
prev = sub; // 다시 상태 초기화
28+
cnt = 1;
29+
}
30+
}
31+
// 남아있는 문자열에 대해서 처리
32+
compressed += (cnt >= 2)? cnt + prev : prev;
33+
// 만들어지는 압축 문자열이 가장 짧은 것이 정답
34+
answer = Math.min(answer, compressed.length());
35+
}
36+
return answer;
37+
}
38+
}

12/4.java

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
5+
// 2차원 리스트 90도 회전하기
6+
public static int[][] rotateMatrixBy90Degree(int[][] a) {
7+
int n = a.length;
8+
int m = a[0].length;
9+
int[][] result = new int[n][m]; // 결과 리스트
10+
for (int i = 0; i < n; i++) {
11+
for (int j = 0; j < m; j++) {
12+
result[j][n - i - 1] = a[i][j];
13+
}
14+
}
15+
return result;
16+
}
17+
18+
// 자물쇠의 중간 부분이 모두 1인지 확인
19+
public static boolean check(int[][] newLock) {
20+
int lockLength = newLock.length / 3;
21+
for (int i = lockLength; i < lockLength * 2; i++) {
22+
for (int j = lockLength; j < lockLength * 2; j++) {
23+
if (newLock[i][j] != 1) {
24+
return false;
25+
}
26+
}
27+
}
28+
return true;
29+
}
30+
31+
public boolean solution(int[][] key, int[][] lock) {
32+
int n = lock.length;
33+
int m = key.length;
34+
// 자물쇠의 크기를 기존의 3배로 변환
35+
int[][] newLock = new int[n * 3][n * 3];
36+
// 새로운 자물쇠의 중앙 부분에 기존의 자물쇠 넣기
37+
for (int i = 0; i < n; i++) {
38+
for (int j = 0; j < n; j++) {
39+
newLock[i + n][j + n] = lock[i][j];
40+
}
41+
}
42+
43+
// 4가지 방향에 대해서 확인
44+
for (int rotation = 0; rotation < 4; rotation++) {
45+
key = rotateMatrixBy90Degree(key); // 열쇠 회전
46+
for (int x = 0; x < n * 2; x++) {
47+
for (int y = 0; y < n * 2; y++) {
48+
// 자물쇠에 열쇠를 끼워 넣기
49+
for (int i = 0; i < m; i++) {
50+
for (int j = 0; j < m; j++) {
51+
newLock[x + i][y + j] += key[i][j];
52+
}
53+
}
54+
// 새로운 자물쇠에 열쇠가 정확히 들어 맞는지 검사
55+
if (check(newLock)) return true;
56+
// 자물쇠에서 열쇠를 다시 빼기
57+
for (int i = 0; i < m; i++) {
58+
for (int j = 0; j < m; j++) {
59+
newLock[x + i][y + j] -= key[i][j];
60+
}
61+
}
62+
}
63+
}
64+
}
65+
return false;
66+
}
67+
}

12/5.java

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import java.util.*;
2+
3+
class Node {
4+
5+
private int time;
6+
private char direction;
7+
8+
public Node(int time, char direction) {
9+
this.time = time;
10+
this.direction = direction;
11+
}
12+
13+
public int getTime() {
14+
return this.time;
15+
}
16+
17+
public char getDirection() {
18+
return this.direction;
19+
}
20+
}
21+
22+
class Position {
23+
24+
private int x;
25+
private int y;
26+
27+
public Position(int x, int y) {
28+
this.x = x;
29+
this.y = y;
30+
}
31+
32+
public int getX() {
33+
return this.x;
34+
}
35+
36+
public int getY() {
37+
return this.y;
38+
}
39+
}
40+
41+
public class Main {
42+
43+
public static int n, k, l;
44+
public static int[][] arr = new int[101][101]; // 맵 정보
45+
public static ArrayList<Node> info = new ArrayList<>(); // 방향 회전 정보
46+
47+
// 처음에는 오른쪽을 보고 있으므로(동, 남, 서, 북)
48+
public static int dx[] = {0, 1, 0, -1};
49+
public static int dy[] = {1, 0, -1, 0};
50+
51+
public static int turn(int direction, char c) {
52+
if (c == 'L') direction = (direction == 0)? 3 : direction - 1;
53+
else direction = (direction + 1) % 4;
54+
return direction;
55+
}
56+
57+
public static int simulate() {
58+
int x = 1, y = 1; // 뱀의 머리 위치
59+
arr[x][y] = 2; // 뱀이 존재하는 위치는 2로 표시
60+
int direction = 0; // 처음에는 동쪽을 보고 있음
61+
int time = 0; // 시작한 뒤에 지난 '초' 시간
62+
int index = 0; // 다음에 회전할 정보
63+
// 뱀이 차지하고 있는 위치 정보(꼬리가 앞쪽)
64+
Queue<Position> q = new LinkedList<>();
65+
q.offer(new Position(x, y));
66+
67+
while (true) {
68+
int nx = x + dx[direction];
69+
int ny = y + dy[direction];
70+
// 맵 범위 안에 있고, 뱀의 몸통이 없는 위치라면
71+
if (1 <= nx && nx <= n && 1 <= ny && ny <= n && arr[nx][ny] != 2) {
72+
// 사과가 없다면 이동 후에 꼬리 제거
73+
if (arr[nx][ny] == 0) {
74+
arr[nx][ny] = 2;
75+
q.offer(new Position(nx, ny));
76+
Position prev = q.poll();
77+
arr[prev.getX()][prev.getY()] = 0;
78+
}
79+
// 사과가 있다면 이동 후에 꼬리 그대로 두기
80+
if (arr[nx][ny] == 1) {
81+
arr[nx][ny] = 2;
82+
q.offer(new Position(nx, ny));
83+
}
84+
}
85+
// 벽이나 뱀의 몸통과 부딪혔다면
86+
else {
87+
time += 1;
88+
break;
89+
}
90+
// 다음 위치로 머리를 이동
91+
x = nx;
92+
y = ny;
93+
time += 1;
94+
if (index < l && time == info.get(index).getTime()) { // 회전할 시간인 경우 회전
95+
direction = turn(direction, info.get(index).getDirection());
96+
index += 1;
97+
}
98+
}
99+
return time;
100+
}
101+
102+
public static void main(String[] args) {
103+
Scanner sc = new Scanner(System.in);
104+
105+
n = sc.nextInt();
106+
k = sc.nextInt();
107+
108+
// 맵 정보(사과 있는 곳은 1로 표시)
109+
for (int i = 0; i < k; i++) {
110+
int a = sc.nextInt();
111+
int b = sc.nextInt();
112+
arr[a][b] = 1;
113+
}
114+
115+
// 방향 회전 정보 입력
116+
l = sc.nextInt();
117+
for (int i = 0; i < l; i++) {
118+
int x = sc.nextInt();
119+
char c = sc.next().charAt(0);
120+
info.add(new Node(x, c));
121+
}
122+
123+
System.out.println(simulate());
124+
}
125+
126+
}

17/1.java

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
public static final int INF = (int) 1e9; // 무한을 의미하는 값으로 10억을 설정
6+
// 노드의 개수(N), 간선의 개수(M)
7+
public static int n, m;
8+
// 2차원 배열(그래프 표현)를 만들기
9+
public static int[][] graph = new int[101][101];
10+
11+
public static void main(String[] args) {
12+
Scanner sc = new Scanner(System.in);
13+
14+
n = sc.nextInt();
15+
m = sc.nextInt();
16+
17+
// 최단 거리 테이블을 모두 무한으로 초기화
18+
for (int i = 0; i < 101; i++) {
19+
Arrays.fill(graph[i], INF);
20+
}
21+
22+
// 자기 자신에서 자기 자신으로 가는 비용은 0으로 초기화
23+
for (int a = 1; a <= n; a++) {
24+
for (int b = 1; b <= n; b++) {
25+
if (a == b) graph[a][b] = 0;
26+
}
27+
}
28+
29+
// 각 간선에 대한 정보를 입력 받아, 그 값으로 초기화
30+
for (int i = 0; i < m; i++) {
31+
// A에서 B로 가는 비용은 C라고 설정
32+
int a = sc.nextInt();
33+
int b = sc.nextInt();
34+
int c = sc.nextInt();
35+
// 가장 짧은 간선 정보만 저장
36+
if (c < graph[a][b]) graph[a][b] = c;
37+
}
38+
39+
// 점화식에 따라 플로이드 워셜 알고리즘을 수행
40+
for (int k = 1; k <= n; k++) {
41+
for (int a = 1; a <= n; a++) {
42+
for (int b = 1; b <= n; b++) {
43+
graph[a][b] = Math.min(graph[a][b], graph[a][k] + graph[k][b]);
44+
}
45+
}
46+
}
47+
48+
// 수행된 결과를 출력
49+
for (int a = 1; a <= n; a++) {
50+
for (int b = 1; b <= n; b++) {
51+
// 도달할 수 없는 경우, 무한(INFINITY)이라고 출력
52+
if (graph[a][b] == INF) {
53+
System.out.print(0 + " ");
54+
}
55+
// 도달할 수 있는 경우 거리를 출력
56+
else {
57+
System.out.print(graph[a][b] + " ");
58+
}
59+
}
60+
System.out.println();
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)