Skip to content

Commit 31a5c71

Browse files
committed
Update
1 parent f88732e commit 31a5c71

File tree

4 files changed

+329
-0
lines changed

4 files changed

+329
-0
lines changed

13/1.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
// 도시의 개수, 도로의 개수, 거리 정보, 출발 도시 번호
6+
public static int n, m, k, x;
7+
public static ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>();
8+
// 모든 도시에 대한 최단 거리 초기화
9+
public static int[] d = new int[300001];
10+
11+
public static void main(String[] args) {
12+
Scanner sc = new Scanner(System.in);
13+
14+
n = sc.nextInt();
15+
m = sc.nextInt();
16+
k = sc.nextInt();
17+
x = sc.nextInt();
18+
19+
// 그래피 및 최단 거리 테이블 초기화
20+
for (int i = 0; i <= n; i++) {
21+
graph.add(new ArrayList<Integer>());
22+
d[i] = -1;
23+
}
24+
25+
// 모든 도로 정보 입력 받기
26+
for (int i = 0; i < m; i++) {
27+
int a = sc.nextInt();
28+
int b = sc.nextInt();
29+
graph.get(a).add(b);
30+
}
31+
32+
// 출발 도시까지의 거리는 0으로 설정
33+
d[x] = 0;
34+
35+
// 너비 우선 탐색(BFS) 수행
36+
Queue<Integer> q = new LinkedList<Integer>();
37+
q.offer(x);
38+
while (!q.isEmpty()) {
39+
int now = q.poll();
40+
// 현재 도시에서 이동할 수 있는 모든 도시를 확인
41+
for (int i = 0; i < graph.get(now).size(); i++) {
42+
int nextNode = graph.get(now).get(i);
43+
// 아직 방문하지 않은 도시라면
44+
if (d[nextNode] == -1) {
45+
// 최단 거리 갱신
46+
d[nextNode] = d[now] + 1;
47+
q.offer(nextNode);
48+
}
49+
}
50+
}
51+
52+
// 최단 거리가 K인 모든 도시의 번호를 오름차순으로 출력
53+
boolean check = false;
54+
for (int i = 1; i <= n; i++) {
55+
if (d[i] == k) {
56+
System.out.println(i);
57+
check = true;
58+
}
59+
}
60+
61+
// 만약 최단 거리가 K인 도시가 없다면, -1 출력
62+
if (!check) System.out.println(-1);
63+
}
64+
}

13/2.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
public static int n, m, result = 0;
6+
public static int[][] arr = new int[8][8]; // 초기 맵 배열
7+
public static int[][] temp = new int[8][8]; // 벽을 설치한 뒤의 맵 배열
8+
9+
// 4가지 이동 방향에 대한 배열
10+
public static int[] dx = {-1, 0, 1, 0};
11+
public static int[] dy = {0, 1, 0, -1};
12+
13+
// 깊이 우선 탐색(DFS)을 이용해 각 바이러스가 사방으로 퍼지도록 하기
14+
public static void virus(int x, int y) {
15+
for (int i = 0; i < 4; i++) {
16+
int nx = x + dx[i];
17+
int ny = y + dy[i];
18+
// 상, 하, 좌, 우 중에서 바이러스가 퍼질 수 있는 경우
19+
if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
20+
if (temp[nx][ny] == 0) {
21+
// 해당 위치에 바이러스 배치하고, 다시 재귀적으로 수행
22+
temp[nx][ny] = 2;
23+
virus(nx, ny);
24+
}
25+
}
26+
}
27+
}
28+
29+
// 현재 맵에서 안전 영역의 크기 계산하는 메서드
30+
public static int getScore() {
31+
int score = 0;
32+
for (int i = 0; i < n; i++) {
33+
for (int j = 0; j < m; j++) {
34+
if (temp[i][j] == 0) {
35+
score += 1;
36+
}
37+
}
38+
}
39+
return score;
40+
}
41+
42+
// 깊이 우선 탐색(DFS)을 이용해 울타리를 설치하면서, 매 번 안전 영역의 크기 계산
43+
public static void dfs(int count) {
44+
// 울타리가 3개 설치된 경우
45+
if (count == 3) {
46+
for (int i = 0; i < n; i++) {
47+
for (int j = 0; j < m; j++) {
48+
temp[i][j] = arr[i][j];
49+
}
50+
}
51+
// 각 바이러스의 위치에서 전파 진행
52+
for (int i = 0; i < n; i++) {
53+
for (int j = 0; j < m; j++) {
54+
if (temp[i][j] == 2) {
55+
virus(i, j);
56+
}
57+
}
58+
}
59+
// 안전 영역의 최대값 계산
60+
result = Math.max(result, getScore());
61+
return;
62+
}
63+
// 빈 공간에 울타리를 설치
64+
for (int i = 0; i < n; i++) {
65+
for (int j = 0; j < m; j++) {
66+
if (arr[i][j] == 0) {
67+
arr[i][j] = 1;
68+
count += 1;
69+
dfs(count);
70+
arr[i][j] = 0;
71+
count -= 1;
72+
}
73+
}
74+
}
75+
}
76+
77+
public static void main(String[] args) {
78+
Scanner sc = new Scanner(System.in);
79+
80+
n = sc.nextInt();
81+
m = sc.nextInt();
82+
83+
for (int i = 0; i < n; i++) {
84+
for (int j = 0; j < m; j++) {
85+
arr[i][j] = sc.nextInt();
86+
}
87+
}
88+
89+
dfs(0);
90+
System.out.println(result);
91+
}
92+
}

13/3.java

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import java.util.*;
2+
3+
class Virus implements Comparable<Virus> {
4+
5+
private int index;
6+
private int second;
7+
private int x;
8+
private int y;
9+
10+
public Virus(int index, int second, int x, int y) {
11+
this.index = index;
12+
this.second = second;
13+
this.x = x;
14+
this.y = y;
15+
}
16+
17+
public int getIndex() {
18+
return this.index;
19+
}
20+
21+
public int getSecond() {
22+
return this.second;
23+
}
24+
25+
public int getX() {
26+
return this.x;
27+
}
28+
29+
public int getY() {
30+
return this.y;
31+
}
32+
33+
// 정렬 기준은 '번호가 낮은 순서'
34+
@Override
35+
public int compareTo(Virus other) {
36+
if (this.index < other.index) {
37+
return -1;
38+
}
39+
return 1;
40+
}
41+
}
42+
43+
public class Main {
44+
45+
public static int n, k;
46+
// 전체 보드 정보를 담는 배열
47+
public static int[][] graph = new int[200][200];
48+
public static ArrayList<Virus> viruses = new ArrayList<Virus>();
49+
50+
// 바이러스가 퍼져나갈 수 있는 4가지의 위치
51+
public static int[] dx = {-1, 0, 1, 0};
52+
public static int[] dy = {0, 1, 0, -1};
53+
54+
public static void main(String[] args) {
55+
Scanner sc = new Scanner(System.in);
56+
57+
n = sc.nextInt();
58+
k = sc.nextInt();
59+
60+
for (int i = 0; i < n; i++) {
61+
for (int j = 0; j < n; j++) {
62+
graph[i][j] = sc.nextInt();
63+
// 해당 위치에 바이러스가 존재하는 경우
64+
if (graph[i][j] != 0) {
65+
// (바이러스 종류, 시간, 위치 X, 위치 Y) 삽입
66+
viruses.add(new Virus(graph[i][j], 0, i, j));
67+
}
68+
}
69+
}
70+
71+
// 정렬 이후에 큐로 옮기기 (낮은 번호의 바이러스가 먼저 증식하므로)
72+
Collections.sort(viruses);
73+
Queue<Virus> q = new LinkedList<Virus>();
74+
for (int i = 0; i < viruses.size(); i++) {
75+
q.offer(viruses.get(i));
76+
}
77+
78+
int targetS = sc.nextInt();
79+
int targetX = sc.nextInt();
80+
int targetY = sc.nextInt();
81+
82+
// 너비 우선 탐색(BFS) 진행
83+
while (!q.isEmpty()) {
84+
Virus virus = q.poll();
85+
// 정확히 second만큼 초가 지나거나, 큐가 빌 때까지 반복
86+
if (virus.getSecond() == targetS) break;
87+
// 현재 노드에서 주변 4가지 위치를 각각 확인
88+
for (int i = 0; i < 4; i++) {
89+
int nx = virus.getX() + dx[i];
90+
int ny = virus.getY() + dy[i];
91+
// 해당 위치로 이동할 수 있는 경우
92+
if (0 <= nx && nx < n && 0 <= ny && ny < n) {
93+
// 아직 방문하지 않은 위치라면, 그 위치에 바이러스 넣기
94+
if (graph[nx][ny] == 0) {
95+
graph[nx][ny] = virus.getIndex();
96+
q.offer(new Virus(virus.getIndex(), virus.getSecond() + 1, nx, ny));
97+
}
98+
}
99+
}
100+
}
101+
102+
System.out.println(graph[targetX - 1][targetY - 1]);
103+
}
104+
}

13/5.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
5+
public static int n;
6+
// 연산을 수행하고자 하는 수 리스트
7+
public static ArrayList<Integer> arr = new ArrayList<>();
8+
// 더하기, 빼기, 곱하기, 나누기 연산자 개수
9+
public static int add, sub, mul, divi;
10+
11+
// 최솟값과 최댓값 초기화
12+
public static int minValue = (int) 1e9;
13+
public static int maxValue = (int) -1e9;
14+
15+
// 깊이 우선 탐색 (DFS) 메서드
16+
public static void dfs(int i, int now) {
17+
// 모든 연산자를 다 사용한 경우, 최솟값과 최댓값 업데이트
18+
if (i == n) {
19+
minValue = Math.min(minValue, now);
20+
maxValue = Math.max(maxValue, now);
21+
}
22+
else {
23+
// 각 연산자에 대하여 재귀적으로 수행
24+
if (add > 0) {
25+
add -= 1;
26+
dfs(i + 1, now + arr.get(i));
27+
add += 1;
28+
}
29+
if (sub > 0) {
30+
sub -= 1;
31+
dfs(i + 1, now - arr.get(i));
32+
sub += 1;
33+
}
34+
if (mul > 0) {
35+
mul -= 1;
36+
dfs(i + 1, now * arr.get(i));
37+
mul += 1;
38+
}
39+
if (divi > 0) {
40+
divi -= 1;
41+
dfs(i + 1, now / arr.get(i));
42+
divi += 1;
43+
}
44+
}
45+
}
46+
47+
public static void main(String[] args) {
48+
Scanner sc = new Scanner(System.in);
49+
50+
n = sc.nextInt();
51+
52+
for (int i = 0; i < n; i++) {
53+
int x = sc.nextInt();
54+
arr.add(x);
55+
}
56+
57+
add = sc.nextInt();
58+
sub = sc.nextInt();
59+
mul = sc.nextInt();
60+
divi = sc.nextInt();
61+
62+
// DFS 메서드 호출
63+
dfs(1, arr.get(0));
64+
65+
// 최댓값과 최솟값 차례대로 출력
66+
System.out.println(maxValue);
67+
System.out.println(minValue);
68+
}
69+
}

0 commit comments

Comments
 (0)