Sdoku
Steve Paks
Question

•

•

•

나머지 빈 칸을 채우는 방식은 다음과 같다.
(1) 각각의 가로줄과 세로줄에는 1부터 9까지의 숫자가 한 번씩만 나타나야 한다.
(2) 굵은 선으로 구분되어 있는 3x3 정사각형 안에도 1부터 9까지의 숫자가 한 번씩만 나타나야 한다.
위의 예의 경우, 첫째 줄에는 1을 제외한 나머지 2부터 9까지의 숫자들이 이미 나타나 있으므로 첫째 줄 빈칸에는 1
이 들어가야 한다.

또한 위쪽 가운데 위치한 3x3 정사각형의 경우에는 3을 제외한 나머지 숫자들이 이미 쓰여 있으므로 가운데 빈 칸
에는 3이 들어가야 한다.
Question(Cont’d)
•

입력 예

035469278
782105609
060278135
321046897
804913506
596820413
917652080
603701952
258394760

출력 예

135469278
782135649
469278135
321546897
874913526
596827413
917652384
643781952
258394761
Algorithm
•

Find way to finish the sdoku
Algorithm(Cont’d)
•

find candidates
–

Get number of candidates

For(i){
if(sdoku[row][i] == 0){
noOfCandidates++;
}
}
–

Find candidates

For(i){
for(j){
if( i == sdoku[row][j]) break;
if( j == noOfRows - 1){
addCandidates(i);
}
}

035469278
782105609
060278135
321046897
804913506
596820413
917652080
603701952
258394760
Algorithm(Cont’d)
•

Check duplicate condition
–
–

Value(= 5)
Row(= 3)

–

Column(= 3)

Value
5

035469278
782105609
060278135
321046897
804913506
596820413
917652080
603701952
258394760
Algorithm(Cont’d)
•

Check duplicate condition(Cont’d)
–

3 X 3 matrix

3, 3 3, 4 3, 5
4, 3 4, 4 4, 5
5, 3 5, 4 5, 5

Row = 3;
Col = 3;
tempRowLimit = (Row % 3) + 3;
tempColLimit = (Col % 3) + 3;
Row = 5;
Col = 5;
tempRowLimit = (Row % 3) + 1;
tempColLimit = (Col % 3) + 1;
Row = 2;
Col = 0;
tempRowLimit = (Row % 3) + 1;
tempColLimit = (Col % 3) + 3;

Value
5

035469278
782105609
060278135
321046897
804913506
596820413
917652080
603701952
258394760

If(n % 3 == 0){
n + 3;
}else if{n % 3 == 1){
n + 2;
}else{
n + 1;
}
Algorithm(Cont’d)
•

Remain issue
–
–
–
–
–
–
–

In an order of visit among the candidates makes an issue
As following example we got the candidates 2, 7 at line 5
At first, 2 is put at the col 2.
In this case 2 makes an duplicate condition in the vertical line
So, we try to put 7 at the position.
And 2 will not be considered.
So, we will reconsider during return to the first recursion.

035469278
782105609
060278135
321046897
804913506
596820413
917652080
603701952
258394760
Core methods
• makeSdoku(int)
–
–
–
–

각 행에서 아직 선택되지 않은 후보들을 찾아서
빈 칸에 놓아 보고
중복 조건에 위배되면
조건을 만족할 때 까지 다른 빈 칸에 놓는 동작을 반복

• getCandidates(int)
– 아직 선택되지 않은 후보들을 찾음

• getVacancyCol(int)
– 빈 칸을 찾음

• isDuplicate(int, int, int)
– 중복 조건을 검토

Sdoku