java - A way to determine in which one of the boxes a number in Sudoku is -
i started implementing sudoku solver , done of algorithm x. means there cover matrix lots of different possible solutions given sudoku , algorithm's task find correct ones.
but problem in generator of matrix. i'll simplify problem as possible. using cover matrix helps me solve sudokus. each line in matrix unique entry. in order compose single line, need determine in cell, row, column , box given number in sudoku is.
since sudoku dimensions defined dimensions of inner boxes (image below), i'm using dimensions determine in row , column pregiven number is.
every box has dimension of width , height or if prefer m , n or other notation - classic 9x9 sudoku has m = 3 , n = 3 (the dimension of inner box).
as can see custom sudoku dimensions m = 2 , n = 3 , has 6 boxes.
i having problem creating formula give me information in box number is. input should position (index) of number in sudoku , output number of box in number is.
below code tried polishing far still doesn't calculate indexes quite correctly. used index of row in given number , tried using determine index of column , tried combining 2 determine number of box. feel free tell me making mistake.
public static void assignsudokubox(int rowindex, int m, int n){ double x = math.ceil(math.ceil((rowindex+1)*1.0/(m*n))/m); double columnindex = (rowindex%(m*n))*m*n + (rowindex/(m*n)); double y = math.ceil(math.ceil(columnindex*1.0/(m*n))/n); double number = math.ceil((x-1)*m+y); system.out.println("box of index " + rowindex + " is: " + number + "."); }
assuming 0-based indices , boxes numbered in same fassion cells, can calculate value follows:
public static void assignsudokubox(int rowindex, int m, int n) { // index, if devided pieces n x 1 int nchunkindex = rowindex / n; // every row has m of pieces , there m rows in each box int row = nchunkindex / (m*m); int column = nchunkindex % m; int result = column + row * m; system.out.println("box of index " + rowindex + " is: " + result + "."); }
output for
for (int = 0; < 36; i++) { assignsudokubox(i, 2, 3); }
box of index 0 is: 0. box of index 1 is: 0. box of index 2 is: 0. box of index 3 is: 1. box of index 4 is: 1. box of index 5 is: 1. box of index 6 is: 0. box of index 7 is: 0. box of index 8 is: 0. box of index 9 is: 1. box of index 10 is: 1. box of index 11 is: 1. box of index 12 is: 2. box of index 13 is: 2. box of index 14 is: 2. box of index 15 is: 3. box of index 16 is: 3. box of index 17 is: 3. box of index 18 is: 2. box of index 19 is: 2. box of index 20 is: 2. box of index 21 is: 3. box of index 22 is: 3. box of index 23 is: 3. box of index 24 is: 4. box of index 25 is: 4. box of index 26 is: 4. box of index 27 is: 5. box of index 28 is: 5. box of index 29 is: 5. box of index 30 is: 4. box of index 31 is: 4. box of index 32 is: 4. box of index 33 is: 5. box of index 34 is: 5. box of index 35 is: 5.
Comments
Post a Comment