Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions src/main/java/org/example/_11week/Alphabet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.example._11week;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Alphabet {

private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static boolean[][] visited;
private static int[] dr = {-1, 0, 1, 0};
private static int[] dc = {0, -1, 0, 1};

private static class Position {
int row;
int col;

public Position(int row, int col) {
this.row = row;
this.col = col;
}
}

private static class Process {
Position position;
String process;

public Process(Position position, String process) {
this.position = position;
this.process = process;
}
}

public static void main(String[] args) throws IOException {
StringTokenizer st = new StringTokenizer(br.readLine());
int R = Integer.parseInt(st.nextToken());
int C = Integer.parseInt(st.nextToken());

String[][] graph = new String[R][C];
visited = new boolean[R][C];

for (int i = 0; i < R; i++) {
String[] split = br.readLine().split("");
graph[i] = split;
}

Queue<Process> queue = new LinkedList<>();
Process process = new Process(new Position(0, 0), graph[0][0]);
queue.offer(process);
visited[0][0] = true;

int maxLength = 1;
while (!queue.isEmpty()) {
Process poll = queue.poll();

for (int i = 0; i < dr.length; i++) {
Position position = poll.position;
String curProcess = poll.process;

int newRow = position.row + dr[i];
int newCol = position.col + dc[i];

if (newRow < 0 || newRow >= R || newCol < 0 || newCol >= C) {
continue;
}

if (visited[newRow][newCol]) {
continue;
}

if (curProcess.contains(graph[newRow][newCol])) {
continue;
}

if (maxLength < curProcess.length() + 1) {
maxLength = curProcess.length() + 1;
}

visited[newRow][newCol] = true;
Process newProcess = new Process(new Position(newRow, newCol), curProcess + graph[newRow][newCol]);
queue.offer(newProcess);
}
}

System.out.println(maxLength);
}
}
101 changes: 101 additions & 0 deletions src/main/java/org/example/_11week/BabyShark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package org.example._11week;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class BabyShark {

private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static int[] dx = {-1, -1, -1, 0, 0, 1, 1, 1};
private static int[] dy = {-1, 0, 1, 1, -1, -1, 0, 1};
private static int[][] graph;
private static int N;
private static int M;

private static int maxSafeDistance = Integer.MIN_VALUE;

public static void main(String[] args) throws IOException {
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
graph = new int[N][M];

for (int row = 0; row < N; row++) {
st = new StringTokenizer(br.readLine());
for (int col = 0; col < M; col++) {
int cell = Integer.parseInt(st.nextToken());
graph[row][col] = cell;
}
}

for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
int safeDistance = bfs(new Position(i, j));
if (maxSafeDistance < safeDistance) {
maxSafeDistance = safeDistance;
}
}
}

System.out.println(maxSafeDistance);
}

private static int bfs(Position startPosition) {
Queue<Position> queue = new LinkedList();
Map<Position, Integer> visited = new HashMap<>();

queue.offer(startPosition);
visited.put(startPosition, 0);

Integer curPositionDistance = 0;
while (!queue.isEmpty()) {
Position position = queue.poll();
curPositionDistance = visited.get(position);
if (graph[position.row][position.col] == 1) {
break;
}

for (int i = 0; i < dx.length; i++) {
int nextRow = position.row + dy[i];
int nextCol = position.col + dx[i];
if (nextRow < 0 || nextRow >= N || nextCol < 0 || nextCol >= M) {
continue;
}

Position nextPosition = new Position(nextRow, nextCol);

if (!visited.containsKey(nextPosition)) {
queue.offer(nextPosition);
visited.put(nextPosition, curPositionDistance + 1);
}
}
}

return curPositionDistance;
}

private static class Position {
int row;
int col;

public Position(int row, int col) {
this.row = row;
this.col = col;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Position position = (Position) o;
return row == position.row && col == position.col;
}

@Override
public int hashCode() {
return Objects.hash(row, col);
}
}
}
87 changes: 87 additions & 0 deletions src/main/java/org/example/_11week/BabySharkNotBFS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.example._11week;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class BabySharkNotBFS {

private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static int N;
private static int M;

private static int maxSafeDistance = Integer.MIN_VALUE;
private static List<Position> sharks = new ArrayList<>();

public static void main(String[] args) throws IOException {
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());

for (int row = 0; row < N; row++) {
st = new StringTokenizer(br.readLine());
for (int col = 0; col < M; col++) {
int cell = Integer.parseInt(st.nextToken());
if (cell == 1) {
sharks.add(new Position(row, col));
}
}
}

for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
int safeDistance = calculateSafeDistance(new Position(i, j));
if (maxSafeDistance < safeDistance) {
maxSafeDistance = safeDistance;
}
}
}

System.out.println(maxSafeDistance);
}

private static int calculateSafeDistance(Position position) {
int minDistance = Integer.MAX_VALUE;

for (Position shark : sharks) {
int distance = calculateDistance(position, shark);

if (distance < minDistance) {
minDistance=distance;
}
}

return minDistance;
}

private static int calculateDistance(Position position, Position shark) {
int rowDistance = Math.abs(position.row - shark.row);
int colDistance = Math.abs(position.col - shark.col);

return Math.max(rowDistance, colDistance);
}

private static class Position {
int row;
int col;

public Position(int row, int col) {
this.row = row;
this.col = col;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Position position = (Position) o;
return row == position.row && col == position.col;
}

@Override
public int hashCode() {
return Objects.hash(row, col);
}
}
}
93 changes: 93 additions & 0 deletions src/main/java/org/example/_11week/BabySharkRefactoring.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package org.example._11week;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class BabySharkRefactoring {
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static int N;
private static int M;

private static int maxSafeDistance = Integer.MIN_VALUE;
private static Map<Position, Boolean> sharks;

public static void main(String[] args) throws IOException {
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
sharks = new HashMap<>(N * M * 2);

for (int row = 0; row < N; row++) {
st = new StringTokenizer(br.readLine());
for (int col = 0; col < M; col++) {
int cell = Integer.parseInt(st.nextToken());
if (cell == 1) {
sharks.put(new Position(row, col), true);
}
}
}

for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
Position position = new Position(i, j);
if (sharks.containsKey(position)) {
continue;
}

int safeDistance = calculateSafeDistance(position);
if (maxSafeDistance < safeDistance) {
maxSafeDistance = safeDistance;
}
}
}

System.out.println(maxSafeDistance);
}

private static int calculateSafeDistance(Position position) {
int minDistance = Integer.MAX_VALUE;

Set<Position> sharkSet = sharks.keySet();
for (Position shark : sharkSet) {
int distance = calculateDistance(position, shark);

if (distance < minDistance) {
minDistance = distance;
}
}

return minDistance;
}

private static int calculateDistance(Position position, Position shark) {
int rowDistance = Math.abs(position.row - shark.row);
int colDistance = Math.abs(position.col - shark.col);

return Math.max(rowDistance, colDistance);
}

private static class Position {
int row;
int col;

public Position(int row, int col) {
this.row = row;
this.col = col;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Position position = (Position) o;
return row == position.row && col == position.col;
}

@Override
public int hashCode() {
return Objects.hash(row, col);
}
}
}
Loading