diff --git a/problems/SWEA/p1226/Solution.java b/problems/SWEA/p1226/Solution.java new file mode 100644 index 0000000..0896588 --- /dev/null +++ b/problems/SWEA/p1226/Solution.java @@ -0,0 +1,186 @@ +/* + * (1226) [S/W 문제해결 기본] 7일차 - 미로1 + * https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14vXUqAGMCFAYD + */ + +package swea.p1226; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.util.ArrayDeque; +import java.util.Objects; +import java.util.Queue; + +/** + * SW Export Academy - 1226. [S/W 문제해결 기본] 7일차 - 미로1 + * @author YeJun, Jung + * + * @see #main(String[]) + * 1. 입출력을 초기화한다. + * 2. 테스트 케이스를 입력받는다. + * 3. 솔루션을 실행한다. + * + * @see #Solution(int) + * 4. 멤버 변수를 초기화 한다. + * + * @see #run() + * 5. 입력, 해결, 출력 순서로 실행한다. + * + * @see #input() + * 6. 미로를 저장할 board 배열을 초기화한다. + * 7. 미로를 입력받아 board 배열에 저장한다. + * 7-1. 출발지와 도착지를 발견하면 저장해둔다. + * + * @see #solve() + * 8. 정답 변수 answer를 초기화 한다. + * 9. 좌표를 보관하는 큐를 선언한다. + * 9-1. 큐에 시작 좌표를 추가한다. + * 10. BFS 방식으로 미로를 탐사한다. + * 10-1. 큐의 가장 앞에 있는 요소를 꺼낸다. + * 10-2. 상하좌우 4방향으로 미로를 이동하면서 탐사한다. + * 10-3. 목적지에 도착했다면 정답을 기록하고 탐사를 중단한다. + * 10-4. 길이 존재한다면 큐에 넣는다. + * 10-5. 큐에 넣은 길은 방문 처리 한다. + * + * @see #print() + * 11. 정답을 출력한다. + */ +public class Solution { + static BufferedReader reader; + static BufferedWriter writer; + + public static void main(String[] args) throws IOException { + // 1. 입출력을 초기화한다. + reader = new BufferedReader(new InputStreamReader(System.in)); + writer = new BufferedWriter(new OutputStreamWriter(System.out)); + + final int testCount = 10; + + for (int count = 1; count <= testCount; count++) { + // 2. 테스트 케이스를 입력받는다. + int testCase = Integer.parseInt(reader.readLine().trim()); + + // 3. 솔루션을 실행한다. + Solution solution = new Solution(testCase); + solution.run(); + } + } + + // ---------------------------------------------------------------- + + static final int BOARD_SIZE = 16; + + static final int[] DIR_X = { -1, 1, 0, 0 }; + static final int[] DIR_Y = { 0, 0, -1, 1 }; + static final int DIR_LEN = 4; + + private int testCase; + private int answer; + + private char[][] board; + + private Pos current; + private Pos goal; + + public Solution(int testCase) { + // 4. 멤버 변수를 초기화 한다. + this.testCase = testCase; + } + + public void run() throws IOException { + // 5. 입력, 해결, 출력 순서로 실행한다. + input(); + solve(); + print(); + } + + private void input() throws IOException { + // 6. 미로를 저장할 board 배열을 초기화한다. + board = new char[BOARD_SIZE][0]; + + for (int y = 0; y < BOARD_SIZE; y++) { + // 7. 미로를 입력받아 board 배열에 저장한다. + board[y] = reader.readLine().trim().toCharArray(); + + // 7-1. 출발지와 도착지를 발견하면 저장해둔다. + for (int x = 0; x < BOARD_SIZE; x++) { + if (board[y][x] == '2') { + current = new Pos(x, y); + } else if (board[y][x] == '3') { + goal = new Pos(x, y); + } + } + } + } + + private void solve() { + // 8. 정답 변수 answer를 초기화 한다. + answer = 0; + + // 9. 좌표를 보관하는 큐를 선언한다. + Queue q = new ArrayDeque<>(); + q.offer(current); // 9-1. 큐에 시작 좌표를 추가한다. + + // 10. BFS 방식으로 미로를 탐사한다. + while (!q.isEmpty()) { + // 10-1. 큐의 가장 앞에 있는 요소를 꺼낸다. + Pos peek = q.poll(); + + // 10-2. 상하좌우 4방향으로 미로를 이동하면서 탐사한다. + for (int index = 0; index < DIR_LEN; index++) { + Pos next = new Pos( + peek.x + DIR_X[index], + peek.y + DIR_Y[index] + ); + + if (next.equals(goal)) { + // 10-3. 목적지에 도착했다면 정답을 기록하고 탐사를 중단한다. + answer = 1; + return; + } else if (board[next.y][next.x] == '0') { + // 10-4. 길이 존재한다면 큐에 넣는다. + q.offer(next); + + // 10-5. 큐에 넣은 길은 방문 처리 한다. + board[next.y][next.x] = '1'; + } + } + } + } + + private void print() throws IOException { + // 11. 정답을 출력한다. + writer.write("#" + testCase + " " + answer + "\n"); + writer.flush(); + } +} + +/** + * Pos + * @author YeJun, Jung + * + * 2차원 좌표를 저장하기 위한 클래스, 객체간의 비교가 가능하다. + */ +class Pos { + public int x; + public int y; + + public Pos(int x, int y) { + this.x = x; + this.y = y; + } + + @Override + public boolean equals(Object obj) { + Pos another = (Pos)obj; + return x == another.x && y == another.y; + } + + @Override + public int hashCode() { + return Objects.hash(x, y); + } +} diff --git a/problems/SWEA/p1228/Solution.java b/problems/SWEA/p1228/Solution.java new file mode 100644 index 0000000..ca05e45 --- /dev/null +++ b/problems/SWEA/p1228/Solution.java @@ -0,0 +1,156 @@ +/* + * (1228) [S/W 문제해결 기본] 8일차 - 암호문1 + * https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14w-rKAHACFAYD&categoryId=AV14w-rKAHACFAYD&categoryType=CODE&problemTitle=1228&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1 + */ + +package swea.p1228; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.ListIterator; +import java.util.StringTokenizer; + +/** + * SW Expert Academy - 1228. [S/W 문제해결 기본] 8일차 - 암호문1 + * @author YeJun. Jung + * + * @see #main(String[]) + * 1. 입출력을 초기화한다. + * 2. 테스트 케이스를 입력받는다. + * 3. 솔루션을 실행한다. + * + * @see #Solution(int) + * 4. 멤버 변수를 초기화 한다. + * + * @see #run() + * 5. 입력, 해결, 출력 순서로 실행한다. + * + * @see #input() + * 6. 원본 암호문의 길이를 originalSize 변수에 저장한다. + * 7. 원본 암호문 내용을 original 배열에 저장한다. + * + * @see #solve() + * 8. 시뮬레이션을 진행할 연결 리스트 memory 객체를 준비한다. + * 9. 원본 암호문 내용을 memory에 올린다. + * 10. 명령어의 개수를 commandSize 변수에 저장한다. + * 11. 한 줄을 입력받아 띄어쓰기를 기준으로 분리한다. + * 12. 명령어에 따라 시뮬레이션을 시작한다. + * 12-1. 명령어를 opcode, index, codeCount로 분리한다. + * 12-2. 버퍼에 삽입할 숫자를 입력한다. + * 12-3. 버퍼 내용을 memory 객체의 index에 삽입한다. + * 13. memory 내용을 iterator로 변환한다. + * 14. 정답으로 출력할 내용의 개수를 계산한다. + * 15. 정답 배열에 memory의 내용을 최대 10개까지 저장한다. + * + * @see #print() + * 16. 정답 배열 내용을 화면에 출력한다. + */ +public class Solution { + static BufferedReader reader; + static BufferedWriter writer; + + public static void main(String[] args) throws IOException { + // 1. 입출력을 초기화한다. + reader = new BufferedReader(new InputStreamReader(System.in)); + writer = new BufferedWriter(new OutputStreamWriter(System.out)); + + final int testCount = 10; + + // 2. 테스트 케이스를 입력받는다. + for (int testCase = 1; testCase <= testCount; testCase++) { + // 3. 솔루션을 실행한다. + Solution solution = new Solution(testCase); + solution.run(); + } + } + + // ---------------------------------------------------------------- + + private int testCase; + private String[] answer; + + private int originalSize; + private String[] original; + + public Solution(int testCase) { + // 4. 멤버 변수를 초기화 한다. + this.testCase = testCase; + } + + public void run() throws IOException { + // 5. 입력, 해결, 출력 순서로 실행한다. + input(); + solve(); + print(); + } + + private void input() throws IOException { + // 6. 원본 암호문의 길이를 originalSize 변수에 저장한다. + originalSize = Integer.parseInt(reader.readLine().trim()); + + // 7. 원본 암호문 내용을 original 배열에 저장한다. + original = new String[originalSize]; + + StringTokenizer inputOrignalTokenizer= new StringTokenizer(reader.readLine().trim()); + for (int index = 0; index < originalSize; index++) { + original[index] = inputOrignalTokenizer.nextToken(); + } + } + + private void solve() throws IOException { + // 8. 시뮬레이션을 진행할 연결 리스트 memory 객체를 준비한다. + LinkedList memory = new LinkedList<>(); + + // 9. 원본 암호문 내용을 memory에 올린다. + for (String code : original) memory.add(code); + + // 10. 명령어의 개수를 commandSize 변수에 저장한다. + int commandSize = Integer.parseInt(reader.readLine().trim()); + // 11. 한 줄을 입력받아 띄어쓰기를 기준으로 분리한다. + StringTokenizer inputCommandTokenizer = new StringTokenizer(reader.readLine().trim()); + + // 12. 명령어에 따라 시뮬레이션을 시작한다. + for (int commandCount = 0; commandCount < commandSize; commandCount++) { + // 12-1. 명령어를 opcode, index, codeCount로 분리한다. + String opcode = inputCommandTokenizer.nextToken(); + int index = Integer.parseInt(inputCommandTokenizer.nextToken()); + int codeCount = Integer.parseInt(inputCommandTokenizer.nextToken()); + + // 12-2. 버퍼에 삽입할 숫자를 입력한다. + String[] buffer = new String[codeCount]; + for (int codeIndex = 0; codeIndex < codeCount; codeIndex++) { + buffer[codeIndex] = inputCommandTokenizer.nextToken(); + } + + // 12-3. 버퍼 내용을 memory 객체의 index에 삽입한다. + memory.addAll(index, Arrays.asList(buffer)); + } + + // 13. memory 내용을 iterator로 변환한다. + ListIterator iterator = memory.listIterator(); + + // 14. 정답으로 출력할 내용의 개수를 계산한다. + final int answerCount = Math.min(10, memory.size()); + answer = new String[answerCount]; + + // 15. 정답 배열에 memory의 내용을 최대 10개까지 저장한다. + for (int index = 0; index < answerCount; index++) { + answer[index] = iterator.next(); + } + } + + private void print() throws IOException { + // 16. 정답 배열 내용을 화면에 출력한다. + writer.write("#" + testCase); + for (int index = 0; index < 10; index++) { + writer.write(" " + answer[index]); + } + writer.write("\n"); + writer.flush(); + } +} diff --git a/problems/SWEA/p1229/Solution.java b/problems/SWEA/p1229/Solution.java new file mode 100644 index 0000000..06d02fc --- /dev/null +++ b/problems/SWEA/p1229/Solution.java @@ -0,0 +1,160 @@ +/* + * (1229) [S/W 문제해결 기본] 8일차 - 암호문2 + * https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14yIsqAHYCFAYD&categoryId=AV14yIsqAHYCFAYD&categoryType=CODE&problemTitle=1229&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1 + */ + +package swea.p1229; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.StringTokenizer; + +/** + * SW Expert Academy - 1229. [S/W 문제해결 기본] 8일차 - 암호문2 + * @author YeJun. Jung + * + * @see #main(String[]) + * 1. 입출력을 초기화한다. + * 2. 테스트 케이스를 입력받는다. + * 3. 솔루션을 실행한다. + * + * @see #Solution(int) + * 4. 멤버 변수를 초기화 한다. + * + * @see #run() + * 5. 입력, 해결, 출력 순서로 실행한다. + * + * @see #input() + * 6. 원본 암호문의 길이를 originalSize 변수에 저장한다. + * 7. 원본 암호문 내용을 original 배열에 저장한다. + * + * @see #solve() + * 8. 시뮬레이션을 진행할 연결 리스트 memory 객체를 준비한다. + * 9. 원본 암호문 내용을 memory에 올린다. + * 10. 명령어의 개수를 commandLength 변수에 저장한다. + * 11. 한 줄을 입력받아 띄어쓰기를 기준으로 분리한다. + * 12. 명령어에 따라 시뮬레이션을 시작한다. + * 12-1. 명령어를 opcode, index, count로 분리한다. + * 12-2. opcode가 I라면 버퍼를 입력받아 memory객체의 index에 삽입한다. + * 12-3. opcode가 D라면 memory 객체의 index부터 count 개수 만큼 삭제한다. + * 13. memory 내용을 iterator로 변환한다. + * 14. 정답으로 출력할 내용의 개수를 계산한다. + * 15. 정답 배열에 memory의 내용을 최대 10개까지 저장한다. + * + * @see #print() + * 16. 정답 배열 내용을 화면에 출력한다. + */ +public class Solution { + static BufferedReader reader; + static BufferedWriter writer; + + public static void main(String[] args) throws IOException { + // 1. 입출력을 초기화한다. + reader = new BufferedReader(new InputStreamReader(System.in)); + writer = new BufferedWriter(new OutputStreamWriter(System.out)); + + final int testCount = 10; + + // 2. 테스트 케이스를 입력받는다. + for (int testCase = 1; testCase <= testCount; testCase++) { + // 3. 솔루션을 실행한다. + Solution solution = new Solution(testCase); + solution.run(); + } + } + + // -------------------------------------------------------- + + int testCase; + String[] answer; + + int originalSize; + String[] original; + + public Solution(int testCase) { + // 4. 멤버 변수를 초기화 한다. + this.testCase = testCase; + } + + public void run() throws IOException { + // 5. 입력, 해결, 출력 순서로 실행한다. + input(); + solve(); + print(); + } + + private void input() throws IOException { + // 6. 원본 암호문의 길이를 originalSize 변수에 저장한다. + originalSize = Integer.parseInt(reader.readLine().trim()); + + // 7. 원본 암호문 내용을 original 배열에 저장한다. + original = new String[originalSize]; + + StringTokenizer codeTokenizer = new StringTokenizer(reader.readLine().trim()); + for (int index = 0; index < originalSize; index++) { + original[index] = codeTokenizer.nextToken(); + } + } + + private void solve() throws IOException { + // 8. 시뮬레이션을 진행할 연결 리스트 memory 객체를 준비한다. + LinkedList memory = new LinkedList<>(); + + // 9. 원본 암호문 내용을 memory에 올린다. + for (String code : original) memory.add(code); + + // 10. 명령어의 개수를 commandLength 변수에 저장한다. + int commandLength = Integer.parseInt(reader.readLine().trim()); + // 11. 한 줄을 입력받아 띄어쓰기를 기준으로 분리한다. + StringTokenizer commandTokenizer = new StringTokenizer(reader.readLine().trim()); + + // 12. 명령어에 따라 시뮬레이션을 시작한다. + for (int commandIndex = 0; commandIndex < commandLength; commandIndex++) { + // 12-1. 명령어를 opcode, index, count로 분리한다. + String opcode = commandTokenizer.nextToken(); + int index = Integer.parseInt(commandTokenizer.nextToken()); + int count = Integer.parseInt(commandTokenizer.nextToken()); + + if (opcode.equals("I")) { + // 12-2. opcode가 I라면 버퍼를 입력받아 memory객체의 index에 삽입한다. + List buffer = new ArrayList<>(); + for (int current = 0; current < count; current++) { + buffer.add(commandTokenizer.nextToken()); + } + memory.addAll(index, buffer); + } else if (opcode.equals("D")) { + // 12-3. opcode가 D라면 memory 객체의 index부터 count 개수 만큼 삭제한다. + for (int current = 0; current < count; current++) { + memory.remove(index); + } + } + } + + // 13. memory 내용을 iterator로 변환한다. + Iterator iter = memory.iterator(); + + // 14. 정답으로 출력할 내용의 개수를 계산한다. + int answerCount = Math.min(10, memory.size()); + answer = new String[answerCount]; + + // 15. 정답 배열에 memory의 내용을 최대 10개까지 저장한다. + for (int index = 0; index < answerCount; index++) { + answer[index] = iter.next(); + } + } + + private void print() throws IOException { + // 16. 정답 배열 내용을 화면에 출력한다. + writer.write("#" + testCase); + for (String code : answer) writer.write(" " + code); + writer.write("\n"); + writer.flush(); + } +} diff --git a/problems/SWEA/p2001/Solution.java b/problems/SWEA/p2001/Solution.java new file mode 100644 index 0000000..ceb3e4b --- /dev/null +++ b/problems/SWEA/p2001/Solution.java @@ -0,0 +1,135 @@ +/* + * (2001) 파리 퇴치 + * https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5PzOCKAigDFAUq&categoryId=AV5PzOCKAigDFAUq&categoryType=CODE&problemTitle=2001&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1 + */ + +package swea.p2001; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.util.StringTokenizer; + +/** + * SW Expert Academy - 2001. 파리 퇴치 + * @author YeJun. Jung + * + * @see #main(String[]) + * 1. 입출력을 초기화한다. + * 2. 테스트 케이스를 입력받는다. + * 3. 솔루션을 실행한다. + * + * @see #Solution(int) + * 4. 멤버 변수를 초기화 한다. + * + * @see #run() + * 5. 입력, 해결, 출력 순서로 실행한다. + * + * @see #input() + * 6. 한줄을 입력받아 띄워쓰기를 기준으로 분리하고 각각 boardSize, cursorSize 변수에 저장한다. + * 7. boardSize를 +1 늘려서 패딩을 추가한다. + * 8. cursorSize를 -1 줄여서 현재 좌표를 기준으로한 크기로 수정한다. + * 9. 파리의 위치 정보를 저장할 board 2차원 배열을 준비한다. + * 10. board 배열을 입력받는다. + * + * @see #solve() + * 11. 누적합 행렬을 만든다. + * 12. 정답 변수인 answer를 초기화 한다. + * 13. board 배열을 순회하면서 가장 큰 부분합을 찾는다. + * + * @see #print() + * 14. 정답 배열 내용을 화면에 출력한다. + */ +public class Solution { + static BufferedReader reader; + static BufferedWriter writer; + + public static void main(String[] args) throws IOException { + // 1. 입출력을 초기화한다. + reader = new BufferedReader(new InputStreamReader(System.in)); + writer = new BufferedWriter(new OutputStreamWriter(System.out)); + + // 2. 테스트 케이스를 입력받는다. + int testCount = Integer.parseInt(reader.readLine().trim()); + + for (int testCase = 1; testCase <= testCount; testCase++) { + // 3. 솔루션을 실행한다. + Solution solution = new Solution(testCase); + solution.run(); + } + } + + // -------------------------------------------------------- + + int testCase; + int answer; + + int boardSize; + int cursorSize; + int[][] board; + + public Solution(int testCase) { + // 4. 멤버 변수를 초기화 한다. + this.testCase = testCase; + } + + public void run() throws IOException { + // 5. 입력, 해결, 출력 순서로 실행한다. + input(); + solve(); + print(); + } + + private void input() throws IOException { + // 6. 한줄을 입력받아 띄워쓰기를 기준으로 분리하고 각각 boardSize, cursorSize 변수에 저장한다. + StringTokenizer line; + line = new StringTokenizer(reader.readLine().trim()); + + boardSize = Integer.parseInt(line.nextToken()); + cursorSize = Integer.parseInt(line.nextToken()); + + boardSize++; // 7. boardSize를 +1 늘려서 패딩을 추가한다. + cursorSize--; // 8. cursorSize를 -1 줄여서 현재 좌표를 기준으로한 크기로 수정한다. + + // 9. 파리의 위치 정보를 저장할 board 2차원 배열을 준비한다. + board = new int[boardSize][boardSize]; + + // 10. board 배열을 입력받는다. + for (int y = 1; y < boardSize; y++) { + line = new StringTokenizer(reader.readLine().trim()); + + for (int x = 1; x < boardSize; x++) { + board[y][x] = Integer.parseInt(line.nextToken()); + } + } + } + + private void solve() { + // 11. 누적합 행렬을 만든다. + for (int y = 1; y < boardSize; y++) + for (int x = 1; x < boardSize; x++) + board[y][x] += board[y - 1][x] + board[y][x - 1] - board[y - 1][x - 1]; + + // 12. 정답 변수인 answer를 초기화 한다. + answer = 0; + + // 13. board 배열을 순회하면서 가장 큰 부분합을 찾는다. + for (int y = 1; y < boardSize - cursorSize; y++) { + for (int x = 1; x < boardSize - cursorSize; x++) { + int current = board[y + cursorSize][x + cursorSize]; + current -= board[y + cursorSize][x - 1] + board[y - 1][x + cursorSize]; + current += board[y - 1][x - 1]; + + answer = Math.max(answer, current); + } + } + } + + private void print() throws IOException { + // 14. 정답 배열 내용을 화면에 출력한다. + writer.write("#" + testCase + " " + answer + "\n"); + writer.flush(); + } +} diff --git a/problems/programmers/others/p87390/Solution.java b/problems/programmers/others/p87390/Solution.java new file mode 100644 index 0000000..19cbef0 --- /dev/null +++ b/problems/programmers/others/p87390/Solution.java @@ -0,0 +1,24 @@ +/* + * (87390) n^2 배열 자르기 + * https://school.programmers.co.kr/learn/courses/30/lessons/87390 + */ + +import java.util.Arrays; // for debugging + +public class Solution { + public int[] solution(int n, long left, long right) { + int[] answer = new int[(int)(right - left) + 1]; // right - left < 100000 + int index = 0; + + for (long x = left; x <= right; x++) { + answer[index++] = 1 + Math.max((int)(x / (long)n), (int)(x % (long)n)); + } + + return answer; + } + + public static void main(String[] args) { + System.out.println(Arrays.toString(new Solution().solution(3, 2, 5))); // [3, 2, 2, 3] + System.out.println(Arrays.toString(new Solution().solution(4, 7, 14))); // [4, 3, 3, 3, 4, 4, 4, 4] + } +} diff --git a/tools/github-bot/message.json b/tools/github-bot/message.json index fea7755..13b8eba 100644 --- a/tools/github-bot/message.json +++ b/tools/github-bot/message.json @@ -14,6 +14,13 @@ "type": "list", "message": "[x] $File[1][12:]$ ([$FileName$]($File[2][3:]$))" }, + { + "path": "problems/programmers", + "title": "## 프로그래머스 문제풀이(Java)", + "target": ["\\\\*.java$"], + "type": "list", + "message": "[x] $File[1][11:]$ ([$File[1][4:]$]($File[2][3:]$))" + }, { "path": "notes/", "title": "## 공부노트 추가",