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

feat: Creating a Sudoku Algorithm #4

Merged
merged 2 commits into from
Sep 11, 2022

Conversation

Juhwa-Lee1023
Copy link
Owner

@Juhwa-Lee1023 Juhwa-Lee1023 commented Sep 11, 2022

@LeeSungNo-ian

Outline

Work Contents

  • Creating a Sudoku Calculation File
  • Creating a Sudoku Algorithm

Trouble Point

1. Impossible to check whether the algorithm

  • �Trouble Situation

    • Various problems in Sudoku.
  • Trouble Shooting

    • Algorithm solving solved the Sudoku problem on the site.
// 해당하는 숫자가 들어가도 되는지 검증
func isVerify(_ number: Int, _ sudoku: [[Int]], _ row: Int, _ col:Int) -> Bool {
    let sectorRow: Int = 3 * Int(row / 3)
    let sectorCol: Int = 3 * Int(col / 3)
    let row1 = (row + 2) % 3
    let row2 = (row + 4) % 3
    let col1 = (col + 2) % 3
    let col2 = (col + 4) % 3

    // 들어갈 숫자가 row, column에 있는 숫자와 겹치는지 확인
    for i in 0..<9 {
        if (sudoku[i][col] == number)
        {
            return false
        }
        if (sudoku[row][i] == number)
        {
            return false
        }
    }

    // 숫자가 들어갈 3*3의 공간에 숫자가 겹치는지 확인
    if (sudoku[row1 + sectorRow][col1 + sectorCol] == number)
    {
        return false
    }
    if (sudoku[row2 + sectorRow][col1 + sectorCol] == number)
    {
        return false
    }
    if (sudoku[row1 + sectorRow][col2 + sectorCol] == number)
    {
        return false
    }
    if (sudoku[row2 + sectorRow][col2 + sectorCol] == number)
    {
        return false
    }

    return true
}

func sudokuCalcuation(_ sudoku: inout [[Int]], _ row: Int, _ col: Int) -> Bool {
    if (row == 9) {
        return true
    }

    // 기존에 존재하는 숫자가 있다면
    if (sudoku[row][col] != 0) {
        if (col == 8) {
            if (sudokuCalcuation(&sudoku, row+1, 0) == true) {
                return true
            }
        } else {
            if (sudokuCalcuation(&sudoku, row, col+1) == true) {
                return true
            }
        }
        return false
    }

    // 모든 칸을 채울 때까지 재귀함수 호출
    for num in 1..<10 {
        if (isVerify(num, sudoku, row, col) == true) {
            sudoku[row][col] = num
            if (col == 8) {
                if (sudokuCalcuation(&sudoku, row+1, 0) == true) {
                    return true
                }
            } else {
                if (sudokuCalcuation(&sudoku, row, col+1) == true) {
                    return true
                }
            }
            // 계산이 불가능하면...
            sudoku[row][col] = 0
        }
    }
    
    return false
}

image

@Juhwa-Lee1023 Juhwa-Lee1023 added the enhancement New feature or request label Sep 11, 2022
@Juhwa-Lee1023 Juhwa-Lee1023 linked an issue Sep 11, 2022 that may be closed by this pull request
2 tasks
@Juhwa-Lee1023 Juhwa-Lee1023 merged commit 7611e29 into main Sep 11, 2022
@Juhwa-Lee1023 Juhwa-Lee1023 deleted the 3-feat-creating-a-sudoku-algorithm branch September 11, 2022 11:59
@LeeSungNo-ian
Copy link

코인 고생하셨습니다! 마지막에 백준 알고리즘 문제까지 푼 것 까지.. 크으으...! 👀
고생하셨습니다!

Copy link

@yeniful yeniful left a comment

Choose a reason for hiding this comment

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

스도쿠 비교 방법에 대해서 잘 확인했습니다! DP인건 알았는데(판에 해당되는 2차원 배열을 만들고 유효성을 체크해준다는 의미에서) DP에 백트래킹을 사용했다는건 잘 몰랐어요. 백트래킹에 대해서 좀 더 공부를 해보도록 하겠습니다.

Sudoku/sudokuCalculation.swift Show resolved Hide resolved
Sudoku/sudokuCalculation.swift Show resolved Hide resolved
Sudoku/sudokuCalculation.swift Show resolved Hide resolved
Sudoku/sudokuCalculation.swift Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: Creating a Sudoku Algorithm
3 participants