Skip to content

Commit c85a43d

Browse files
authored
Feat/16week (#17)
* [feat] 16주차 * [feat] 18주차
1 parent 8982a92 commit c85a43d

21 files changed

+1818
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package org.example._11week;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.LinkedList;
7+
import java.util.Queue;
8+
import java.util.StringTokenizer;
9+
10+
public class Alphabet {
11+
12+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
private static boolean[][] visited;
14+
private static int[] dr = {-1, 0, 1, 0};
15+
private static int[] dc = {0, -1, 0, 1};
16+
17+
private static class Position {
18+
int row;
19+
int col;
20+
21+
public Position(int row, int col) {
22+
this.row = row;
23+
this.col = col;
24+
}
25+
}
26+
27+
private static class Process {
28+
Position position;
29+
String process;
30+
31+
public Process(Position position, String process) {
32+
this.position = position;
33+
this.process = process;
34+
}
35+
}
36+
37+
public static void main(String[] args) throws IOException {
38+
StringTokenizer st = new StringTokenizer(br.readLine());
39+
int R = Integer.parseInt(st.nextToken());
40+
int C = Integer.parseInt(st.nextToken());
41+
42+
String[][] graph = new String[R][C];
43+
visited = new boolean[R][C];
44+
45+
for (int i = 0; i < R; i++) {
46+
String[] split = br.readLine().split("");
47+
graph[i] = split;
48+
}
49+
50+
Queue<Process> queue = new LinkedList<>();
51+
Process process = new Process(new Position(0, 0), graph[0][0]);
52+
queue.offer(process);
53+
visited[0][0] = true;
54+
55+
int maxLength = 1;
56+
while (!queue.isEmpty()) {
57+
Process poll = queue.poll();
58+
59+
for (int i = 0; i < dr.length; i++) {
60+
Position position = poll.position;
61+
String curProcess = poll.process;
62+
63+
int newRow = position.row + dr[i];
64+
int newCol = position.col + dc[i];
65+
66+
if (newRow < 0 || newRow >= R || newCol < 0 || newCol >= C) {
67+
continue;
68+
}
69+
70+
if (visited[newRow][newCol]) {
71+
continue;
72+
}
73+
74+
if (curProcess.contains(graph[newRow][newCol])) {
75+
continue;
76+
}
77+
78+
if (maxLength < curProcess.length() + 1) {
79+
maxLength = curProcess.length() + 1;
80+
}
81+
82+
visited[newRow][newCol] = true;
83+
Process newProcess = new Process(new Position(newRow, newCol), curProcess + graph[newRow][newCol]);
84+
queue.offer(newProcess);
85+
}
86+
}
87+
88+
System.out.println(maxLength);
89+
}
90+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package org.example._11week;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.*;
7+
8+
public class BabyShark {
9+
10+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
private static int[] dx = {-1, -1, -1, 0, 0, 1, 1, 1};
12+
private static int[] dy = {-1, 0, 1, 1, -1, -1, 0, 1};
13+
private static int[][] graph;
14+
private static int N;
15+
private static int M;
16+
17+
private static int maxSafeDistance = Integer.MIN_VALUE;
18+
19+
public static void main(String[] args) throws IOException {
20+
StringTokenizer st = new StringTokenizer(br.readLine());
21+
N = Integer.parseInt(st.nextToken());
22+
M = Integer.parseInt(st.nextToken());
23+
graph = new int[N][M];
24+
25+
for (int row = 0; row < N; row++) {
26+
st = new StringTokenizer(br.readLine());
27+
for (int col = 0; col < M; col++) {
28+
int cell = Integer.parseInt(st.nextToken());
29+
graph[row][col] = cell;
30+
}
31+
}
32+
33+
for (int i = 0; i < N; i++) {
34+
for (int j = 0; j < M; j++) {
35+
int safeDistance = bfs(new Position(i, j));
36+
if (maxSafeDistance < safeDistance) {
37+
maxSafeDistance = safeDistance;
38+
}
39+
}
40+
}
41+
42+
System.out.println(maxSafeDistance);
43+
}
44+
45+
private static int bfs(Position startPosition) {
46+
Queue<Position> queue = new LinkedList();
47+
Map<Position, Integer> visited = new HashMap<>();
48+
49+
queue.offer(startPosition);
50+
visited.put(startPosition, 0);
51+
52+
Integer curPositionDistance = 0;
53+
while (!queue.isEmpty()) {
54+
Position position = queue.poll();
55+
curPositionDistance = visited.get(position);
56+
if (graph[position.row][position.col] == 1) {
57+
break;
58+
}
59+
60+
for (int i = 0; i < dx.length; i++) {
61+
int nextRow = position.row + dy[i];
62+
int nextCol = position.col + dx[i];
63+
if (nextRow < 0 || nextRow >= N || nextCol < 0 || nextCol >= M) {
64+
continue;
65+
}
66+
67+
Position nextPosition = new Position(nextRow, nextCol);
68+
69+
if (!visited.containsKey(nextPosition)) {
70+
queue.offer(nextPosition);
71+
visited.put(nextPosition, curPositionDistance + 1);
72+
}
73+
}
74+
}
75+
76+
return curPositionDistance;
77+
}
78+
79+
private static class Position {
80+
int row;
81+
int col;
82+
83+
public Position(int row, int col) {
84+
this.row = row;
85+
this.col = col;
86+
}
87+
88+
@Override
89+
public boolean equals(Object o) {
90+
if (this == o) return true;
91+
if (o == null || getClass() != o.getClass()) return false;
92+
Position position = (Position) o;
93+
return row == position.row && col == position.col;
94+
}
95+
96+
@Override
97+
public int hashCode() {
98+
return Objects.hash(row, col);
99+
}
100+
}
101+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package org.example._11week;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.*;
7+
8+
public class BabySharkNotBFS {
9+
10+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
private static int N;
12+
private static int M;
13+
14+
private static int maxSafeDistance = Integer.MIN_VALUE;
15+
private static List<Position> sharks = new ArrayList<>();
16+
17+
public static void main(String[] args) throws IOException {
18+
StringTokenizer st = new StringTokenizer(br.readLine());
19+
N = Integer.parseInt(st.nextToken());
20+
M = Integer.parseInt(st.nextToken());
21+
22+
for (int row = 0; row < N; row++) {
23+
st = new StringTokenizer(br.readLine());
24+
for (int col = 0; col < M; col++) {
25+
int cell = Integer.parseInt(st.nextToken());
26+
if (cell == 1) {
27+
sharks.add(new Position(row, col));
28+
}
29+
}
30+
}
31+
32+
for (int i = 0; i < N; i++) {
33+
for (int j = 0; j < M; j++) {
34+
int safeDistance = calculateSafeDistance(new Position(i, j));
35+
if (maxSafeDistance < safeDistance) {
36+
maxSafeDistance = safeDistance;
37+
}
38+
}
39+
}
40+
41+
System.out.println(maxSafeDistance);
42+
}
43+
44+
private static int calculateSafeDistance(Position position) {
45+
int minDistance = Integer.MAX_VALUE;
46+
47+
for (Position shark : sharks) {
48+
int distance = calculateDistance(position, shark);
49+
50+
if (distance < minDistance) {
51+
minDistance=distance;
52+
}
53+
}
54+
55+
return minDistance;
56+
}
57+
58+
private static int calculateDistance(Position position, Position shark) {
59+
int rowDistance = Math.abs(position.row - shark.row);
60+
int colDistance = Math.abs(position.col - shark.col);
61+
62+
return Math.max(rowDistance, colDistance);
63+
}
64+
65+
private static class Position {
66+
int row;
67+
int col;
68+
69+
public Position(int row, int col) {
70+
this.row = row;
71+
this.col = col;
72+
}
73+
74+
@Override
75+
public boolean equals(Object o) {
76+
if (this == o) return true;
77+
if (o == null || getClass() != o.getClass()) return false;
78+
Position position = (Position) o;
79+
return row == position.row && col == position.col;
80+
}
81+
82+
@Override
83+
public int hashCode() {
84+
return Objects.hash(row, col);
85+
}
86+
}
87+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package org.example._11week;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.*;
7+
8+
public class BabySharkRefactoring {
9+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
private static int N;
11+
private static int M;
12+
13+
private static int maxSafeDistance = Integer.MIN_VALUE;
14+
private static Map<Position, Boolean> sharks;
15+
16+
public static void main(String[] args) throws IOException {
17+
StringTokenizer st = new StringTokenizer(br.readLine());
18+
N = Integer.parseInt(st.nextToken());
19+
M = Integer.parseInt(st.nextToken());
20+
sharks = new HashMap<>(N * M * 2);
21+
22+
for (int row = 0; row < N; row++) {
23+
st = new StringTokenizer(br.readLine());
24+
for (int col = 0; col < M; col++) {
25+
int cell = Integer.parseInt(st.nextToken());
26+
if (cell == 1) {
27+
sharks.put(new Position(row, col), true);
28+
}
29+
}
30+
}
31+
32+
for (int i = 0; i < N; i++) {
33+
for (int j = 0; j < M; j++) {
34+
Position position = new Position(i, j);
35+
if (sharks.containsKey(position)) {
36+
continue;
37+
}
38+
39+
int safeDistance = calculateSafeDistance(position);
40+
if (maxSafeDistance < safeDistance) {
41+
maxSafeDistance = safeDistance;
42+
}
43+
}
44+
}
45+
46+
System.out.println(maxSafeDistance);
47+
}
48+
49+
private static int calculateSafeDistance(Position position) {
50+
int minDistance = Integer.MAX_VALUE;
51+
52+
Set<Position> sharkSet = sharks.keySet();
53+
for (Position shark : sharkSet) {
54+
int distance = calculateDistance(position, shark);
55+
56+
if (distance < minDistance) {
57+
minDistance = distance;
58+
}
59+
}
60+
61+
return minDistance;
62+
}
63+
64+
private static int calculateDistance(Position position, Position shark) {
65+
int rowDistance = Math.abs(position.row - shark.row);
66+
int colDistance = Math.abs(position.col - shark.col);
67+
68+
return Math.max(rowDistance, colDistance);
69+
}
70+
71+
private static class Position {
72+
int row;
73+
int col;
74+
75+
public Position(int row, int col) {
76+
this.row = row;
77+
this.col = col;
78+
}
79+
80+
@Override
81+
public boolean equals(Object o) {
82+
if (this == o) return true;
83+
if (o == null || getClass() != o.getClass()) return false;
84+
Position position = (Position) o;
85+
return row == position.row && col == position.col;
86+
}
87+
88+
@Override
89+
public int hashCode() {
90+
return Objects.hash(row, col);
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)