Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c2d77c7

Browse files
Merge pull request #138 from icegosimperson/main
[10주차] 이혜원
2 parents dd15be9 + 49e10d1 commit c2d77c7

File tree

9 files changed

+556
-0
lines changed

9 files changed

+556
-0
lines changed

‎BOJ/1000-5000번/HW_1613.java‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class HW_1613 {
5+
public static void main(String[] args) throws IOException{
6+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
StringTokenizer st = new StringTokenizer(br.readLine());
8+
int N = Integer.parseInt(st.nextToken());
9+
int K = Integer.parseInt(st.nextToken());
10+
11+
boolean[][] visited = new boolean[N+1][N+1]; // 방문 여부만 확인
12+
for(int i=0; i<K; i++) {
13+
st = new StringTokenizer(br.readLine());
14+
int a = Integer.parseInt(st.nextToken());
15+
int b = Integer.parseInt(st.nextToken());
16+
visited[a][b] = true; // 전후 관계 알고 있음
17+
}
18+
19+
for(int k=1; k<=N; k++) {
20+
for(int i=1; i<=N; i++) {
21+
for(int j=1; j<=N; j++){
22+
if(visited[i][k] && visited[k][j]) {
23+
visited[i][j] = true;
24+
}
25+
}
26+
}
27+
}
28+
29+
int S = Integer.parseInt(br.readLine()); // 사건의 전후 관계를 알고 싶은 사건 쌍의 수
30+
for(int i=0; i<S; i++) {
31+
st = new StringTokenizer(br.readLine());
32+
int a = Integer.parseInt(st.nextToken());
33+
int b = Integer.parseInt(st.nextToken());
34+
35+
if(visited[a][b]) {
36+
System.out.println(-1);
37+
}
38+
else if(visited[b][a]) {
39+
System.out.println(1);
40+
} else System.out.println(0);
41+
}
42+
}
43+
}
44+

‎BOJ/1000-5000번/HW_2096.java‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
public static void main(String[] args) throws IOException {
6+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
int N = Integer.parseInt(br.readLine());
8+
9+
int[][] dp = new int[N][3];
10+
for (int i = 0; i < N; i++) {
11+
StringTokenizer st = new StringTokenizer(br.readLine());
12+
for (int j = 0; j < 3; j++) {
13+
dp[i][j] = Integer.parseInt(st.nextToken());
14+
}
15+
}
16+
17+
int[][] min = new int[N][3];
18+
int[][] max = new int[N][3];
19+
20+
for (int i = 0; i < 3; i++) {
21+
min[0][i] = dp[0][i];
22+
max[0][i] = dp[0][i];
23+
}
24+
25+
for (int i = 1; i < N; i++) {
26+
max[i][0] = Math.max(max[i-1][0], max[i-1][1]) + dp[i][0];
27+
max[i][1] = Math.max(Math.max(max[i-1][0], max[i-1][1]), max[i-1][2]) + dp[i][1];
28+
max[i][2] = Math.max(max[i-1][1], max[i-1][2]) + dp[i][2];
29+
30+
min[i][0] = Math.min(min[i-1][0], min[i-1][1]) + dp[i][0];
31+
min[i][1] = Math.min(Math.min(min[i-1][0], min[i-1][1]), min[i-1][2]) + dp[i][1];
32+
min[i][2] = Math.min(min[i-1][1], min[i-1][2]) + dp[i][2];
33+
}
34+
35+
int maxValue = Math.max(Math.max(max[N-1][0], max[N-1][1]), max[N-1][2]);
36+
int minValue = Math.min(Math.min(min[N-1][0], min[N-1][1]), min[N-1][2]);
37+
38+
System.out.println(maxValue + " " + minValue);
39+
}
40+
}

‎BOJ/1000-5000번/HW_2955.java‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import java.io.*;
2+
3+
public class HW_2955 {
4+
static int[][] board;
5+
public static boolean[] row, col;
6+
public static boolean[][] square;
7+
static boolean error = false;
8+
9+
public static void main(String[] args) throws Exception {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
12+
board = new int[9][9];
13+
for (int i = 0; i < 9; i++) {
14+
String line = br.readLine();
15+
for (int j = 0; j < 9; j++) {
16+
if (line.charAt(j) == '.') {
17+
board[i][j] = 0;
18+
} else board[i][j] = line.charAt(j) - '0';
19+
}
20+
}
21+
22+
while (true) {
23+
if (!solve()) {
24+
break;
25+
}
26+
}
27+
28+
if (error) {
29+
System.out.println("ERROR");
30+
} else {
31+
for (int i = 0; i < 9; i++) {
32+
for (int j = 0; j < 9; j++) {
33+
if(board[i][j] == 0){
34+
System.out.print(".");
35+
} else System.out.print(board[i][j]);
36+
}
37+
System.out.println();
38+
}
39+
}
40+
}
41+
42+
public static boolean solve() {
43+
for (int i = 1; i <= 9; i++) {
44+
if (crossHatching(i)) {
45+
return true;
46+
}
47+
}
48+
return false;
49+
}
50+
51+
public static boolean crossHatching(int x) {
52+
row = new boolean[9];
53+
col = new boolean[9];
54+
square = new boolean[3][3];
55+
56+
for (int i = 0; i < 9; i++) {
57+
for (int j = 0; j < 9; j++) {
58+
if (board[i][j] == x) {
59+
if (row[i] || col[j] || square[i / 3][j / 3]) {
60+
error = true;
61+
return false;
62+
}
63+
row[i] = true;
64+
col[j] = true;
65+
square[i / 3][j / 3] = true;
66+
}
67+
}
68+
}
69+
for (int i = 0; i < 3; i++) {
70+
for (int j = 0; j < 3; j++) {
71+
if (!square[i][j]) {
72+
if (check(i * 3, j * 3, x)) {
73+
return true;
74+
}
75+
}
76+
}
77+
}
78+
return false;
79+
}
80+
81+
public static boolean check(int curX, int curY, int x) { // x : 배치하려는 숫자
82+
int nx = -1;
83+
int ny = -1;
84+
int cnt = 0;
85+
86+
for (int i = 0; i < 3; i++) {
87+
for (int j = 0; j < 3; j++) {
88+
if (board[curX + i][curY + j] == 0 && !row[curX + i] && !col[curY + j]) {
89+
if (cnt == 0) {
90+
nx = curX + i;
91+
ny = curY + j;
92+
cnt++;
93+
} else {
94+
return false;
95+
}
96+
}
97+
}
98+
}
99+
100+
if (cnt == 0) {
101+
error = true;
102+
return false;
103+
}
104+
105+
board[nx][ny] = x;
106+
row[nx] = true;
107+
col[ny] = true;
108+
109+
return true;
110+
}
111+
}
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class HW_회전하는_빙하 {
5+
static class Node {
6+
int x, y;
7+
public Node(int x, int y) {
8+
this.x = x;
9+
this.y = y;
10+
}
11+
}
12+
13+
static int n, q, L;
14+
static int[][] board, nextboard;
15+
static boolean[][] visited;
16+
static int[] dx = {0, 1, -1, 0}; // 오른쪽, 아래, 위, 왼쪽
17+
static int[] dy = {1, 0, 0, -1};
18+
static int max = 0;
19+
20+
public static void main(String[] args) throws Exception {
21+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
22+
StringTokenizer st = new StringTokenizer(br.readLine());
23+
24+
n = Integer.parseInt(st.nextToken()); // 격자의 크기 (2^n * 2^n)
25+
q = Integer.parseInt(st.nextToken()); // 회전 횟수
26+
L = (int) Math.pow(2, n);
27+
board = new int[L][L];
28+
nextboard = new int[L][L];
29+
30+
// 보드 입력
31+
for (int i = 0; i < L; i++) {
32+
st = new StringTokenizer(br.readLine());
33+
for (int j = 0; j < L; j++) {
34+
board[i][j] = Integer.parseInt(st.nextToken());
35+
}
36+
}
37+
38+
// 회전 레벨 입력
39+
st = new StringTokenizer(br.readLine());
40+
while (q-- > 0) {
41+
int lv = Integer.parseInt(st.nextToken());
42+
if (lv > 0) rotate(lv);
43+
melt();
44+
}
45+
46+
// 1. 남아있는 빙하의 총량 계산
47+
int sum = 0;
48+
for (int i = 0; i < L; i++) {
49+
for (int j = 0; j < L; j++) {
50+
sum += board[i][j];
51+
}
52+
}
53+
System.out.println(sum);
54+
55+
// 2. 가장 큰 얼음 군집 크기 계산
56+
visited = new boolean[L][L];
57+
max = 0;
58+
for (int i = 0; i < L; i++) {
59+
for (int j = 0; j < L; j++) {
60+
if (!visited[i][j] && board[i][j] > 0) {
61+
max = Math.max(max, bfs(i, j));
62+
}
63+
}
64+
}
65+
System.out.println(max);
66+
}
67+
68+
// 초기화
69+
public static void initialize() {
70+
for (int i = 0; i < L; i++) {
71+
for (int j = 0; j < L; j++) {
72+
nextboard[i][j] = 0;
73+
}
74+
}
75+
}
76+
77+
78+
public static void rotate(int level) {
79+
initialize(); // `nextboard` 초기화
80+
int boxSize = (int) Math.pow(2, level);
81+
int halfSize = (int) Math.pow(2, level - 1);
82+
83+
for (int i = 0; i < L; i += boxSize) { // 시계 방향 90도 회전
84+
for (int j = 0; j < L; j += boxSize) {
85+
move(i, j, halfSize, 0);
86+
move(i, j + halfSize, halfSize, 1);
87+
move(i + halfSize, j, halfSize, 2);
88+
move(i + halfSize, j + halfSize, halfSize, 3);
89+
}
90+
}
91+
92+
for (int i = 0; i < L; i++) {
93+
for (int j = 0; j < L; j++) {
94+
board[i][j] = nextboard[i][j]; // 배열 복사
95+
}
96+
}
97+
}
98+
99+
// size`만큼의 하위 격자를 시계 방향으로 회전
100+
public static void move(int startX, int startY, int size, int dir) {
101+
for (int i = startX; i < startX + size; i++) {
102+
for (int j = startY; j < startY + size; j++) {
103+
int nx = i + dx[dir] * size;
104+
int ny = j + dy[dir] * size;
105+
if (!isValid(nx, ny)) continue;
106+
nextboard[nx][ny] = board[i][j];
107+
}
108+
}
109+
}
110+
111+
public static void melt() {
112+
initialize();
113+
114+
for (int i = 0; i < L; i++) {
115+
for (int j = 0; j < L; j++) {
116+
if (board[i][j] != 0 && iceCnt(i, j) < 3) {
117+
nextboard[i][j] = board[i][j] - 1; // -1
118+
} else {
119+
nextboard[i][j] = board[i][j];
120+
}
121+
}
122+
}
123+
124+
for (int i = 0; i < L; i++) {
125+
for (int j = 0; j < L; j++) {
126+
board[i][j] = nextboard[i][j];
127+
}
128+
}
129+
}
130+
131+
public static int iceCnt(int x, int y) {
132+
int cnt = 0;
133+
for (int d = 0; d < 4; d++) {
134+
int nx = x + dx[d];
135+
int ny = y + dy[d];
136+
137+
if (isValid(nx, ny) && board[nx][ny] != 0) {
138+
cnt++;
139+
}
140+
}
141+
return cnt;
142+
}
143+
144+
// BFS : 얼음 군집 크기를 계산
145+
public static int bfs(int x, int y) {
146+
int size = 1;
147+
Queue<int[]> queue = new LinkedList<>();
148+
queue.offer(new int[]{x, y});
149+
visited[x][y] = true;
150+
151+
while (!queue.isEmpty()) {
152+
int[] pos = queue.poll();
153+
154+
for (int d = 0; d < 4; d++) {
155+
int nx = pos[0] + dx[d];
156+
int ny = pos[1] + dy[d];
157+
158+
if (isValid(nx, ny) && !visited[nx][ny] && board[nx][ny] > 0) {
159+
visited[nx][ny] = true;
160+
queue.offer(new int[]{nx, ny});
161+
size++;
162+
}
163+
}
164+
}
165+
return size;
166+
}
167+
168+
public static boolean isValid(int x, int y) {
169+
return x >= 0 && y >= 0 && x < L && y < L;
170+
}
171+
}

‎Programmers/Level2/HW_181188.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.*;
2+
// 구하는 것 : 모든 폭격 미사일을 요격하기 위해 필요한 요격 미사일 수의 최솟값
3+
class Solution {
4+
public int solution(int[][] targets) {
5+
Arrays.sort(targets, (a, b) -> Integer.compare(a[1], b[1])); // e구간 기준 정렬
6+
7+
int cnt = 0; // 요격 미사일 개수
8+
double endShoot = Integer.MIN_VALUE; // 미사일 쏘는 위치
9+
10+
for(int i=0; i<targets.length; i++){
11+
int s = targets[i][0]; // s
12+
int e = targets[i][1]; // e
13+
14+
if(endShoot<s){ // 마지막 발사 위치보다 현재 시작 구간이 더 크면
15+
cnt++; // 새로운 미사일 설치
16+
endShoot = e-0.5; // 개구간 = 범위 포함X : 임의로 실수를 빼줌
17+
}
18+
}
19+
return cnt;
20+
}
21+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /