이전에는 공부한 내용이나 회고글 들을 모두 로컬로 관리했었다. 문서 도구로 옵시디언을 사용한다. 이번에 로컬로 관리하던 옵시디언 파일들을 모두 깃으로 관리하기로 했다. 아직까지는 대만족이다! 👍 하루 알고리즘 1문제를 풀어야 1일 커밋이 완료되기 때문에 이때까지는 문제풀이에 강박이 있었다. 알고리즘 문제를 풀지 못한다면 커밋이 안 된다는 불안감 때문에 신경이 많이 쓰였다. 이제는 다른 공부를 하더라도 해당 내용이 커밋되면서 커밋 기록이 초기화 되지 않는다. 강박 치료에 도움이 되지 않을까,,, 기대가 된다. 😀
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
Solution s = new Solution();
System.out.println(s.solution(getInput(br)));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static List<List<Integer>> getInput(BufferedReader br) throws IOException {
String[] tokens = br.readLine().split(" ");
int len = Integer.parseInt(tokens[0]);
int numOfNodes = Integer.parseInt(tokens[1]);
return connect(br, numOfNodes, getNetwork(len));
}
private static List<List<Integer>> getNetwork(int len) {
List<List<Integer>> network = new ArrayList<>();
for (int i = 0; i < len; i++) {
network.add(new ArrayList<>());
}
return network;
}
private static List<List<Integer>> connect(BufferedReader br, int numOfNodes, List<List<Integer>> network) throws IOException {
for (int i = 0; i < numOfNodes; i++) {
String[] tokens = br.readLine().split(" ");
int from = Integer.parseInt(tokens[0]);
int to = Integer.parseInt(tokens[1]);
network.get(from).add(to);
network.get(to).add(from);
}
return network;
}
}
class Solution {
public int solution(List<List<Integer>> network) {
Calculator c = new Calculator(network);
return c.getResult();
}
private class Calculator{
List<List<Integer>> network;
boolean[] isVisited;
int result = 0;
public Calculator(List<List<Integer>> network) {
this.network = network;
this.isVisited = new boolean[network.size()];
}
public int getResult() {
for (int i = 0; i < network.size(); i++) {
isVisited[i] = true;
DFS(i, 0);
isVisited[i] = false;
if (result == 1) break;
}
return result;
}
private void DFS(int position, int cnt) {
if (cnt == 4) {
result = 1;
return;
}
for (int nextFriend : network.get(position)) {
if(isVisited[nextFriend]) continue;
isVisited[nextFriend] = true;
DFS(nextFriend, cnt + 1);
isVisited[nextFriend] = false;
}
}
}
}
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
Input ip = getInput(br);
Solution s = new Solution();
System.out.println(s.solution(ip.board, ip.knight, ip.targets));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static Input getInput(BufferedReader br) throws IOException {
String[] tokens = br.readLine().split(" ");
int n = Integer.parseInt(tokens[0]);
int m = Integer.parseInt(tokens[1]);
int[][] targets = new int[m][2];
int[][] board = new int[n + 1][n + 1];
int[] knight = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
for (int i = 0; i < targets.length; i++) {
targets[i] = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
}
return new Input(board, knight, targets);
}
private static class Input {
int[][] board;
int[] knight;
int[][] targets;
public Input(int[][] board, int[] knight, int[][] targets) {
this.board = board;
this.knight = knight;
this.targets = targets;
}
}
}
class Solution {
public String solution(int[][] board, int[] knight, int[][] targets) {
Calculator c = new Calculator(board, knight, targets);
return c.getResult();
}
private class Calculator {
int[][] board;
boolean[][] isVisited;
int[] knight;
int[][] targets;
int[][] directions = {{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2}, {1, -2}, {1, 2}, {2, -1}, {2, 1}};
int[] result;
public Calculator(int[][] board, int[] knight, int[][] targets) {
this.board = board;
this.isVisited = new boolean[board.length][board[0].length];
this.knight = knight;
this.targets = targets;
this.result = new int[targets.length];
}
public String getResult() {
for (int[] target : targets) {
board[target[0]][target[1]] = -1;
}
BFS();
return getAnswer(result);
}
private String getAnswer(int[] arr) {
StringBuilder answer = new StringBuilder();
for (int num : arr) {
answer.append(num).append(" ");
}
return answer.toString();
}
private void BFS() {
Queue<Location> q = new ArrayDeque<>();
q.offer(new Location(knight[0], knight[1], 0));
isVisited[knight[0]][knight[1]] = true;
while (!q.isEmpty()) {
Location currentLocation = q.poll();
int currentX = currentLocation.x;
int currentY = currentLocation.y;
int currentStep = currentLocation.step;
if (board[currentX][currentY] == -1) {
addResult(currentX, currentY, currentStep);
}
for (int[] direction : directions) {
int nx = currentX + direction[0];
int ny = currentY + direction[1];
if (isWithinBoard(nx, ny) && !isVisited[nx][ny]) {
isVisited[nx][ny] = true;
q.offer(new Location(nx, ny, currentStep + 1));
}
}
}
}
private void addResult(int x, int y, int curStep) {
for (int i = 0; i < targets.length; i++) {
int[] candidate = targets[i];
if (candidate[0] == x && candidate[1] == y) {
result[i] = curStep;
return;
}
}
}
private boolean isWithinBoard(int x, int y) {
return 1 <= x && x < board.length && 1 <= y && y < board[x].length;
}
private class Location {
int x;
int y;
int step;
public Location(int x, int y, int step) {
this.x = x;
this.y = y;
this.step = step;
}
}
}
}