1. S0 구하기
var sudoku: [[[Int]]] = []
var S : [[Int]] = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
for _ in 0...9{ // 무작위로 수를 생성하여 10번 자리를 바꿔주었다.
let randNum1 = Int(arc4random_uniform(3))
let randNum2 = Int(arc4random_uniform(3))
let randNum3 = Int(arc4random_uniform(3))
let randNum4 = Int(arc4random_uniform(3))
let temp = S[randNum1][randNum2]
S[randNum1][randNum2] = S[randNum3][randNum4]
S[randNum3][randNum4] = temp
}
let X1 = [[0,0,1],
[1,0,0],
[0,1,0]]
// S1 = X1 * S
var S1: [[Int]] = []
for k in 0...2{
var arr: [Int] = []
for i in 0...2{
var sum = 0
for j in 0...2{
sum += X1[k][j] * S[j][i]
}
arr.append(sum)
}
S1.append(arr)
}
let X2 = [[0,1,0],
[0,0,1],
[1,0,0]]
// S2 = X2 * S
var S2: [[Int]] = []
for k in 0...2{
var arr: [Int] = []
for i in 0...2{
var sum = 0
for j in 0...2{
sum += X2[k][j] * S[j][i]
}
arr.append(sum)
}
S2.append(arr)
}
sudoku.append(S2)
// S3 = S * X1
var S3: [[Int]] = []
for k in 0...2{
var arr: [Int] = []
for i in 0...2{
var sum = 0
for j in 0...2{
sum += S[k][j] * X1[j][i]
}
arr.append(sum)
}
S3.append(arr)
}
sudoku.append(S3)
// S4 = X1 * S * X1
var S4: [[Int]] = []
for k in 0...2{
var arr: [Int] = []
for i in 0...2{
var sum = 0
for j in 0...2{
sum += S1[k][j] * X1[j][i]
}
arr.append(sum)
}
S4.append(arr)
}
sudoku.append(S4)
// S5 = X2 * S * X1
var S5: [[Int]] = []
for k in 0...2{
var arr: [Int] = []
for i in 0...2{
var sum = 0
for j in 0...2{
sum += S2[k][j] * X1[j][i]
}
arr.append(sum)
}
S5.append(arr)
}
sudoku.append(S5)
// S6 = S * X2
var S6: [[Int]] = []
for k in 0...2{
var arr: [Int] = []
for i in 0...2{
var sum = 0
for j in 0...2{
sum += S[k][j] * X2[j][i]
}
arr.append(sum)
}
S6.append(arr)
}
sudoku.append(S6)
// S7 = X1 * S * X2
var S7: [[Int]] = []
for k in 0...2{
var arr: [Int] = []
for i in 0...2{
var sum = 0
for j in 0...2{
sum += S1[k][j] * X2[j][i]
}
arr.append(sum)
}
S7.append(arr)
}
sudoku.append(S7)
// S8 = X2 * S * X2
var S8: [[Int]] = []
for k in 0...2{
var arr: [Int] = []
for i in 0...2{
var sum = 0
for j in 0...2{
sum += S2[k][j] * X2[j][i]
}
arr.append(sum)
}
S8.append(arr)
}
sudoku.append(S8)
// 가로줄 교환
var horizonNum = 0
for _ in 0...2{
let randNum1 = Int(arc4random_uniform(3)) + horizonNum
let randNum2 = Int(arc4random_uniform(3)) + horizonNum
if randNum1 != randNum2 {
let temp = sudoku2Arr[randNum1]
sudoku2Arr[randNum1] = sudoku2Arr[randNum2]
sudoku2Arr[randNum2] = temp
}
horizonNum += 3
}
// 세로줄 교환
var verticalNum = 0
for _ in 0...2{
let randNum1 = Int(arc4random_uniform(3)) + verticalNum
let randNum2 = Int(arc4random_uniform(3)) + verticalNum
if randNum1 != randNum2{
for i in 0...8{
let temp = sudoku2Arr[i][randNum1]
sudoku2Arr[i][randNum1] = sudoku2Arr[i][randNum2]
sudoku2Arr[i][randNum2] = temp
}
}
verticalNum += 3
}
// 가로 영역
let randNum1 = Int(arc4random_uniform(3)) * 3
let randNum2 = Int(arc4random_uniform(3)) * 3
if randNum1 != randNum2{
for i in 0...2{
let temp = sudoku2Arr[randNum1 + i]
sudoku2Arr[randNum1 + i] = sudoku2Arr[randNum2 + i]
sudoku2Arr[randNum2 + i] = temp
}
}
// 세로 영역
let randNum3 = Int(arc4random_uniform(3)) * 3
let randNum4 = Int(arc4random_uniform(3)) * 3
if randNum3 != randNum4{
for i in 0...2{
for j in 0...8{
let temp = sudoku2Arr[j][i + randNum3]
sudoku2Arr[j][i + randNum3] = sudoku2Arr[j][i + randNum4]
sudoku2Arr[j][i + randNum4] = temp
}
}
}
for _ in 1...10{ // 무작위로 10번 교환
let randNum1 = Int(arc4random_uniform(9)) + 1
let randNum2 = Int(arc4random_uniform(9)) + 1
if randNum1 != randNum2{
var xyArr: [[Int]] = []
for i in 0...8{
var cnt = 0
for j in 0...8{
if sudoku2Arr[i][j] == randNum1{
xyArr.append([i,j,randNum2])
cnt += 1
} else if sudoku2Arr[i][j] == randNum2{
xyArr.append([i,j,randNum1])
cnt += 1
}
if cnt == 2{
break
}
}
}
for i in xyArr{
sudoku2Arr[i[0]][i[1]] = i[2]
}
}
}