Skip to content
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

[week16] sejinkim #87

Open
wants to merge 3 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
67 changes: 67 additions & 0 deletions sejinkim/week16/DFS와_BFS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import java.util.*;

public class DFS와_BFS {
static Node[] nodes;
static boolean[] visited;
static StringBuilder sb;

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), e = sc.nextInt(), start = sc.nextInt();

nodes = new Node[n + 1];
for (int i = 1; i <= n; i++) {
nodes[i] = new Node();
}

while (e-- > 0) {
int a = sc.nextInt(), b = sc.nextInt();
nodes[a].next.add(b);
nodes[b].next.add(a);
}

for (int i = 1; i <= n; i++) {
nodes[i].next.sort(Comparator.naturalOrder());
}

sb = new StringBuilder();
visited = new boolean[n + 1];
dfs(start);

sb.append('\n');
visited = new boolean[n + 1];
bfs(start);

System.out.println(sb);
}

static void bfs(int start) {
Queue<Integer> q = new LinkedList<>();
visited[start] = true;
q.add(start);
while (!q.isEmpty()) {
int cur = q.poll();
sb.append(cur).append(' ');
for (int i : nodes[cur].next) {
if (!visited[i]) {
visited[i] = true;
q.add(i);
}
}
}
}

static void dfs(int cur) {
if (!visited[cur]) {
visited[cur] = true;
sb.append(cur).append(' ');
for (int i : nodes[cur].next) {
dfs(i);
}
}
}

static class Node {
List<Integer> next = new ArrayList<>();
}
}
24 changes: 24 additions & 0 deletions sejinkim/week16/수학숙제.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.util.*;
import java.math.BigInteger;

public class 수학숙제 {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
PriorityQueue<BigInteger> pq = new PriorityQueue<>();
while (n-- > 0) {
String[] numbers = sc.next().split("[a-z]");
for (String number : numbers) {
if (number.length() > 0) {
pq.add(new BigInteger(number));
}
}
}
StringBuilder sb = new StringBuilder();
while (!pq.isEmpty()) {
sb.append(pq.poll()).append('\n');
}
System.out.println(sb);
}
}
105 changes: 105 additions & 0 deletions sejinkim/week16/인싸들의_가위바위보.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import java.util.*;

public class 인싸들의_가위바위보 {
static int n, k;
static int[][] gestures, scorecard;

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
k = sc.nextInt();
scorecard = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scorecard[i][j] = sc.nextInt();
}
}
gestures = new int[3][20];
for (int i = 1; i < n; i++) {
gestures[0][i] = i;
}
for (int i = 0; i < 20; i++) {
gestures[1][i] = sc.nextInt() - 1;
}
for (int i = 0; i < 20; i++) {
gestures[2][i] = sc.nextInt() - 1;
}
System.out.println(testAll());
}

static int testAll() {
do {
if (testOne() == 0) {
return 1;
}
} while (nextPermutation(gestures[0]));
return 0;
}

static int testOne() {
Player[] playerInfo = new Player[3];
for (int i = 0; i < 3; i++) {
playerInfo[i] = new Player(i);
}
int[] roundInfo = { 0, 1 };
while (playerInfo[0].round < n) {
int winner = getWinner(playerInfo[roundInfo[0]], playerInfo[roundInfo[1]]);
if (playerInfo[winner].win() == k) {
return winner;
}
roundInfo[0] = 3 - roundInfo[0] - roundInfo[1];
roundInfo[1] = winner;
}
return -1;
}

static boolean nextPermutation(int[] arr) {
int top = n - 1;
while (top > 0 && arr[top - 1] > arr[top]) {
top--;
}
if (top == 0) {
return false;
}
int target = n - 1;
while (arr[top - 1] > arr[target]) {
target--;
}
swap(arr, top - 1, target);
target = n - 1;
while (top < target) {
swap(arr, top++, target--);
}
return true;
}

static void swap(int[] arr, int i, int j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}

static int getWinner(Player first, Player second) {
int result = scorecard[first.gesture()][second.gesture()];
if (result == 2 || (result == 1 && first.id > second.id)) {
return first.id;
}
return second.id;
}

static class Player {
int id, round, score;

Player(int id) {
this.id = id;
}

int win() {
return ++score;
}

int gesture() {
return gestures[id][round++];
}
}
}