[BOJ] 2615.μ€λͺ© (Java)
by λλΆλ
2615λ²: μ€λͺ©
μ€λͺ©μ λ°λνμ κ²μ λ°λμκ³Ό ν° λ°λμμ κ΅λλ‘ λμμ 겨루λ κ²μμ΄λ€. λ°λνμλ 19κ°μ κ°λ‘μ€κ³Ό 19κ°μ μΈλ‘μ€μ΄ κ·Έλ €μ Έ μλλ° κ°λ‘μ€μ μμμλΆν° μλλ‘ 1λ², 2λ², ... ,19λ²μ λ²νΈ
www.acmicpc.net
λ°λνμ 19κ°μ κ°λ‘μ€κ³Ό 19κ°μ μΈλ‘μ€μ΄ κ·Έλ €μ Έ μμ λ, κ°μ μμ λ°λμμ΄ μ°μμ μΌλ‘ λ€μ― μ λμ΄λ©΄ μ΄κΈ°κ² λλ κ²μμ λλ€.
μ¬κΈ°μ μ°μμ μΌλ‘λ κ°λ‘, μΈλ‘ λλ λκ°μ λ°©ν₯μ λ»νλ©°, μ¬μ― μ μ΄μμ΄ μ°μμ μΌλ‘ λμ κ²½μ°μλ μ΄κΈ΄ κ²μ΄ μλλλ€.
λ°λν μνκ° μ£Όμ΄μ‘μ λ, κ²μμμ 1, ν°μμ 2, μμ΄ λμ΄μ§ μμ μ리λ 0μΌλ‘ νμν©λλ€.
κ²μμμ΄ μ΄κΈ°λ©΄ 1, ν°μμ΄ μ΄κΈ°λ©΄ 2, μμ§ μΉλΆκ° κ²°μ λμ§ μμΌλ©΄ 0μ μΆλ ₯ν©λλ€.
κ·Έλ¦¬κ³ κ²μμ λλ ν°μμ΄ μ΄κ²Όμ κ²½μ°, λμ§Έ μ€μ μ°μλ λ€μ― κ°μ λ°λμ μ€ κ°μ₯ μΌμͺ½μ μλ λ°λμμ κ°λ‘μ€, μΈλ‘μ€ λ²νΈλ₯Ό μμλλ‘ μΆλ ₯ν©λλ€. μ°μλ λ€μ― κ°μ λ°λμμ΄ μΈλ‘λ‘ λμΈ κ²½μ°, κ·Έ μ€ κ°μ₯ μμ μλ λ°λμμ κ°λ‘μ€, μΈλ‘μ€ λ²νΈλ₯Ό μμλλ‘ μΆλ ₯ν©λλ€.
λ¬Έμ μμ½
1. κ°λ‘, μΈλ‘ λλ λκ°μ λ°©ν₯μΌλ‘ λ°λμμ΄ μ°μμ μΌλ‘ λ€μ― κ°κ° λμ΄λ κ²½μ° μ°ΎκΈ°
2. μ¬μ― μ μ΄μμ΄ μ°μμ μΌλ‘ λμ΄λ κ²½μ° μ΄κΈ΄ κ²μ΄ μλ
3. κ²μμμ΄ μ΄κΈ°λ©΄ 1, ν°μμ΄ μ΄κΈ°λ©΄ 2, μμ§ μΉλΆκ° κ²°μ λμ§ μμΌλ©΄ 0 μΆλ ₯
4. μ°μλ λ€μ― κ° λ°λμ μ€ κ°μ₯ μΌμͺ½μ μλ λ°λμ μμΉ μΆλ ₯ (μΈλ‘λ‘ λμ΄λ©΄, κ°μ₯ μμͺ½ λ°λμ μμΉ μΆλ ₯)
λ¬Έμ νμ΄
μ²μ λ°λνμ μνλ₯Ό λΆλ¬μ¬ λ, κ²μμ λ°λμμ μμΉμ ν°μ λ°λμμ μμΉλ₯Ό 리μ€νΈμ μ μ₯ν΄λ‘λλ€.
κ·Έλ¦¬κ³ κ²μμ λ°λμ μμΉ λ¦¬μ€νΈμ ν°μ λ°λμ μμΉ λ¦¬μ€νΈλ₯Ό μμλλ‘ νμΈνλ©° λ€μ― μμ΄ μ°μμ μΌλ‘ λμ΄λμ§ νμΈν©λλ€.
μ¬κΈ°μ μ λ λ€μ―μμ΄ λμΈ μνμΌ λ, μΆλ°ν μμΉ μμͺ½μ λ°λμμ΄ μλμ§ νμΈνμ¬ 6κ° μ΄μμ΄ λλμ§ νμΈνμ΅λλ€.
import java.util.*;
class Position {
int row;
int col;
Position(int row, int col) {
this.row = row;
this.col = col;
}
}
public class Main {
public static List<Position> solution(int[][] board, List<Position> positions, int color) {
int[][] direction = {{0, 1}, {1, -1}, {1, 0}, {1, 1}};
int[][] checkDirection = {{0, -1}, {-1, 1}, {-1, 0}, {-1, -1}};
int count;
boolean isBreak = false;
boolean isExistence;
List<Position> winPosition = null;
Queue<Position> queue = new LinkedList<>();
for (Position position : positions) {
if (isBreak) break;
for (int i = 0; i < 4; i++) {
winPosition = new ArrayList<>();
queue.add(position);
count = 0;
while (!queue.isEmpty()) {
Position pos = queue.poll();
winPosition.add(new Position(pos.row, pos.col));
count++;
int nextRow = pos.row + direction[i][0];
int nextCol = pos.col + direction[i][1];
if (0 <= nextRow && nextRow < 19 && 0 <= nextCol && nextCol < 19) {
if (board[nextRow][nextCol] == color) {
queue.add(new Position(nextRow, nextCol));
}
}
}
if (count == 5) {
int checkRow = position.row + checkDirection[i][0];
int checkCol = position.col + checkDirection[i][1];
isExistence = false;
// μ€λͺ©μ΄ λ μμμ μμ λμ΄ μλμ§ νμΈνμ¬ μ‘λͺ© μ΄μμΈμ§ 체ν¬
if (0 <= checkRow && checkRow < 19 && 0 <= checkCol && checkCol < 19) {
if (board[checkRow][checkCol] == color) {
isExistence = true;
}
}
if (!isExistence) {
isBreak = true;
break;
}
}
}
}
return winPosition;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[][] board = new int[19][19];
List<Position> blackPos = new ArrayList<>();
List<Position> whitePos = new ArrayList<>();
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19; j++) {
int temp = input.nextInt();
board[i][j] = temp;
if (temp == 1) {
blackPos.add(new Position(i, j));
} else if (temp == 2) {
whitePos.add(new Position(i, j));
}
}
}
List<Position> blackWinPosition = solution(board, blackPos, 1);
List<Position> whiteWinPosition = solution(board, whitePos, 2);
if (blackWinPosition != null && blackWinPosition.size() == 5) {
System.out.println(1);
blackWinPosition.sort(((o1, o2) -> o1.col - o2.col));
System.out.println((blackWinPosition.get(0).row + 1) + " " + (blackWinPosition.get(0).col + 1));
} else if (whiteWinPosition != null && whiteWinPosition.size() == 5) {
System.out.println(2);
whiteWinPosition.sort(((o1, o2) -> o1.col - o2.col));
System.out.println((whiteWinPosition.get(0).row + 1) + " " + (whiteWinPosition.get(0).col + 1));
} else {
System.out.println(0);
}
}
}
'Algorithm > BOJ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[BOJ] 2660.νμ₯ λ½κΈ° (Java, BFS) (0) | 2023.01.01 |
---|---|
[BOJ] 2016.λ―Έν μ£Όμ νκΈ° (0) | 2022.06.13 |
[BOJ] 1759.μνΈλ§λ€κΈ° (Java) (0) | 2021.06.20 |
[BOJ] 12094.Aμ B (Java) (0) | 2021.06.10 |
[BOJ] 10159.μ μΈ (Java) (0) | 2021.05.02 |
λΈλ‘κ·Έμ μ 보
λλΆλμ Devlog
λλΆλ