공부만 하는데도 시간이 너무 빨리 가는 만 28살 백수... 백수 된 지도 3달 차가 다 돼간다. 🐷 어서 취업해야 할 텐데... 걱정만 하고 꾸준히 노는데 집중하고 있다. 우주의 블랙홀처럼 집구석에만 머무르며 자신의 질량을 꾸준히 불려 나간 8월의 넷 째 주를 되돌아본다.
함께 자라기, 애자일로 가는 길
을 계속 읽고 있다.@RestControllerAdvice
를 사용한다.
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.start, ip.target, ip.savedPosition));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static Input getInput(BufferedReader br) throws IOException {
String[] tokens = br.readLine().split(" ");
int start = Integer.parseInt(tokens[0]);
int target = Integer.parseInt(tokens[1]);
int len = Integer.parseInt(br.readLine());
int[] savedPosition = new int[len];
for (int i = 0; i < savedPosition.length; i++) {
savedPosition[i] = Integer.parseInt(br.readLine());
}
return new Input(start, target, savedPosition);
}
private static class Input {
int start;
int target;
int[] savedPosition;
public Input(int start, int target, int[] savedPosition) {
this.start = start;
this.target = target;
this.savedPosition = savedPosition;
}
}
}
class Solution {
public int solution(int start, int target, int[] savedPosition) {
Calculator c = new Calculator(start, target, savedPosition);
return c.getResult();
}
}
class Calculator {
int start;
int target;
boolean[] isChecked;
Queue<Click> q;
public Calculator(int start, int target, int[] savedPosition) {
this.start = start;
this.target = target;
this.isChecked = new boolean[1_000];
this.q = new ArrayDeque<>();
q.offer(new Click(start, 0));
isChecked[start] = true;
for (int position : savedPosition) {
isChecked[position] = true;
q.offer(new Click(position, 1));
}
}
public int getResult() {
while (!q.isEmpty()) {
Click cur = q.poll();
if(cur.position == target) return cur.click;
else move(cur);
}
return -1;
}
private void move(Click cur) {
int[] nextPositions = {cur.position + 1, cur.position - 1};
for (int nextPosition : nextPositions) {
if (isValid(nextPosition) && !isChecked[nextPosition]) {
isChecked[nextPosition] = true;
q.offer(new Click(nextPosition, cur.click + 1));
}
}
}
private boolean isValid(int position) {
return 0 <= position && position < 1000;
}
private static class Click {
int position;
int click;
public Click(int position, int click) {
this.position = position;
this.click = click;
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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), getInput(br)));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static char[] getInput(BufferedReader br) throws IOException {
return br.readLine().toCharArray();
}
}
class Solution {
int[] scores = {3, 2, 1, 2, 3, 3, 2, 3, 3, 2, 2, 1, 2, 2, 1, 2, 2, 2, 1, 2, 1, 1, 1, 2, 2, 1};
public String solution(char[] his, char[] her) {
return getAnswer(process(getScoredName(his, her)));
}
private int[] getScoredName(char[] his, char[] her) {
int[] result = new int[his.length + her.length];
for (int i = 0; i < his.length; i++) {
result[2 * i] = scores[his[i] - 'A'];
result[2 * i + 1] = scores[her[i] - 'A'];
}
return result;
}
private int[] process(int[] nameScore) {
int repeat = nameScore.length;
while (repeat > 2) {
for (int i = 0; i < repeat - 1; i++) {
nameScore[i] = (nameScore[i] + nameScore[i + 1]) % 10;
}
repeat--;
}
return nameScore;
}
private String getAnswer(int[] nameScore) {
return "" + nameScore[0] + nameScore[1];
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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.startPlayer, ip.draws));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static Input getInput(BufferedReader br) throws IOException {
int startPlayer = Integer.parseInt(br.readLine());
int[][] draws = new int[9][2];
for (int i = 0; i < draws.length; i++) {
String[] tokens = br.readLine().split(" ");
int x = Integer.parseInt(tokens[0]);
int y = Integer.parseInt(tokens[1]);
draws[i][0] = x;
draws[i][1] = y;
}
return new Input(startPlayer, draws);
}
private static class Input {
int startPlayer;
int[][] draws;
public Input(int startPlayer, int[][] draws) {
this.startPlayer = startPlayer;
this.draws = draws;
}
}
}
class Solution {
public int solution(int startPlayer, int[][] draws) {
Game g = new Game(startPlayer);
for (int[] draw : draws) {
g.check(draw);
if(g.isDone()) return g.getWinner();
}
return 0;
}
}
class Game {
final int PLAYER_ONE = 1;
final int PLAYER_TWO = 2;
int prevPlayer;
int[][] field;
int winner = 0;
public Game(int startPlayer) {
this.prevPlayer = startPlayer == PLAYER_ONE ? PLAYER_TWO : PLAYER_ONE;
this.field = new int[4][4];
}
public void check(int[] draw) {
int x = draw[0];
int y = draw[1];
int player = getPlayer();
field[x][y] = player;
prevPlayer = player;
}
public boolean isDone() {
return isLineDone() || isDiagonalDone();
}
public int getWinner() {
return winner;
}
private boolean isLineDone() {
for (int i = 1; i < field.length; i++) {
int[] horizontalChecker = new int[3];
int[] verticalChecker = new int[3];
for (int j = 1; j < field.length; j++) {
horizontalChecker[field[i][j]]++;
verticalChecker[field[j][i]]++;
}
if(isWinnerExist(horizontalChecker)) return true;
if(isWinnerExist(verticalChecker)) return true;
}
return false;
}
private boolean isDiagonalDone() {
int[] checker = new int[3];
int[] reversedChecker = new int[3];
for (int i = 1; i < field.length; i++) {
checker[field[i][i]]++;
reversedChecker[field[i][4 - i]]++;
}
return isWinnerExist(checker) || isWinnerExist(reversedChecker);
}
private boolean isWinnerExist(int[] checker) {
for (int i = 1; i < 3; i++) {
if (checker[i] == 3) {
winner = i;
return true;
}
}
return false;
}
private int getPlayer() {
return prevPlayer == PLAYER_ONE ? PLAYER_TWO : PLAYER_ONE;
}
}