From d5ac7e83fa1d8ba29f9e9c6bf27328629922cb07 Mon Sep 17 00:00:00 2001 From: Jinyeong Seol Date: Mon, 16 Jun 2025 23:13:47 +0900 Subject: [PATCH] =?UTF-8?q?[20250616]=20BOJ=20/=20G4=20/=20=EC=8A=A4?= =?UTF-8?q?=EB=8F=84=EC=BF=A0=20/=20=EC=84=A4=EC=A7=84=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4 \354\212\244\353\217\204\354\277\240.md" | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 "Seol-JY/202506/16 BOJ G4 \354\212\244\353\217\204\354\277\240.md" diff --git "a/Seol-JY/202506/16 BOJ G4 \354\212\244\353\217\204\354\277\240.md" "b/Seol-JY/202506/16 BOJ G4 \354\212\244\353\217\204\354\277\240.md" new file mode 100644 index 00000000..529998f4 --- /dev/null +++ "b/Seol-JY/202506/16 BOJ G4 \354\212\244\353\217\204\354\277\240.md" @@ -0,0 +1,91 @@ +```java +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +class Main { + private static int[] horizontalBit = new int[9]; + private static int[] verticalBit = new int[9]; + private static int[] sectionBit = new int[9]; + + private static int[][] map = new int[9][9]; + + private static int[] emptyRows = new int[81]; + private static int[] emptyCols = new int[81]; + private static int emptyCount = 0; + + private static StringBuilder result = new StringBuilder(); + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + for (int i = 0; i < 9; i++) { + String line = br.readLine(); + for (int j = 0; j < 9; j++) { + int num = line.charAt(j) - '0'; + map[i][j] = num; + + if (num == 0) { + emptyRows[emptyCount] = i; + emptyCols[emptyCount] = j; + emptyCount++; + continue; + } + + int bitMask = 1 << (num - 1); + horizontalBit[i] |= bitMask; + verticalBit[j] |= bitMask; + sectionBit[getSectionNumber(i, j)] |= bitMask; + } + } + + solve(0); + + for (int i = 0; i < 9; i++) { + for (int j = 0; j < 9; j++) { + result.append(map[i][j]); + } + result.append('\n'); + } + System.out.print(result); + } + + private static boolean solve(int index) { + if (index == emptyCount) { + return true; + } + + int row = emptyRows[index]; + int col = emptyCols[index]; + int sectionNum = getSectionNumber(row, col); + + int usedBits = horizontalBit[row] | verticalBit[col] | sectionBit[sectionNum]; + + for (int num = 1; num <= 9; num++) { + int bitMask = 1 << (num - 1); + + if ((usedBits & bitMask) == 0) { + map[row][col] = num; + horizontalBit[row] |= bitMask; + verticalBit[col] |= bitMask; + sectionBit[sectionNum] |= bitMask; + + if (solve(index + 1)) { + return true; + } + + map[row][col] = 0; + horizontalBit[row] &= ~bitMask; + verticalBit[col] &= ~bitMask; + sectionBit[sectionNum] &= ~bitMask; + } + } + + return false; + } + + private static int getSectionNumber(int row, int col) { + return (row / 3) * 3 + (col / 3); + } +} +```