Skip to content

Commit 21bb7d3

Browse files
authored
Merge pull request #204 from JavaCote/sangeok
이상억 35주차
2 parents 383859f + 4a4e8cc commit 21bb7d3

File tree

4 files changed

+255
-0
lines changed

4 files changed

+255
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.util.*;
2+
import java.io.*;
3+
4+
public class Main {
5+
6+
static int N, K;
7+
static int[] A;
8+
static boolean[] used;
9+
static int count = 0;
10+
11+
public static void main(String[] args) throws IOException {
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
StringTokenizer st = new StringTokenizer(br.readLine());
14+
15+
N = Integer.parseInt(st.nextToken());
16+
K = Integer.parseInt(st.nextToken());
17+
18+
A = new int[N];
19+
used = new boolean[N];
20+
21+
st = new StringTokenizer(br.readLine());
22+
for (int i = 0; i < N; i++) {
23+
A[i] = Integer.parseInt(st.nextToken());
24+
}
25+
26+
dfs(0, 500);
27+
System.out.println(count);
28+
}
29+
30+
static void dfs(int day, int weight) {
31+
if (weight < 500) return;
32+
33+
if (day == N) {
34+
count++;
35+
return;
36+
}
37+
38+
for (int i = 0; i < N; i++) {
39+
if (!used[i]) {
40+
used[i] = true;
41+
dfs(day + 1, weight + A[i] - K);
42+
used[i] = false;
43+
}
44+
}
45+
}
46+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
public static void main(String[] args) throws Exception {
6+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
StringBuilder sb = new StringBuilder();
8+
9+
int T = Integer.parseInt(br.readLine());
10+
11+
for (int i = 0; i < T; i++) {
12+
StringTokenizer st = new StringTokenizer(br.readLine());
13+
long n = Long.parseLong(st.nextToken());
14+
long m = Long.parseLong(st.nextToken());
15+
16+
if (n >= 2 && m >= 2 && (n % 2 == m % 2)) {
17+
sb.append("NO\n");
18+
} else {
19+
sb.append("YES\n");
20+
}
21+
}
22+
23+
System.out.print(sb);
24+
}
25+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import java.util.*;
2+
import java.io.*;
3+
4+
public class Main {
5+
6+
static class Edge {
7+
int to;
8+
int cost;
9+
Edge(int to, int cost){
10+
this.to = to;
11+
this.cost = cost;
12+
}
13+
}
14+
15+
static int n, m, r;
16+
static int[] items;
17+
static List<Edge>[] graph;
18+
19+
public static void main(String[] args) throws IOException {
20+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
21+
StringTokenizer st;
22+
23+
st = new StringTokenizer(br.readLine());
24+
n = Integer.parseInt(st.nextToken()); // 지역 개수
25+
m = Integer.parseInt(st.nextToken()); // 수색 범위
26+
r = Integer.parseInt(st.nextToken()); // 길 개수
27+
28+
items = new int[n + 1];
29+
30+
st = new StringTokenizer(br.readLine());
31+
for (int i = 1; i <= n; i++) {
32+
items[i] = Integer.parseInt(st.nextToken());
33+
}
34+
35+
graph = new ArrayList[n + 1];
36+
for (int i = 1; i <= n; i++) {
37+
graph[i] = new ArrayList<>();
38+
}
39+
40+
for (int i = 0; i < r; i++) {
41+
st = new StringTokenizer(br.readLine());
42+
int a = Integer.parseInt(st.nextToken());
43+
int b = Integer.parseInt(st.nextToken());
44+
int l = Integer.parseInt(st.nextToken());
45+
46+
graph[a].add(new Edge(b, l));
47+
graph[b].add(new Edge(a, l));
48+
}
49+
50+
int answer = 0;
51+
52+
// 모든 지역을 시작점으로 다익스트라
53+
for (int i = 1; i <= n; i++) {
54+
answer = Math.max(answer, dijkstra(i));
55+
}
56+
57+
System.out.println(answer);
58+
}
59+
60+
static int dijkstra(int start) {
61+
int[] dist = new int[n + 1];
62+
Arrays.fill(dist, Integer.MAX_VALUE);
63+
dist[start] = 0;
64+
65+
PriorityQueue<int[]> pq =
66+
new PriorityQueue<>((a, b) -> Integer.compare(a[1], b[1]));
67+
68+
pq.add(new int[]{start, 0});
69+
70+
while (!pq.isEmpty()) {
71+
int[] cur = pq.poll();
72+
int now = cur[0];
73+
int cost = cur[1];
74+
75+
if (cost > dist[now]) continue;
76+
77+
for (Edge e : graph[now]) {
78+
int next = e.to;
79+
int nextCost = cost + e.cost;
80+
81+
if (nextCost < dist[next]) {
82+
dist[next] = nextCost;
83+
pq.add(new int[]{next, nextCost});
84+
}
85+
}
86+
}
87+
88+
int sum = 0;
89+
for (int i = 1; i <= n; i++) {
90+
if (dist[i] <= m) {
91+
sum += items[i];
92+
}
93+
}
94+
return sum;
95+
}
96+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import java.util.*;
2+
import java.io.*;
3+
4+
public class Main {
5+
6+
static int N;
7+
static char[][] map;
8+
static boolean[][] visited;
9+
10+
static int[] dx = {-1, 1, 0, 0};
11+
static int[] dy = {0, 0, -1, 1};
12+
13+
public static void main(String[] args) throws IOException {
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
16+
N = Integer.parseInt(br.readLine());
17+
map = new char[N][N];
18+
19+
for (int i = 0; i < N; i++) {
20+
String line = br.readLine();
21+
for (int j = 0; j < N; j++) {
22+
map[i][j] = line.charAt(j);
23+
}
24+
}
25+
26+
// 일반 시야
27+
visited = new boolean[N][N];
28+
int normal = 0;
29+
30+
for (int i = 0; i < N; i++) {
31+
for (int j = 0; j < N; j++) {
32+
if (!visited[i][j]) {
33+
bfs(i, j, false);
34+
normal++;
35+
}
36+
}
37+
}
38+
39+
// 적록색약
40+
visited = new boolean[N][N];
41+
int blind = 0;
42+
43+
for (int i = 0; i < N; i++) {
44+
for (int j = 0; j < N; j++) {
45+
if (!visited[i][j]) {
46+
bfs(i, j, true);
47+
blind++;
48+
}
49+
}
50+
}
51+
52+
System.out.println(normal + " " + blind);
53+
}
54+
55+
static void bfs(int x, int y, boolean colorblind) {
56+
Queue<int[]> q = new LinkedList<>();
57+
q.add(new int[]{x, y});
58+
visited[x][y] = true;
59+
60+
char start = map[x][y];
61+
62+
while (!q.isEmpty()) {
63+
int[] cur = q.poll();
64+
65+
for (int d = 0; d < 4; d++) {
66+
int nx = cur[0] + dx[d];
67+
int ny = cur[1] + dy[d];
68+
69+
if (nx < 0 || ny < 0 || nx >= N || ny >= N) continue;
70+
if (visited[nx][ny]) continue;
71+
72+
char next = map[nx][ny];
73+
74+
if (sameColor(start, next, colorblind)) {
75+
visited[nx][ny] = true;
76+
q.add(new int[]{nx, ny});
77+
}
78+
}
79+
}
80+
}
81+
82+
static boolean sameColor(char a, char b, boolean blind) {
83+
if (!blind) return a == b;
84+
85+
if (a == 'B' || b == 'B') return a == b;
86+
return true;
87+
}
88+
}

0 commit comments

Comments
 (0)