Skip to content

[권혁준-12주차 알고리즘 스터디] #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions 권혁준_12주차/[BOJ-11049] 행렬 곱셈 순서.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
```java

import java.util.*;
import java.io.*;

class Main {

// IO field
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st = new StringTokenizer("");

static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
static String nextToken() throws Exception {
while(!st.hasMoreTokens()) nextLine();
return st.nextToken();
}
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
static void bwEnd() throws Exception {bw.flush();bw.close();}

// Additional field

static int N;
static int[][] dp;
static int[][] A;

public static void main(String[] args) throws Exception {

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

N = nextInt();
A = new int[N][2];
for(int i=0;i<N;i++) for(int j=0;j<2;j++) A[i][j] = nextInt();
dp = new int[N][N];

}

static void solve() throws Exception{

for(int k=1;k<N;k++) {
for(int s=0;s+k<N;s++) {
dp[s][s+k] = Integer.MAX_VALUE;
for(int m=s;m<s+k;m++) {
dp[s][s+k] = Math.min(dp[s][s+k], dp[s][m] + dp[m+1][s+k] + A[s][0] * A[m][1] * A[s+k][1]);
}
}
}
bw.write(dp[0][N-1] + "\n");

}

}

```
101 changes: 101 additions & 0 deletions 권혁준_12주차/[BOJ-1327] 소트 게임.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
```java

import java.util.*;
import java.io.*;

class Main {

// IO field
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st = new StringTokenizer("");

static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
static String nextToken() throws Exception {
while(!st.hasMoreTokens()) nextLine();
return st.nextToken();
}
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
static void bwEnd() throws Exception {bw.flush();bw.close();}

// Additional field

static class Node{
String per;
int time;
Node(String per, int time){
this.per = per;
this.time = time;
}
}

static int N, K;
static int[] A;
static TreeSet<String> T = new TreeSet<>();

public static void main(String[] args) throws Exception {

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

N = nextInt();
K = nextInt();
A = new int[N];
for(int i=0;i<N;i++) A[i] = nextInt();

}

static void solve() throws Exception{

int[] B = new int[N];
for(int i=0;i<N;i++) B[i] = A[i];
Arrays.sort(B);
String endstr = tostr(B);

T.add(tostr(A));
Queue<Node> Q = new LinkedList<>();
Q.offer(new Node(tostr(A), 0));
while(!Q.isEmpty()) {
Node now = Q.poll();
String cur = now.per;
int t = now.time;
if(cur.equals(endstr)) {
bw.write(t + "\n");
return;
}

int[] arr = new int[N];
for(int i=0;i<cur.length();i+=2) arr[i/2] = cur.charAt(i)-'0';
for(int i=0;i<=N-K;i++) {
int[] tmp = new int[N];
for(int j=0;j<i;j++) tmp[j] = arr[j];
for(int j=i+K;j<N;j++) tmp[j] = arr[j];
for(int j=i,e=i+K-1;j<i+K;j++) tmp[j] = arr[e--];
String next = tostr(tmp);
if(T.contains(next)) continue;
T.add(next);
Q.offer(new Node(next, t+1));
}

}
bw.write("-1");

}

static String tostr(int[] a) {
String res = "";
for(int i:a) res += i + " ";
return res;
}

}

```
38 changes: 38 additions & 0 deletions 권혁준_12주차/[BOJ-1405] 미친 로봇.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
```python

K, E, W, N, S = map(int, input().split())

vis = [[0 for _ in range(30)] for _ in range(30)]
cnt = 0
dp = [E,W,N,S]
dx = [1,-1,0,0]
dy = [0,0,1,-1]

def dfs(x, y, c, p):
global vis
global cnt
global dp
global dx
global dy
if c == K:
cnt += p
return
for i in range(4):
xx, yy = x+dx[i], y+dy[i]
if vis[xx][yy] != 0:
continue
vis[xx][yy]+=1
dfs(xx,yy,c+1,p*dp[i])
vis[xx][yy]-=1


vis[15][15]+=1
for i in range(4):
x, y = 15+dx[i], 15+dy[i]
vis[x][y]+=1
dfs(x,y,1,dp[i])
vis[x][y]-=1

print(cnt / (100**K))

```
88 changes: 88 additions & 0 deletions 권혁준_12주차/[BOJ-21278] 호석이 두 마리 치킨.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
```java

import java.util.*;
import java.io.*;

class Main {

// IO field
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st = new StringTokenizer("");

static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
static String nextToken() throws Exception {
while(!st.hasMoreTokens()) nextLine();
return st.nextToken();
}
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
static void bwEnd() throws Exception {bw.flush();bw.close();}

// Additional field

static int N, M;
static boolean[][] V;
static int[][] dist;

static final int INF = (int)1e9 + 7;

public static void main(String[] args) throws Exception {

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

N = nextInt();
M = nextInt();
dist = new int[N+1][N+1];
V = new boolean[N+1][N+1];
for(int i=0;i<M;i++) {
int a = nextInt(), b = nextInt();
V[a][b] = V[b][a] = true;
}
for(int i=1;i<=N;i++) Arrays.fill(dist[i], INF);

}

static void solve() throws Exception{

for(int i=1;i<=N;i++) {
Queue<int[]> Q = new LinkedList<>();
boolean[] vis = new boolean[N+1];
Q.offer(new int[] {i,0});
vis[i] = true;
while(!Q.isEmpty()) {
int[] now = Q.poll();
int n = now[0], t = now[1];
dist[i][n] = t;
for(int j=1;j<=N;j++) if(V[n][j] && !vis[j]) {
vis[j] = true;
Q.offer(new int[] {j,t+1});
}
}
}

int ans = INF, A = -1, B = -1;
for(int i=1;i<=N;i++) for(int j=i+1;j<=N;j++) {
int res = 0;
for(int k=1;k<=N;k++) res += Math.min(dist[k][i], dist[k][j]);
if(res < ans) {
ans = res;
A = i;
B = j;
}
}
bw.write(A + " " + B + " " + (ans*2));

}

}

```
63 changes: 63 additions & 0 deletions 권혁준_12주차/[BOJ-2629] 양팔저울.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
```java

import java.util.*;
import java.io.*;

class Main {

// IO field
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st = new StringTokenizer("");

static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
static String nextToken() throws Exception {
while(!st.hasMoreTokens()) nextLine();
return st.nextToken();
}
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
static void bwEnd() throws Exception {bw.flush();bw.close();}

// Additional field

static int N, M;
static TreeSet<Integer> S = new TreeSet<>();

public static void main(String[] args) throws Exception {

ready();
solve();

bwEnd();

}

static void ready() throws Exception{

N = nextInt();

}

static void solve() throws Exception{

S.add(0);
for(int i=0;i<N;i++) {
int a = nextInt();
TreeSet<Integer> NS = new TreeSet<>();
for(int s:S) {
NS.add(s+a);
NS.add(Math.abs(s-a));
}
for(int ns:NS) S.add(ns);
NS.clear();
}

for(M = nextInt();M-->0;) bw.write(S.contains(nextInt()) ? "Y " : "N ");

}

}

```
Loading