Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

42-tgyuuAn #152

Merged
merged 2 commits into from
Mar 22, 2024
Merged

42-tgyuuAn #152

merged 2 commits into from
Mar 22, 2024

Conversation

tgyuuAn
Copy link
Member

@tgyuuAn tgyuuAn commented Mar 7, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

์Šค๋„์ฟ 

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

1์‹œ๊ฐ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

์ด ๋ฌธ์ œ ๊ฐ„๋‹จํ•˜๋‹ค.

๊ทธ๋ƒฅ ์™„ํƒ์œผ๋กœ ํ’€๋ฉด ๋˜๋Š”๋ฐ,

๋ฐฑ ํŠธ๋ž˜ํ‚น์„ ์ด์šฉํ•˜๋ฉด ๋ณด๋‹ค ํšจ์œจ์ ์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ๋‹ค.

image







์•„๋ž˜์™€ ๊ฐ™์€ ๊ทธ๋ฆผ์ด ์žˆ๋‹ค๊ณ  ํ•ด๋ณด์ž.

image







์šฐ๋ฆฌ๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€์˜ ๋‹ต์ด ์žˆ์„ ๊ฒฝ์šฐ ์‚ฌ์ง„์ˆœ์œผ๋กœ ์ •๋ ฌํ–ˆ์„ ๋•Œ ๊ฐ€์žฅ ์•ž์— ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ๋‹ต์œผ๋กœ ์ œ์ถœ ํ•ด์•ผ ํ•œ๋‹ค.

์ฆ‰, ๊ฐ€์žฅ ์™ผ์ชฝ - ์œ„ ์นธ๋ถ€ํ„ฐ ์œ„์ชฝ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์ฐจ๊ณก์ฐจ๊ณก ์ž‘์€ ์ˆ˜ ๋ถ€ํ„ฐ ์ง‘์–ด๋„ฃ์œผ๋ฉด์„œ ๋งˆ์ง€๋ง‰ ์นธ ๊นŒ์ง€ ์ง‘์–ด ๋„ฃ์œผ๋ฉด ๋œ๋‹ค.

๋งจ ์ฒซ ์นธ์— ์ˆซ์ž๋ฅผ ๋„ฃ๋Š”๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž.

image

๊ทธ๋Ÿฌ๋ฉด ํ•ด๋‹น ์นธ์—๋Š” ์ˆซ์ž 1์„ ๋„ฃ์„ ์ˆ˜ ๋ฐ–์— ์—†๋‹ค.

1์„ ๋„ฃ์–ด ์ค€๋‹ค.

image







๊ทธ ๋‹ค์Œ ๋นˆ์นธ์„ ์œ„์—์„œ ์™ผ์ชฝ -> ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ›‘์œผ๋ฉด์„œ ์ฐพ์•„์ค€๋‹ค.

ํ•ด๋‹น ์นธ์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ˆ˜๋Š” 3๋ฐ–์— ์—†์œผ๋ฏ€๋กœ ๋˜ 3์„ ๋„ฃ์–ด์ค€๋‹ค.

image







์•„๋‹ˆ ์ง€๊ธˆ ๋ณด๋‹ˆ๊นŒ ๊ฐ–๊ณ  ์˜จ ์˜ˆ์”จ๊ฐ€ ๋„˜ ์‰ฌ์›Œ์„œ ํ•œ ์นธ์— 2๊ฐ€์ง€ ์ด์ƒ ๊ฐˆ๋ฆผ๊ธธ์ด ์—†๋‹ค.

ํ ...







์Œ... ์ง€๊ธˆ ์™€์„œ ๋‹ค์‹œ ๊ทธ๋ฆฌ๊ธฐ์—” ์ข€ ๋ฒ„๊ฒ๊ณ ..

image

์œ„ ์‚ฌ์ง„์ด ๋ฐฑ ํŠธ๋ž˜ํ‚น์˜ ๋ชจ๋“  ๊ฒƒ์ด๋‹ค!!

๋งŒ์•ฝ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๊ฐˆ๋ฆผ๊ธธ์ด ์žˆ์„ ๊ฒฝ์šฐ ํ•˜๋‚˜ ํ•˜๋‚˜ ๋„ฃ์–ด๋ณด๋ฉด์„œ ๋งŒ์•ฝ ๊ทธ ๊ฒฝ๋กœ๋กœ ๋ชฉ์ ์ง€ ๊นŒ์ง€ ๋„๋‹ฌํ•˜์ง€ ๋ชปํ•˜๋ฉด ๋‹ค์‹œ ๋Œ์•„์™€์„œ ๋‹ค๋ฅธ ๊ฒฝ์šฐ๋ฅผ ๋„ฃ์œผ๋ฉด ๋œ๋‹ค!

์ด ๋•Œ ์šฐ๋ฆฌ๋Š” ์‚ฌ์ „์ˆœ์œผ๋กœ ๊ฐ€์žฅ ์•ž์„  ๊ฒฝ์šฐ๋ฅผ ๋„ฃ์–ด์•ผ ํ•˜๋ฏ€๋กœ ์•„๋ฌด ์ˆซ์ž๋‚˜ ๋„ฃ์œผ๋ฉด ์•ˆ๋˜๊ณ  ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๊ฒฝ์šฐ์˜ ์ˆ˜ ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ์ˆ˜ ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ๋„ฃ์–ด๋ณธ๋‹ค!!!!!!

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์ด ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค๋ฉด ์Šค๋„์ฟ  ๋„ ๊ณต์งœ๋‹ค!

@tgyuuAn tgyuuAn added tgyuuAn ํ•œ ์ค„๋กœ๋Š” ์†Œ๊ฐœํ•  ์ˆ˜ ์—†๋Š” ๋‚จ์ž. ์ž‘์„ฑ ์ค‘ โฑ๏ธ labels Mar 7, 2024
@tgyuuAn tgyuuAn requested review from pknujsp, Munbin-Lee and H0ngJu March 7, 2024 14:06
@tgyuuAn tgyuuAn self-assigned this Mar 7, 2024
@tgyuuAn tgyuuAn marked this pull request as ready for review March 7, 2024 14:23
Copy link
Collaborator

@H0ngJu H0ngJu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"์™„ํƒ์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ์ง€๋งŒ ๋ฐฑํŠธ๋ž˜ํ‚น์œผ๋กœ ๋” ํšจ์œจ์ ์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ๋‹ค."

์ฒจ๋ถ€๋œ ์‚ฌ์ง„์—์„œ์˜ ๋ฐฑํŠธ๋ž˜ํ‚น ์„ค๋ช…์„ ๋ณด๋‹ˆ, ์™„ํƒ์˜ DFS์™€ ๋น„์Šทํ•œ ๊ฒƒ ๊ฐ™์€๋ฐ, ์ฐจ์ด์ ์ด ๋ฌด์—‡์ธ์ง€ ๊ถ๊ธˆํ•ด์ ธ์„œ ์ฐพ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.

๋ฐฑํŠธ๋ž˜ํ‚น๊ณผ DFS์˜ ๊ฐ€์žฅ ํฐ ์ฐจ์ด์ ์€ "๋ถˆํ•„์š”ํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜๊นŒ์ง€ ํƒ์ƒ‰ํ•˜์ง€ ์•Š๋Š”๋‹ค" ์ฆ‰, ํŠธ๋ฆฌ์˜ ๋งจ ์•„๋ž˜๊นŒ์ง€ ํƒ์ƒ‰ํ•  ํ•„์š” ์—†์ด, ํŠน์ • ๊ฐ€์ง€์—์„œ ์กฐ๊ฑด์ด ๋งž์ง€ ์•Š์œผ๋ฉด ๋˜๋Œ์•„ ์˜จ๋‹ค๋Š” ๊ฒƒ์ธ ๊ฒƒ ๊ฐ™๋”๊ตฐ์šฉ

๋Œ€ํ‘œ์ ์ธ ๋ฌธ์ œ๊ฐ€ N-queen์ด๋˜๋ฐ, ์ „์— ํ’€์—ˆ๋˜ N-queen์„ ๋ณด๋‹ˆ ๋‘˜์˜ ์ฐจ์ด๊ฐ€ ์ดํ•ด๊ฐ€ ๋˜๋Š” ๊ฒƒ ๊ฐ™๊ธฐ๋„ ํ•˜๊ณ  ,,, ์ •๋ฆฌํ•˜๋ฉด 'dfs์—์„œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์œผ๋ฉด ๋ฐฑํŠธ๋ž˜ํ‚น์ด๋‹ค!' ์ •๋„๊ฐ€ ๋˜๊ฒ ๋„ค์š”๐Ÿค”๐Ÿค” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์œ ํ˜• ๋˜ ํ•˜๋‚˜ ์–ป๊ณ  ๊ฐ‘๋‹ˆ๋‹ค~!~!! ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค

Copy link
Collaborator

@pknujsp pknujsp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋„ ๋น„์Šทํ•œ ๋กœ์ง์œผ๋กœ ํ’€์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

๋‹ค๋ฅธ ๋ถ€๋ถ„์ด๋ผ๋ฉด 0์ธ ๊ณณ์˜ ์œ„์น˜๋ฅผ ๋‹ด์€ ๋ฆฌ์ŠคํŠธ ๋‘ ๊ฐœ๋ฅผ ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด์„œ dfs๋ฅผ ์ง„ํ–‰ํ•˜๊ฒŒ ํ–ˆ์–ด์š”.

๊ทธ๋Ÿฐ๋ฐ ๋‹ค์‹œ ๋ณด๋‹ˆ ์ด ๋ถ€๋ถ„๋„ ํƒœ๊ทœ๋‹˜ ํ’€์ด์ฒ˜๋Ÿผ ํ•˜๋Š”๊ฒŒ ๋” ๋‚˜์€๊ฑฐ ๊ฐ™๋„ค์š”.

๋‹จ์ˆœํ•˜๊ฒŒ dfsํ•  ๋•Œ 0์ด ์•„๋‹ˆ๋ฉด ๋‹ค์Œ ์œ„์น˜๋กœ ๋„˜์–ด๊ฐ€๋ฉด ๋˜๋‹ˆ๊นŒ
๋ฉ”๋ชจ๋ฆฌ๋„ ๋” ์•„๋‚„ ์ˆ˜ ์žˆ์„๊ฑฐ ๊ฐ™๊ธฐ๋„ ํ•˜๊ณ  ํฌ๊ฒŒ ๋”ฐ๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค ํ•„์š”๊ฐ€ ์—†์–ด๋ณด์ด๋„ค์š”.

๋ฌธ์ œ ์ œ์ถœ์ด ํŒŒ์ด์ฌ3๋กœ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜๊ธธ๋ž˜ ํ˜น์‹œ๋‚˜ ํ•ด์„œ ํŒŒ์ดํŒŒ์ด๋กœ ํ•˜๋‹ˆ ํ†ต๊ณผ๊ฐ€ ๋˜๊ตฐ์š”
ํ˜น์‹œ ํŒŒ์ด์ฌ3๋กœ ์ œ์ถœํ•˜์…จ๋‚˜์š”

board = [list(map(int, stdin.readline().strip())) for _ in range(9)]
empties = []
rowv = [None for _ in range(9)]

for r in range(9):
    rowv[r] = list(set(range(1, 10)) - set(board[r]))
    rowv[r].sort()
    
    for c in range(9):
        if not board[r][c]:
            empties.append((r, c))

rowchecks = [[False] * len(rowv[r]) for r in range(9)]

def is_valid(r, c, v):
    # ์—ด๊ณผ 3x3 ์˜์—ญ ๊ฒ€์‚ฌ๋งŒ ํ•œ๋‹ค
    for i in range(9):
        if board[i][c] == v:
            return False

    nr = r - r % 3
    nc = c - c % 3
    
    for i in range(nr, nr + 3):
        for j in range(nc, nc + 3):
            if board[i][j] == v:
                return False
                
    return True

def solve(i):
    if i == len(empties):
        return True
        
    r, c = empties[i]
    # ํ–‰ ๋ณ„๋กœ ์ถ”๊ฐ€๊ฐ€๋Šฅํ•œ ๊ฐ’๋งŒ ํ™•์ธ
    for j in range(len(rowv[r])):
        if rowchecks[r][j]:
            continue
        v = rowv[r][j]
        
        if is_valid(r, c, v):
            rowchecks[r][j] = True
            board[r][c] = v
            
            if solve(i + 1):
                return True
                
            rowchecks[r][j] = False
            board[r][c] = 0
            
    return False


solve(0)
for row in board:
    print(''.join(map(str, row)))

@tgyuuAn
Copy link
Member Author

tgyuuAn commented Mar 12, 2024

์ €๋„ ๋น„์Šทํ•œ ๋กœ์ง์œผ๋กœ ํ’€์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

๋‹ค๋ฅธ ๋ถ€๋ถ„์ด๋ผ๋ฉด 0์ธ ๊ณณ์˜ ์œ„์น˜๋ฅผ ๋‹ด์€ ๋ฆฌ์ŠคํŠธ ๋‘ ๊ฐœ๋ฅผ ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด์„œ dfs๋ฅผ ์ง„ํ–‰ํ•˜๊ฒŒ ํ–ˆ์–ด์š”.

๊ทธ๋Ÿฐ๋ฐ ๋‹ค์‹œ ๋ณด๋‹ˆ ์ด ๋ถ€๋ถ„๋„ ํƒœ๊ทœ๋‹˜ ํ’€์ด์ฒ˜๋Ÿผ ํ•˜๋Š”๊ฒŒ ๋” ๋‚˜์€๊ฑฐ ๊ฐ™๋„ค์š”.

๋‹จ์ˆœํ•˜๊ฒŒ dfsํ•  ๋•Œ 0์ด ์•„๋‹ˆ๋ฉด ๋‹ค์Œ ์œ„์น˜๋กœ ๋„˜์–ด๊ฐ€๋ฉด ๋˜๋‹ˆ๊นŒ ๋ฉ”๋ชจ๋ฆฌ๋„ ๋” ์•„๋‚„ ์ˆ˜ ์žˆ์„๊ฑฐ ๊ฐ™๊ธฐ๋„ ํ•˜๊ณ  ํฌ๊ฒŒ ๋”ฐ๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค ํ•„์š”๊ฐ€ ์—†์–ด๋ณด์ด๋„ค์š”.

๋ฌธ์ œ ์ œ์ถœ์ด ํŒŒ์ด์ฌ3๋กœ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜๊ธธ๋ž˜ ํ˜น์‹œ๋‚˜ ํ•ด์„œ ํŒŒ์ดํŒŒ์ด๋กœ ํ•˜๋‹ˆ ํ†ต๊ณผ๊ฐ€ ๋˜๊ตฐ์š” ํ˜น์‹œ ํŒŒ์ด์ฌ3๋กœ ์ œ์ถœํ•˜์…จ๋‚˜์š”

board = [list(map(int, stdin.readline().strip())) for _ in range(9)]
empties = []
rowv = [None for _ in range(9)]

for r in range(9):
    rowv[r] = list(set(range(1, 10)) - set(board[r]))
    rowv[r].sort()
    
    for c in range(9):
        if not board[r][c]:
            empties.append((r, c))

rowchecks = [[False] * len(rowv[r]) for r in range(9)]

def is_valid(r, c, v):
    # ์—ด๊ณผ 3x3 ์˜์—ญ ๊ฒ€์‚ฌ๋งŒ ํ•œ๋‹ค
    for i in range(9):
        if board[i][c] == v:
            return False

    nr = r - r % 3
    nc = c - c % 3
    
    for i in range(nr, nr + 3):
        for j in range(nc, nc + 3):
            if board[i][j] == v:
                return False
                
    return True

def solve(i):
    if i == len(empties):
        return True
        
    r, c = empties[i]
    # ํ–‰ ๋ณ„๋กœ ์ถ”๊ฐ€๊ฐ€๋Šฅํ•œ ๊ฐ’๋งŒ ํ™•์ธ
    for j in range(len(rowv[r])):
        if rowchecks[r][j]:
            continue
        v = rowv[r][j]
        
        if is_valid(r, c, v):
            rowchecks[r][j] = True
            board[r][c] = v
            
            if solve(i + 1):
                return True
                
            rowchecks[r][j] = False
            board[r][c] = 0
            
    return False


solve(0)
for row in board:
    print(''.join(map(str, row)))

์ €๋„ ํŒŒ์ด์ฌ์œผ๋กœ ํ•˜๋‹ˆ๊นŒ ์‹œ๊ฐ„ ์ดˆ๊ณผ๋‚˜์„œ pypy๋กœ ์ œ์ถœํ–ˆ๋‹ต๋‹ˆ๋‹ค....

@Munbin-Lee
Copy link
Member

#include <iostream>
#include <array>
#include <functional>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    array<array<int, 9>, 9> board{};
    array<array<bool, 9>, 10> visitedRow{}, visitedCol{}, visitedBox{};

    auto getBox = [](int y, int x) {
        return y / 3 * 3 + x / 3;
    };

    for (int y = 0; y < 9; y++) {
        string s;
        cin >> s;

        for (int x = 0; x < 9; x++) {
            int &cell = board[y][x];
            cell = s[x] - '0';

            if (cell) {
                visitedRow[y][cell] = true;
                visitedCol[x][cell] = true;
                visitedBox[getBox(y, x)][cell] = true;
            }
        }
    }

    function<void(int)> dfs = [&](int index) {
        if (index == 81) {
            for (auto &row: board) {
                for (int item: row) {
                    cout << item;
                }
                cout << '\n';
            }
            exit(0);
        }

        int y = index / 9;
        int x = index % 9;
        int &cell = board[y][x];

        if (cell) {
            dfs(index + 1);
            return;
        }

        for (int i = 1; i <= 9; i++) {
            if (visitedRow[y][i]) continue;
            if (visitedCol[x][i]) continue;
            if (visitedBox[getBox(y, x)][i]) continue;

            visitedRow[y][i] = true;
            visitedCol[x][i] = true;
            visitedBox[getBox(y, x)][i] = true;
            cell = i;

            dfs(index + 1);

            visitedRow[y][i] = false;
            visitedCol[x][i] = false;
            visitedBox[getBox(y, x)][i] = false;
            cell = 0;
        }
    };

    dfs(0);
}

์ €๋Š” ์ˆซ์ž๋ฅผ ๋„ฃ์„ ๋•Œ๋งˆ๋‹ค ํ–‰, ์—ด, ๋ฐ•์Šค ๋ณ„๋กœ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์–ด ๋ฐฑํŠธ๋ž˜ํ‚นํ–ˆ์Šต๋‹ˆ๋‹ค ํ—ˆํ—ˆ

@tgyuuAn tgyuuAn merged commit 0c41f9e into main Mar 22, 2024
@tgyuuAn tgyuuAn deleted the 42-tgyuuAn branch March 22, 2024 04:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tgyuuAn ํ•œ ์ค„๋กœ๋Š” ์†Œ๊ฐœํ•  ์ˆ˜ ์—†๋Š” ๋‚จ์ž. ๋ฆฌ๋ทฐ ์™„๋ฃŒ โœ”๏ธ
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants