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

+64
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

+92
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

+104
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

+69
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)