Skip to content

Commit f716f10

Browse files
authored
Merge pull request #206 from icegosimperson/main
[15μ£Όμ°¨] μ΄ν˜œμ›
2 parents bc13faa + 87a02a3 commit f716f10

File tree

9 files changed

+422
-0
lines changed

9 files changed

+422
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
// ν”„λ ‰νƒˆ 평면이 λ‹¨μœ„ μ‹œκ°„μ΄ 진행될 λ•Œ λ§ˆλ‹€ 크기가 증가함
5+
// ν”„λž™νƒˆ ν•œλ³€ 길이 : N^S
6+
// 검정색 μ˜μ—­μ΄ μ•„λ‹Œ 경우 색을 κ²°μ •ν•  수 μ—†μŒ
7+
// ν˜„μž¬ μ‹œκ°„μ—μ„œ ν•΄λ‹Ή μœ„μΉ˜ μ–΄λ–€ 색인지 κ²°μ • X
8+
// 색을 κ²°μ •ν•˜λ €λ©΄? 더 μž‘μ€ 블둝(μƒμœ„ νŒ¨ν„΄)의 μ’Œν‘œλ‘œ μ΄λ™ν•΄μ„œ μž¬κ·€μ μœΌλ‘œ 확인 ν•΄μ€˜μ•Όν•¨
9+
public class HW_1030 {
10+
static int S, N, K, R1, R2, C1, C2;
11+
public static void main(String[] args) throws IOException {
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
StringTokenizer st = new StringTokenizer(br.readLine());
14+
15+
S = Integer.parseInt(st.nextToken()); // μ‹œκ°„
16+
N = Integer.parseInt(st.nextToken()); // λΆ„ν•  크기
17+
K = Integer.parseInt(st.nextToken()); // κ²€μ • μ˜μ—­ 크기
18+
R1 = Integer.parseInt(st.nextToken());
19+
R2 = Integer.parseInt(st.nextToken());
20+
C1 = Integer.parseInt(st.nextToken());
21+
C2 = Integer.parseInt(st.nextToken());
22+
23+
int size = (int) Math.pow(N, S); // ν”„λž™νƒˆ ν•œλ³€ 길이 : N^S
24+
25+
// μ£Όμ–΄μ§„ λ²”μœ„λ§Œ 확인
26+
for (int r = R1; r <= R2; r++) {
27+
for (int c = C1; c <= C2; c++) {
28+
System.out.print(isCheck(r, c, size)); // 각 μ’Œν‘œμ˜ 색 계산
29+
}
30+
System.out.println();
31+
}
32+
}
33+
34+
static int isCheck(int r, int c, int size) {
35+
if (size == 1) { // λ‹¨μœ„ μ‹œκ°„ 0일 λ•Œ
36+
return 0; // 흰색(0) μ •μ‚¬κ°ν˜• ν•œ 개
37+
}
38+
39+
int curSize = size / N; // ν˜„μž¬ 블둝 크기
40+
int s = curSize * (N - K) / 2; // κ²€μ • μ˜μ—­ μ‹œμž‘
41+
int e = curSize * (N + K) / 2; // κ²€μ • μ˜μ—­ 끝
42+
43+
// ν˜„μž¬ μ’Œν‘œκ°€ κ²€μ • μ˜μ—­μ— μ†ν•˜λ©΄ 색을 λ°”λ‘œ λ°˜ν™˜(1)
44+
if (s <= r && r < e && s <= c && c < e) {
45+
return 1;
46+
}
47+
48+
// κ²€μ • μ˜μ—­μ— μ†ν•˜μ§€ μ•ŠμœΌλ©΄
49+
// μ’Œν‘œλ₯Ό μƒμœ„ λΈ”λ‘μœΌλ‘œ λ³€ν™˜ν•˜μ—¬ μž¬κ·€μ μœΌλ‘œ 확인
50+
return isCheck(r % curSize, c % curSize, curSize);
51+
}
52+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
import java.io.*;
3+
import java.util.*;
4+
5+
// N<= 10^5 μ‹œκ°„ λ³΅μž‘λ„ : O(N)
6+
public class HW_1725 {
7+
public static void main(String[] args) throws IOException {
8+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
int N = Integer.parseInt(br.readLine());
10+
int[] heights = new int[N+1];
11+
12+
for(int i=0; i<N; i++) {
13+
heights[i] = Integer.parseInt(br.readLine());
14+
}
15+
System.out.println(simulation(heights));
16+
}
17+
static long simulation(int[] heights) {
18+
Stack<Integer> stack = new Stack<>(); // λ§‰λŒ€ 높이λ₯Ό μ €μž₯
19+
long max = 0; // λ§‰λŒ€μ˜ 높이(10^9) * λ„ˆλΉ„(10^5) -> long
20+
21+
for(int i=0; i<heights.length; i++) { // μ§μ‚¬κ°ν˜•μ˜ μ΅œλŒ€ 높이 계산
22+
while(!stack.isEmpty() && heights[i] < heights[stack.peek()]) { // ν˜„μž¬ 높이가 μŠ€νƒμ˜ μ΅œμƒλ‹¨μ— μžˆλŠ” 높이보닀 μž‘μœΌλ©΄
23+
int h = heights[stack.pop()]; // μŠ€νƒμ—μ„œ 높이λ₯Ό 꺼냄
24+
int w = stack.isEmpty() ? i : i - stack.peek()-1;
25+
// μŠ€νƒμ΄ λΉ„μ–΄μžˆλŠ” 경우 -> ν˜„μž¬ 인덱슀(i)κ°€ 넓이 w=i;
26+
// μŠ€νƒμ΄ λΉ„μ–΄μžˆμ§€X 경우 -> μ™Όμͺ½ 경계(μ™Όμͺ½μ— λ‚¨μ•„μžˆλŠ” λ§‰λŒ€) w = i - stack.peek()-1;
27+
// ν˜„μž¬ λ§‰λŒ€μ™€μ˜ κ°„κ²©λ§ŒνΌλ§Œ μ§μ‚¬κ°ν˜•μ„ ν™•μž₯ν•  수 있기 λ•Œλ¬Έ
28+
max = Math.max(max, (long)h*w);// κΊΌλ‚Έ 높이 κΈ°μ€€μœΌλ‘œ 넓이 계산
29+
}
30+
stack.push(i);
31+
}
32+
return max;
33+
}
34+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.*;
5+
6+
public class HW_1992 {
7+
static int N;
8+
static char[][] board;
9+
static StringBuilder sb = new StringBuilder();
10+
public static void main(String[] args) throws IOException {
11+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
13+
int N = Integer.parseInt(br.readLine());
14+
board = new char[N][N];
15+
for(int i=0; i<N; i++){
16+
String line = br.readLine();
17+
board[i] = line.toCharArray();
18+
}
19+
Quad(0, 0, N);
20+
System.out.println(sb);
21+
}
22+
static void Quad(int x, int y, int size){
23+
if(isValid(x, y, size)){
24+
sb.append(board[x][y]);
25+
return;
26+
}
27+
int half = size/2;
28+
29+
sb.append('(');
30+
Quad(x, y, half); // μ™Όμͺ½ μœ„
31+
Quad(x, y+half, half);
32+
Quad(x+half, y, half);
33+
Quad(x+half, y+half, half);
34+
sb.append(')');
35+
}
36+
static boolean isValid(int x, int y, int size){ // μ••μΆ• κ°€λŠ₯ν•œμ§€ 확인
37+
char value = board[x][y]; // 첫번째 κ°’ κΈ°μ€€
38+
// 0 + 1 μ„žμ—¬μžˆμœΌλ©΄ 4개 μ˜μ—­μœΌλ‘œ λ‚˜λˆ μ•Όν•¨
39+
for(int i=x; i<x+size; i++){
40+
for(int j=y; j<y+size; j++){
41+
if(value != board[i][j]){
42+
return false; // ν•œ λ¬ΈμžλΌλ„ λ‹€λ₯΄λ©΄ false
43+
}
44+
}
45+
}
46+
return true; // λͺ¨λ‘ 같은 값일 경우 true
47+
}
48+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
// μ‹œκ°„λ³΅μž‘λ„ : O(V+E)
5+
public class HW_4803 {
6+
static List<List<Integer>> graph;
7+
static boolean[] visited;
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
int test = 1;
11+
12+
while(true){
13+
StringTokenizer st = new StringTokenizer(br.readLine());
14+
int N = Integer.parseInt(st.nextToken());
15+
int M = Integer.parseInt(st.nextToken());
16+
17+
if(N == 0 && M == 0) {
18+
break;
19+
}
20+
graph = new ArrayList<>();
21+
for(int i=0; i<=N; i++) {
22+
graph.add(new ArrayList<>());
23+
}
24+
25+
for(int i=0; i<M; i++) { // κ°„μ„  μž…λ ₯ λ°›κΈ°
26+
st = new StringTokenizer(br.readLine());
27+
int u = Integer.parseInt(st.nextToken());
28+
int v = Integer.parseInt(st.nextToken());
29+
graph.get(u).add(v); // μ–‘λ°©ν–₯
30+
graph.get(v).add(u);
31+
}
32+
visited = new boolean[N+1];
33+
int cnt = 0;
34+
35+
for(int i=1; i<=N; i++) {
36+
if(!visited[i]) {
37+
if(isCheck(i, -1)) {
38+
cnt++;
39+
}
40+
}
41+
}
42+
43+
if(cnt==0) {
44+
System.out.printf("Case %d: No trees.", test);
45+
}
46+
else if(cnt==1) {
47+
System.out.printf("Case %d: There is one tree.", test);
48+
}
49+
else {
50+
System.out.printf("Case %d: A forest of %d trees.", test, cnt);
51+
}
52+
System.out.println();
53+
test++;
54+
}
55+
}
56+
static boolean isCheck(int node, int parent) { // BFSλ₯Ό 톡해 트리 μ—°κ²° λΆ€λΆ„ 확인
57+
Queue<int[]> queue = new LinkedList<>(); // 큐 μ΄ˆκΈ°ν™”
58+
queue.add(new int[] {node, parent}); // {탐색 μ‹œμž‘ν•  정점, node의 λΆ€λͺ¨ 정점}
59+
visited[node] = true;
60+
int nodeCnt = 0; // μ—°κ²° μš”μ†Œμ—μ„œ λ°©λ¬Έν•œ 정점 개수 cnt
61+
int edgeCnt = 0; // μ—°κ²° μš”μ†Œμ—μ„œ λ°©λ¬Έν•œ κ°„μ„  개수 cnt
62+
63+
while(!queue.isEmpty()) {
64+
int[] cur = queue.poll(); // νμ—μ„œ ν˜„μž¬ λ…Έλ“œ, λΆ€λͺ¨ λ…Έλ“œλ₯Ό κ°€μ Έμ˜΄
65+
int curNode = cur[0]; // ν˜„μž¬ λ…Έλ“œ
66+
int curParent = cur[1]; // λΆ€λͺ¨ λ…Έλ“œ
67+
68+
nodeCnt++; // 정점 개수++
69+
70+
for(int i=0; i<graph.get(curNode).size(); i++) { // 인접 λ…Έλ“œ 탐색
71+
int neighbor = graph.get(curNode).get(i);
72+
edgeCnt++; // κ°„μ„  cnt++
73+
74+
if(!visited[neighbor]) { // λ°©λ¬Έν•˜μ§€ μ•Šμ€ λ…Έλ“œλ₯Ό 큐에 μΆ”κ°€ν•˜κ³ 
75+
visited[neighbor] = true; // λ°©λ¬Έ ν‘œμ‹œ
76+
queue.add(new int[] {neighbor, curNode});
77+
} else if(neighbor != curParent) { // 사이클 λ°œμƒ(이미 λ°©λ¬Έν•œ λ…Έλ“œκ°€ λΆ€λͺ¨ λ…Έλ“œκ°€ μ•„λ‹ˆλΌλ©΄)
78+
return false; // 트리X
79+
}
80+
}
81+
}
82+
return edgeCnt/2 == nodeCnt -1; // μ–‘λ°©ν–₯ κ°„μ„  κ³ λ € endge/2, 트리의 κ°„μ„  개수 쑰건 V-1
83+
}
84+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.*;
5+
6+
// μ£Όμ–΄μ§€λŠ” λΉ„ν–‰ μŠ€μΌ€μ€„μ€ 항상 μ—°κ²° κ·Έλž˜ν”„λ₯Ό 이룸 -> MST(μ΅œμ†Œ μŠ€νŒ¨λ‹νŠΈλ¦¬)
7+
// λͺ¨λ“  κ΅­κ°€λ₯Ό μ—°κ²°ν•˜λŠ”λ° ν•„μš”ν•œ μ΅œμ„  κ°„μ„ μ˜ 수 μ°ΎκΈ°
8+
// μ—°κ²°λœ κ·Έλž˜ν”„μ—μ„œ MST κ°„μ„ μˆ˜ : N-1개
9+
public class HW_9372 {
10+
public static void main(String[] args) throws IOException {
11+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
StringTokenizer st;
13+
int T = Integer.parseInt(br.readLine());
14+
15+
for(int i=0; i<T; i++){
16+
st = new StringTokenizer(br.readLine());
17+
int N = Integer.parseInt(st.nextToken()); // κ΅­κ°€μ˜ 수
18+
int M = Integer.parseInt(st.nextToken()); // λΉ„ν–‰κΈ°μ˜ 수
19+
for(int j=0; j<M; j++){
20+
br.readLine(); // 각 λΉ„ν–‰ 경둜 μž…λ ₯만 λ°›μŒ(a, b)
21+
}
22+
System.out.println(N-1);
23+
}
24+
}
25+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import java.util.*;
2+
import java.io.*;
3+
4+
// 미리 계산을 ν•˜μ—¬ μ£Όμ–΄μ§„ 이동 νšŸμˆ˜μ— λ‚˜κ°ˆ 말의 μ’…λ₯˜λ₯Ό 잘 μ‘°ν•©ν•˜μ—¬ 얻을 수 μžˆλŠ” 점수의 μ΅œλŒ“κ°’
5+
// μ£Όμ–΄μ§„ 이동 μΉΈ 수λ₯Ό μ΄μš©ν•΄μ„œ 얻을 수 μžˆλŠ” 점수의 μ΅œλŒ“κ°’μ„ 좜λ ₯
6+
// νŒŒλž€ 칸에 도착 μ‹œ μ΅œλŒ€ 이동 횟수 μ΄λ™ν•˜κΈ°
7+
public class HW_μœ·λ†€μ΄_사기단 {
8+
static int[] board = {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40,
9+
13, 16, 19, 22, 24, 28, 27, 26, 25, 30, 35, 0}; // 32μΉΈ
10+
static int[] horse = {0, 0, 0, 0}; // 말 4개
11+
static int[][] move = new int[33][6]; // 말의 이동 경둜 μ €μž₯
12+
static int max = 0;
13+
static int[] input;
14+
public static void main(String[] args) throws IOException{
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
StringTokenizer st = new StringTokenizer(br.readLine());
17+
input = new int[10];
18+
19+
for(int i=0; i<10; i++){
20+
input[i] = Integer.parseInt(st.nextToken());
21+
}
22+
23+
simulation();
24+
dfs(0, 0, horse);
25+
System.out.println(max);
26+
}
27+
private static void simulation(){
28+
// μœ·λ†€μ΄νŒ 이동 경둜 μ„€μ •
29+
for(int i=0; i<20; i++){ // 직선 경둜 이동 처리
30+
for(int j=1; j<=5; j++){ // 윷 놀이 1~5μΉΈ 이동 κ°€λŠ₯
31+
move[i][j] = i+j;
32+
}
33+
}
34+
35+
// νŒŒλž€μΉΈ 도착 μ‹œ 이동 경둜 μ„€μ •
36+
move[5] = new int[]{0, 21, 22, 23, 29, 30}; // 0~5μΉΈ 이동
37+
move[10] = new int[]{0, 24, 25, 29, 30, 31};
38+
move[15] = new int[]{0, 26, 27, 28, 29, 30};
39+
40+
// 직선 경둜 μ œμ™Έ 이동 경둜 μ„€μ •
41+
move[21] = new int[]{0, 22, 23, 29, 30, 31}; // 13~
42+
move[22] = new int[]{0, 23, 29, 30, 31, 20};
43+
move[23] = new int[]{0, 29, 30, 31, 20, 32}; // ~19
44+
45+
move[24] = new int[]{0, 25, 29, 30, 31, 20};
46+
move[25] = new int[]{0, 29, 30, 31, 20, 32};
47+
move[26] = new int[]{0, 27, 28, 29, 30, 31};
48+
move[27] = new int[]{0, 28, 29, 30, 31, 20};
49+
move[28] = new int[]{0, 29, 30, 31, 20, 32};
50+
move[29] = new int[]{0, 30, 31, 20, 32, 32};
51+
move[30] = new int[]{0, 31, 20, 32, 32, 32};
52+
move[31] = new int[]{0, 20, 32, 32, 32, 32};
53+
54+
// 도착 지점 경둜 μ„€μ •
55+
move[16][5] = 32;
56+
move[17] = new int[]{0, 18, 19, 20, 32, 32};
57+
move[18] = new int[]{0, 19, 20, 32, 32, 32};
58+
move[19] = new int[]{0, 20, 32, 32, 32, 32};
59+
move[20] = new int[]{0, 32, 32, 32, 32, 32};
60+
}
61+
static void dfs(int moveCnt, int score, int[] horse){
62+
if(moveCnt==10){ // 10번 이동 μ™„λ£Œ μ‹œ
63+
max = Math.max(max, score);
64+
return;
65+
}
66+
for(int i=0; i<4; i++){
67+
int cur = horse[i];
68+
if(cur==32){ // λ„μ°©ν•˜λ©΄ μ›€μ§μ΄μ§€μ•ŠμŒ
69+
continue;
70+
}
71+
int next = move[cur][input[moveCnt]]; // λ‹€μŒ μœ„μΉ˜ 계산
72+
boolean flag = true;
73+
for(int j=0; j<4; j++){ // κ²ΉμΉ˜μ§€ μ•Šμ•„μ•Όν•¨
74+
if(i!=j && horse[j]==next && next !=32){ // λ„μ°©μ‹œ 움직이지 μ•ŠμŒ
75+
flag = false;
76+
break;
77+
}
78+
}
79+
if(!flag){
80+
continue;
81+
}
82+
horse[i] = next; // 말 이동
83+
dfs(moveCnt+1, score+board[next], horse);
84+
horse[i] = cur; // μƒνƒœ 볡원?
85+
}
86+
87+
}
88+
}

0 commit comments

Comments
Β (0)