λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

Algorithm/BOJ

[BOJ] μŠ€λ„μΏ (2580)

[λ°±μ€€(BOJ)] μŠ€λ„μΏ (2580) C++

문제 : BOJ_2580번 μŠ€λ„μΏ 

μž…μΆœλ ₯ ν˜•μ‹λ§Œ 살짝 λ‹€λ₯Έ 같은 문제둜 BOJ_2239번 μŠ€λ„μΏ λ„ μžˆμŠ΅λ‹ˆλ‹€.

문제 μ„€λͺ…

λ°±νŠΈλž˜ν‚Ή, dfs

μ™„μ„±λ˜μ§€ μ•Šμ€ μŠ€λ„μΏ κ°€ 주어지고, 이λ₯Ό μ™„μ„±ν•˜λŠ” λ¬Έμ œμž…λ‹ˆλ‹€.

μŠ€λ„μΏ μ˜ μˆ˜κ°€ ν™•μ •λ˜μ§€ μ•Šμ€ 곳은 0으둜 μž…λ ₯λ˜μ–΄ 있고, 이 0듀을 μ „λΆ€ μ±„μš΄ μŠ€λ„μΏ νŒμ„ 좜λ ₯ν•΄μ•Ό ν•©λ‹ˆλ‹€.

λ¬Έμ œλŠ” μŠ€νŽ˜μ…œ μ €μ§€μ΄λ―€λ‘œ, ν•˜λ‚˜μ˜ κ²½μš°κ°€ μ•„λ‹Œ μ—¬λŸ¬ κ²½μš°κ°€ 닡이 될 수 μžˆλŠ” μŠ€λ„μΏ λŠ” 그쀑 ν•˜λ‚˜λ₯Ό 좜λ ₯ν•˜λ©΄ μ •λ‹΅ μ²˜λ¦¬κ°€ λ©λ‹ˆλ‹€.

dfs와 λ°±νŠΈλž˜ν‚Ήμ„ μ΄μš©ν•΄μ•Ό ν•©λ‹ˆλ‹€.


Solution

μž…λ ₯ λ²ˆν˜Έκ°€ 0인 곳의 μ’Œν‘œλ“€μ„ ν•˜λ‚˜ν•˜λ‚˜ μ €μž₯ν•©λ‹ˆλ‹€.

μ €μž₯된 μ’Œν‘œλ₯Ό ν•˜λ‚˜μ”© dfs둜 μˆœνšŒν•©λ‹ˆλ‹€.

μ΄λ•Œ ν˜„μž¬ 순회 쀑인 곳에 λ§žλŠ” λ²ˆν˜Έκ°€ 있으면 λ‹€μŒ 순회둜 μ§„ν–‰ν•˜κ³ , 1~9 λͺ¨λ“  λ²ˆν˜Έκ°€ λ‹€ μ€‘λ³΅λœλ‹€λ©΄ 이전 순회둜 λŒμ•„κ°€μ„œ λ‹€λ₯Έ 수λ₯Ό μž…λ ₯ν•˜λŠ” λ°±νŠΈλž˜ν‚Ή 방식을 μ‚¬μš©ν•΄μ•Ό ν•©λ‹ˆλ‹€.

0인 각 μ’Œν‘œλ₯Ό λŒλ©΄μ„œ μŠ€λ„μΏ μ˜ κ·œμΉ™μ— 맞게 μ’Œν‘œμ˜ μ„Έλ‘œμΆ•λ“€, κ°€λ‘œμΆ•λ“€μ— μΈμ ‘ν•œ κ³³λ“€μ˜ 번호λ₯Ό ν™•μΈν•˜μ—¬ μ²΄ν¬ν•©λ‹ˆλ‹€.

λ˜ν•œ 각 μ’Œν‘œκ°€ μ†ν•œ 3*3 μ‚¬μ΄μ¦ˆμ˜ μ‚¬κ°ν˜•λ„ μˆœνšŒν•˜λ©° 번호λ₯Ό ν™•μΈν•˜κ³  체크해 μ£Όμ–΄μ•Ό ν•©λ‹ˆλ‹€.


Description

  • 0인 각 μ’Œν‘œλŠ” vector<pair<int, int>> zeroPointλ₯Ό μ‚¬μš©ν•΄ μ €μž₯ν–ˆμŠ΅λ‹ˆλ‹€.
  • dfs의 μΈμžλŠ” (μˆœνšŒν•  λ‹€μŒ xμ’Œν‘œ, μˆœνšŒν•  λ‹€μŒ yμ’Œν‘œ, 순회 횟수)둜 λ§Œλ“€μ—ˆμŠ΅λ‹ˆλ‹€.
  • 각 μˆœνšŒλ§ˆλ‹€ bool possible[10]의 boolν˜• 배열을 λ§Œλ“€μ–΄μ„œ κ°€λŠ₯, λΆˆκ°€λŠ₯ν•œ 수λ₯Ό μ²΄ν¬ν–ˆμŠ΅λ‹ˆλ‹€.

#include <iostream>
#include <vector>
using namespace std;
int map[10][10];
vector<pair<int, int>> zeroPoint;
int zeroPointCnt;
bool endSwc = false;
void dfs(int x, int y, int cnt) {
    if (endSwc) return;
    bool possible[10];
    for (int i = 1; i <= 9; i++) possible[i] = true;
    for (int i = 1; i <= 9; i++) {
        if (y == i) continue;
        possible[map[x][i]] = false;
    }
    for (int i = 1; i <= 9; i++) {
        if (x == i) continue;
        possible[map[i][y]] = false;
    }
    int xSt = 0;
    if (x % 3 == 0) xSt = x - 3;
    else xSt = x - (x % 3);
    int ySt = 0;
    if (y % 3 == 0) ySt = y - 3;
    else ySt = y - (y % 3);
    for (int i = xSt + 1; i <= xSt + 3; i++) {
        for (int j = ySt + 1; j <= ySt + 3; j++) {
            if (x == i && y == j) continue;
            possible[map[i][j]] = false;
        }
    }
    for (int i = 1; i <= 9; i++) {
        if (possible[i]) {
            if (cnt == zeroPointCnt) {
                map[x][y] = i;
                for (int i = 1; i <= 9; i++) {
                    for (int j = 1; j <= 9; j++) {
                        cout << map[i][j] << ' ';
                    }
                    cout << '\n';
                }
                endSwc = true;
                return;
            }
            else {
                map[x][y] = i;
                dfs(zeroPoint[cnt].first, zeroPoint[cnt].second, cnt + 1);
                map[x][y] = 0;
            }
        }
    }
}
int main(void) {
    for (int i = 1; i <= 9; i++) {
        for (int j = 1; j <= 9; j++) {
            cin >> map[i][j];
            if (map[i][j] == 0) {
                zeroPoint.push_back({ i,j });
                zeroPointCnt++;
            }
        }
    }
    dfs(zeroPoint[0].first, zeroPoint[0].second, 1);
    return 0;
}

'Algorithm > BOJ' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

[BOJ] ν‰λ²”ν•œ λ°°λ‚­(12865)  (0) 2022.03.12
[BOJ] μ™ΈνŒμ› 순회(2098)  (0) 2022.03.11
[BOJ] 백쑰의 호수(3197)  (0) 2022.03.10
[BOJ] ν›„μœ„ ν‘œκΈ°μ‹(1918)  (0) 2022.03.10
[BOJ] ν–‰μš΄μ˜ 바퀴(2840)  (0) 2022.03.10