Skip to content

[김유성-12주차 알고리즘 스터디] #61

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 4 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
45 changes: 45 additions & 0 deletions 김유성-12주차/Main1405_미친로봇.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package study12week;

import java.util.Scanner;

public class Main1405_미친로봇 {

static int N, move[][] = {{0 ,-1}, {0, 1}, {1, 0}, {-1, 0}};//동, 서, 남, 북 (좌, 우, 하, 상)
static double ewsn[] = new double[4], value;
static boolean[][] visited;

public static void main(String[] args) {
init();
visited[14][14] = true;
dfs(14, 14, 0, 1);
System.out.println(value);
}

static void dfs(int h, int w, int cnt, double val) {
if (cnt == N) {
value += val;
return;
}

int nh, nw;
for (int i = 0; i < 4; i++) {
nh = h + move[i][0];
nw = w + move[i][1];

if (!visited[nh][nw]) {
visited[nh][nw] = true;
dfs(nh, nw, cnt + 1, val * ewsn[i]);
visited[nh][nw] = false;
}
}
}

static void init() {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
for (int i = 0; i < 4; i++) {
ewsn[i] = sc.nextInt() * 0.01;
}
visited = new boolean[30][30];
}
}
74 changes: 74 additions & 0 deletions 김유성-12주차/Main21278_호석이두마리치킨.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {

static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;

static int N, M, dist[][], INF = 1_000_000_000;

public static void main(String[] args) throws IOException {
init();
solve();
}

static void solve() {
for (int k = 1; k <= N; k++) {
for (int i = 1; i <= N; i++) {
if (i == k) continue;
for (int j = 1; j <= N; j++) {
if (i == j || j == k) continue;

dist[i][j] = Math.min(dist[i][k] + dist[k][j], dist[i][j]);
}
}
}
int min = Integer.MAX_VALUE;
StringBuilder sb = new StringBuilder("");
for (int i = 1; i < N; i++) {
for (int j = i + 1; j <= N; j++) {
int ret = 0;

for (int k = 1; k <= N; k++) {
if (i == k || j == k) continue;

if (dist[i][k] <= dist[j][k]) {
ret += dist[i][k];
} else {
ret += dist[j][k];
}
}
if (min > ret * 2) {
min = ret * 2;
sb = new StringBuilder(i + " " + j + " " + min);
}
}
}
System.out.println(sb.toString());
}

static void init() throws IOException {
st = new StringTokenizer(br.readLine().trim());

N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());

dist = new int[N + 1][N + 1];
for (int i = 0; i <= N; i++) {
Arrays.fill(dist[i], INF);
}

for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine().trim());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());

dist[a][b] = 1;
dist[b][a] = 1;
}
}
}
52 changes: 52 additions & 0 deletions 김유성-12주차/Main2629_양팔저울.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package study12week;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main2629_양팔저울 {

static int N, M, sum = 0, base[]; // N: 추의 개수, M: 무게를 확인하고자 하는 구슬의 개수
static boolean dp[][];
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;

public static void main(String[] args) throws IOException {
init();
}

static void init() throws IOException {
N = Integer.parseInt(br.readLine().trim());
base = new int[N + 1];
st = new StringTokenizer(br.readLine().trim());

for (int i = 1; i <= N; i++) {
base[i] = Integer.parseInt(st.nextToken());
sum += base[i];
}
dp = new boolean[N + 1][sum + 1];
dp(0, 0);

M = Integer.parseInt(br.readLine().trim());
st = new StringTokenizer(br.readLine().trim());
for (int i = 0; i < M; i++) {
int ball = Integer.parseInt(st.nextToken());
if (ball <= sum && dp[N][ball])
System.out.print("Y ");
else
System.out.print("N ");
}
}

static void dp(int index, int weight) {
if (dp[index][weight]) return;

dp[index][weight] = true;
if (index == N) return;

dp(index + 1, weight); // 현재 무게에 해당 추를 추가하지 않음
dp(index + 1, weight + base[index + 1]); // 현재 무게에 해당 추를 추가한 경우
dp(index + 1, Math.abs(weight - base[index + 1])); // 현재 무게에서 해당 추를 뺀 경우
}
}
162 changes: 162 additions & 0 deletions 김유성-12주차/Main7682_틱택토.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

public class Main {

static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;

static char[][] map = new char[3][3];
static int X, O;
static boolean win, visited[][] = new boolean[3][3];
static int move[][] = { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, -1 }, { 0, 1 }, { 1, -1 }, { 1, 0 }, { 1, 1 } };
static List<int[]> X_hw;
static List<int[]> O_hw;

public static void main(String[] args) throws IOException {
while (init()) {
if (solve()) {
System.out.println("valid");
} else {
System.out.println("invalid");
}
}
}

static void dfs(int h, int w, int cnt, char c, int dir) {
if (cnt == 3) {
win = true;
return;
}

int nh, nw;
nh = h + move[dir][0];
nw = w + move[dir][1];

if (nh >= 0 && nw >= 0 && nh < 3 && nw < 3 && !visited[nh][nw] && map[nh][nw] == c) {
dfs(nh, nw, cnt + 1, c, dir);
}
}

static boolean solve() {
if (X - O == 0) { // O가 이김
// o가 이겨야 되는데 x가 이기면 false
for (int[] x : X_hw) {
int nh, nw;
for (int i = 0; i < 8; i++) {
nh = x[0] + move[i][0];
nw = x[1] + move[i][1];

if (nh >= 0 && nw >= 0 && nh < 3 && nw < 3 && !visited[nh][nw] && map[nh][nw] == 'X') {
dfs(nh, nw, 2, 'X', i);
if (win == true)
return false;
}
}
}

// o가 이긴경우가 맞으면 true
for (int[] o : O_hw) {
int nh, nw;
for (int i = 0; i < 8; i++) {
nh = o[0] + move[i][0];
nw = o[1] + move[i][1];

if (nh >= 0 && nw >= 0 && nh < 3 && nw < 3 && !visited[nh][nw] && map[nh][nw] == 'O') {
dfs(nh, nw, 2, 'O', i);
if (win == true)
return true;
}
}
}

} else if (X - O == 1) { // X가 이김 or 게임 끝남
if (X + O == 9) {
for (int[] o : O_hw) {
int nh, nw;
for (int i = 0; i < 8; i++) {
nh = o[0] + move[i][0];
nw = o[1] + move[i][1];

if (nh >= 0 && nw >= 0 && nh < 3 && nw < 3 && !visited[nh][nw] && map[nh][nw] == 'O') {
dfs(nh, nw, 2, 'O', i);
if (win == true)
return false;
}
}
}
return true;
} else {
for (int[] o : O_hw) {
int nh, nw;
for (int i = 0; i < 8; i++) {
nh = o[0] + move[i][0];
nw = o[1] + move[i][1];

if (nh >= 0 && nw >= 0 && nh < 3 && nw < 3 && !visited[nh][nw] && map[nh][nw] == 'O') {
dfs(nh, nw, 2, 'O', i);
if (win == true)
return false;
}
}
}

for (int[] x : X_hw) {
int nh, nw;
for (int i = 0; i < 8; i++) {
nh = x[0] + move[i][0];
nw = x[1] + move[i][1];

if (nh >= 0 && nw >= 0 && nh < 3 && nw < 3 && !visited[nh][nw] && map[nh][nw] == 'X') {
dfs(nh, nw, 2, 'X', i);
if (win == true)
return true;
}
}
}
}
}
return false;
}

static boolean init() throws IOException {
String input = br.readLine().trim();
if (input.equals("end"))
return false;

X_hw = new LinkedList<>();
O_hw = new LinkedList<>();
X = 0;
O = 0;
win = false;

int len = 0;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
map[i][j] = input.charAt(len++);
if (map[i][j] == 'O') {
O_hw.add(new int[] { i, j });
O++;
} else if (map[i][j] == 'X') {
X_hw.add(new int[] { i, j });
X++;
}
}

return true;
}
}

/*
* 게임 조건 1. X먼저 시작하므로, X의 수가 1개 더 많거나 같아야 한다. => X의 수가 더 많으면 X가 이긴경우, 같으면 O가 이겨서
* 중간에 끝난거.
*
* 2. 빈칸이 있는 경우, 게임이 끝난 경우여야 한다. 3. 게임이 끝나야 하는데 더 진행되면 안된다. -> 해당 좌표의 말이 없을 때
* 게임이 안끝나면 가능
*
*
*/