## 과제 : 백준 문제 풀이
목표 : 백준 문제를 풀어본다.
- [녹색 옷 입은 애가 젤다지?](https://www.acmicpc.net/problem/4485)
- [플로이드](https://www.acmicpc.net/problem/11404)
- [사이클 게임](https://www.acmicpc.net/problem/20040)
- [줄 세우기](https://www.acmicpc.net/problem/2252)
---
## 과제 : 백준 문제 풀이 모범답안
### 녹색 옷 입은 애가 젤다지?
```java
import java.util.*;
public class Main {
static int[] dx = {0, 1, 0, -1};
static int[] dy = {1, 0, -1, 0};
static class Node implements Comparable<Node> {
int x, y, cost;
Node(int x, int y, int cost) {
this.x = x;
this.y = y;
this.cost = cost;
}
@Override
public int compareTo(Node other) {
return this.cost - other.cost;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = 1;
while (true) {
int N = sc.nextInt();
if (N == 0) break;
int[][] map = new int[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
map[i][j] = sc.nextInt();
}
}
int[][] dist = new int[N][N];
for (int[] row : dist) {
Arrays.fill(row, Integer.MAX_VALUE);
}
dist[0][0] = map[0][0];
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.add(new Node(0, 0, map[0][0]));
while (!pq.isEmpty()) {
Node current = pq.poll();
for (int i = 0; i < 4; i++) {
int nx = current.x + dx[i];
int ny = current.y + dy[i];
if (nx >= 0 && nx < N && ny >= 0 && ny < N) {
int nextCost = current.cost + map[nx][ny];
if (nextCost < dist[nx][ny]) {
dist[nx][ny] = nextCost;
pq.add(new Node(nx, ny, nextCost));
}
}
}
}
System.out.println("Problem " + t++ + ": " + dist[N-1][N-1]);
}
}
}
```
### 플로이드
```java
import java.util.*;
public class Main {
static final int INF = 10000000;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] dist = new int[n][n];
for (int i = 0; i < n; i++) {
Arrays.fill(dist[i], INF);
dist[i][i] = 0;
}
for (int i = 0; i < m; i++) {
int u = sc.nextInt() - 1;
int v = sc.nextInt() - 1;
int w = sc.nextInt();
if (dist[u][v] > w) {
dist[u][v] = w;
}
}
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (dist[i][j] > dist[i][k] + dist[k][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
sb.append(dist[i][j] == INF ? 0 : dist[i][j]).append(" ");
}
sb.append("\n");
}
System.out.print(sb.toString());
}
}
```
### 사이클 게임
```java
import java.util.Scanner;
public class Main {
static int[] parent;
public static int find(int x) {
if (parent[x] == x) {
return x;
}
return parent[x] = find(parent[x]);
}
public static boolean union(int x, int y) {
x = find(x);
y = find(y);
if (x == y) {
return true; // 이미 같은 집합에 속해 있음 (사이클 발생)
}
parent[y] = x;
return false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
parent = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
}
int result = 0;
for (int i = 1; i <= m; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
if (union(a, b)) {
result = i;
break;
}
}
System.out.println(result);
sc.close();
}
}
```
### 줄 세우기
```java
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt(); // 학생 수
int M = sc.nextInt(); // 비교 횟수
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
int[] indegree = new int[N + 1];
for (int i = 0; i <= N; i++) {
adj.add(new ArrayList<>());
}
for (int i = 0; i < M; i++) {
int A = sc.nextInt();
int B = sc.nextInt();
adj.get(A).add(B);
indegree[B]++;
}
Queue<Integer> queue = new LinkedList<>();
for (int i = 1; i <= N; i++) {
if (indegree[i] == 0) {
queue.offer(i);
}
}
ArrayList<Integer> result = new ArrayList<>();
while (!queue.isEmpty()) {
int current = queue.poll();
result.add(current);
for (int next : adj.get(current)) {
indegree[next]--;
if (indegree[next] == 0) {
queue.offer(next);
}
}
}
for (int num : result) {
System.out.print(num + " ");
}
sc.close();
}
}
```