Using System.Collections.Generic;
Using Unityeditor;
Using Unityengine;
public class Testsudo
{
Class Sudokuitem
{
public bool Isnormal = FALSE; Fixed numbers do not need to detect expectations
public int value; The value that the item is stored in
Public list<int> expectvaluelist; Whether the item is available 1-9 1: Available 0: Not available
}
Static list<list<sudokuitem>> sudolist;
[MenuItem ("Testsudo/action")]
public static void Sudoaction ()
{
Int[][] target = {
New int[]{8,0,0,0,0,0,0,0,0},
New int[]{0,0,3,6,0,0,0,0,0},
New int[]{0,7,0,0,9,0,2,0,0},
New int[]{0,5,0,0,0,7,0,0,0},
New int[]{0,0,0,0,4,5,7,0,0},
New int[]{0,0,7,1,0,0,0,3,0},
New int[]{0,0,1,0,0,0,0,6,8},
New int[]{0,0,8,5,0,0,0,1,0},
New int[]{0,9,0,0,0,0,4,0,0}
};
Constructionsudoku (target);
Begincalculate (0);
String log = "";
for (int r = 0; r < 9; r++)
{
for (int c = 0; c < 9; C + +)
{
Log + = Sudolist[r][c].value + "";
}
Log + = "\ n";
}
Debug.Log (LOG);
}
Building a Sudoku List
public static void Constructionsudoku (int[][] target)
{
Sudolist = new list<list<sudokuitem>> ();
for (int r = 0; r < 9; r++)
{
list<sudokuitem> rList = new list<sudokuitem> ();
for (int c = 0; c < 9; C + +)
{
Sudokuitem newitem = new Sudokuitem ();
Newitem.value = 0;
Newitem.expectvaluelist = new list<int> (new int[9] {1, 1, 1, 1, 1, 1, 1, 1, 1});
Rlist.add (NewItem);
}
Sudolist.add (rList);
}
for (int r = 0; r < 9; r++)
{
for (int c = 0; c < 9; C + +)
{
if (Target[r][c] > 0)
{
Sudolist[r][c].isnormal = true;
Sudolist[r][c].value = Target[r][c];
Sudolist[r][c].expectvaluelist = new list<int> (new Int[9] {0, 0, 0, 0, 0, 0, 0, 0, 0});
Removeexpectvalue (R, C, Target[r][c]);
}
}
}
}
Remove the number of rows due to the fixed number of the lattice corresponding to the other lattice of the expectation of the figure
public static void Removeexpectvalue (int rindex, int cindex, int value)
{
for (int r = 0; r < 9; r++)
{
for (int c = 0; c < 9; C + +)
{
if (r = = Rindex | | c = CINDEX | | (R/3 = = Rindex/3 && C/3 = = CINDEX/3))
{
Sudolist[r][c].expectvaluelist[value-1] = 0;
}
}
}
}
Start recursive detection
public static bool Begincalculate (int index)
{
if (index >= Bayi) return true;
int r = INDEX/9;
int c = index% 9;
if (sudolist[r][c].isnormal)//is a fixed number that jumps directly to the next
{
Return Begincalculate (index + 1);
}
for (int v = 1; v <= 9; v++)
{
if (sudolist[r][c].expectvaluelist[v-1] = = 0) continue; The number is not available to skip
First determine the number is not available, you can first fill in the grid, and then go to the next lattice
if (Checknumbervalid (R, C, v))
{
Sudolist[r][c].value = v;
if (begincalculate (index + 1)) return true;
}
}
Reset to 0 if all numbers are not available and return to the previous layer
Sudolist[r][c].value = 0;
return false;
}
Detects if a cell is available with a number
public static bool Checknumbervalid (int rindex, int cindex, int value)
{
Yes
for (int c = 0; c < 9; C + +)
{
if (c! = CIndex && Sudolist[rindex][c].value = = value)
{
return false;
}
}
Column
for (int r = 0; r < 9; r++)
{
if (r! = Rindex && Sudolist[r][cindex].value = = value)
{
return false;
}
}
Palace
for (int g = RINDEX/3 * 3; g < RINDEX/3 * 3 + 3; g++)
{
for (int i = CINDEX/3 * 3; i < CINDEX/3 * 3 + 3; i++)
{
if (g! = Rindex && i! = CIndex && Sudolist[g][i].value = = value)
{
return false;
}
}
}
return true;
}
}
Sudoku Brute Force Code by Unity