Guest User

Untitled

a guest
Apr 27th, 2012
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.95 KB | None | 0 0
  1. Creating 9 subarrays from a 9x9 2d array java
  2. public static void Validate(final int[][] sudokuBoard)
  3. {
  4. int width = sudokuBoard[0].length;
  5. int height = sudokuBoard.length;
  6.  
  7. for(int i = 0; i < width; i++)
  8.  
  9. if(!IsValidRow(sudokuBoard, i, width))
  10. {
  11. System.out.print("(Row)" + i + " Error Detected n");
  12. //Do something - The row has repetitions
  13. }
  14. for(int j = 0; j < height; j++)
  15. if(!IsValidColumn(sudokuBoard, j, height))
  16. {
  17. System.out.print(" (Column)" + j + " Error Detected n");
  18. //Do something - The columns has repetitions
  19. }
  20. // for(int i=0; i<3; i++)
  21. // if(!isBlock1Valid(sudokuBoard,width, height)){
  22. // System.out.print("hi");
  23. //}
  24.  
  25. }
  26.  
  27. static boolean isBlock1Valid(int[][] sudokuBoard, int referenceRow, int referenceColumn)
  28. {
  29. block1
  30. boolean[] seen = new boolean[9];
  31.  
  32. for (int i = 0; i < 3; i++){
  33.  
  34. for (int j = 0; j < 3; j++){
  35.  
  36. if ( seen[sudokuBoard[referenceColumn+i][referenceRow+j]]) return false;
  37.  
  38.  
  39. else ( seen[sudokuBoard[referenceColumn+i][referenceRow+j]]) = true;
  40. }
  41. }
  42. return true;
  43. }
  44.  
  45. import java.util.*;
  46.  
  47. public class TwoDArray
  48. {
  49. public static void main(String... args) throws Exception
  50. {
  51. Scanner scanner = new Scanner(System.in);
  52. int[][] array = {
  53. {0, 1, 2, 3, 4, 5, 6, 7, 8},
  54. {9, 10, 11, 12, 13, 14, 15, 16, 17},
  55. {18, 19, 20, 21, 22, 23, 24, 25, 26},
  56. {27, 28, 29, 30, 31, 32, 33, 34, 35},
  57. {36, 37, 38, 39, 40, 41, 42, 43, 44},
  58. {45, 46, 47, 48, 49, 50, 51, 52, 53},
  59. {54, 55, 56, 57, 58, 59, 60, 61, 62},
  60. {63, 64, 65, 66, 67, 68, 69, 70, 71},
  61. {72, 73, 74, 75, 76, 77, 78, 79, 80}
  62. };
  63.  
  64. displayMatrix(array);
  65. displayEachBlock(array);
  66. }
  67.  
  68. private static void displayMatrix(int[][] array)
  69. {
  70. for (int i = 0; i < array.length; i++)
  71. {
  72. if (i == 3 || i == 6)
  73. System.out.println("------------------------------------");
  74. for (int j = 0; j < array[i].length; j++)
  75. {
  76. System.out.format("%-3s", array[i][j]);
  77. if (j == 2 || j == 5 || j == 8)
  78. System.out.print(" | ");
  79. }
  80. System.out.println();
  81. }
  82. System.out.println("------------------------------------");
  83. }
  84.  
  85. private static void displayEachBlock(int[][] array)
  86. {
  87. for (int i = 0; i < array.length; i += 3)
  88. {
  89. for (int j = 0; j < array[i].length; j += 3)
  90. {
  91. /*
  92. * Here we are finding which block we are standing at.
  93. */
  94. int block = (((i / 3) * 3) + (j / 3));
  95. System.out.println("Block : " + block);
  96. int[][] newArray = new int[3][3];
  97. int newRow = 0;
  98. for (int k = i; k < (i + 3); k++)
  99. {
  100. int newColumn = 0;
  101. for (int l = j; l < (j + 3); l++)
  102. {
  103. // This is where you are getting your array inside the given block.
  104. newArray[newRow][newColumn] = array[k][l];
  105. System.out.format("[%-1s][%-1s] : %-3s ", newRow, newColumn, newArray[newRow][newColumn++]);
  106. }
  107. newRow++;
  108. System.out.println();
  109. }
  110. // Here you can send your newArray for VALIDATION, thingy.
  111. // So that we can move on to the next Block for further processing.
  112. }
  113. }
  114. }
  115. }
  116.  
  117. 0  1  2   | 3  4  5   | 6  7  8   |
  118. 9  10 11  | 12 13 14  | 15 16 17  |
  119. 18 19 20  | 21 22 23  | 24 25 26  |
  120. ------------------------------------
  121. 27 28 29  | 30 31 32  | 33 34 35  |
  122. 36 37 38  | 39 40 41  | 42 43 44  |
  123. 45 46 47  | 48 49 50  | 51 52 53  |
  124. ------------------------------------
  125. 54 55 56  | 57 58 59  | 60 61 62  |
  126. 63 64 65  | 66 67 68  | 69 70 71  |
  127. 72 73 74  | 75 76 77  | 78 79 80  |
  128. ------------------------------------
  129. Block : 0
  130. [0][0] : 0   [0][1] : 1   [0][2] : 2
  131. [1][0] : 9   [1][1] : 10  [1][2] : 11
  132. [2][0] : 18  [2][1] : 19  [2][2] : 20
  133. Block : 1
  134. [0][0] : 3   [0][1] : 4   [0][2] : 5
  135. [1][0] : 12  [1][1] : 13  [1][2] : 14
  136. [2][0] : 21  [2][1] : 22  [2][2] : 23
  137. Block : 2
  138. [0][0] : 6   [0][1] : 7   [0][2] : 8
  139. [1][0] : 15  [1][1] : 16  [1][2] : 17
  140. [2][0] : 24  [2][1] : 25  [2][2] : 26
  141. Block : 3
  142. [0][0] : 27  [0][1] : 28  [0][2] : 29
  143. [1][0] : 36  [1][1] : 37  [1][2] : 38
  144. [2][0] : 45  [2][1] : 46  [2][2] : 47
  145. Block : 4
  146. [0][0] : 30  [0][1] : 31  [0][2] : 32
  147. [1][0] : 39  [1][1] : 40  [1][2] : 41
  148. [2][0] : 48  [2][1] : 49  [2][2] : 50
  149. Block : 5
  150. [0][0] : 33  [0][1] : 34  [0][2] : 35
  151. [1][0] : 42  [1][1] : 43  [1][2] : 44
  152. [2][0] : 51  [2][1] : 52  [2][2] : 53
  153. Block : 6
  154. [0][0] : 54  [0][1] : 55  [0][2] : 56
  155. [1][0] : 63  [1][1] : 64  [1][2] : 65
  156. [2][0] : 72  [2][1] : 73  [2][2] : 74
  157. Block : 7
  158. [0][0] : 57  [0][1] : 58  [0][2] : 59
  159. [1][0] : 66  [1][1] : 67  [1][2] : 68
  160. [2][0] : 75  [2][1] : 76  [2][2] : 77
  161. Block : 8
  162. [0][0] : 60  [0][1] : 61  [0][2] : 62
  163. [1][0] : 69  [1][1] : 70  [1][2] : 71
  164. [2][0] : 78  [2][1] : 79  [2][2] : 80
  165.  
  166. import java.util.ArrayList;
  167.  
  168. public class Main
  169. {
  170. public static void main(String[] args)
  171. {
  172. int[][] input = {{1,1,1,1,1,1,1,1,1},
  173. {2,2,2,2,2,2,2,2,2},
  174. {3,3,3,3,3,3,3,3,3},
  175. {4,4,4,4,4,4,4,4,4},
  176. {1,2,3,4,5,6,7,8,9},
  177. {6,6,6,6,6,6,6,6,6},
  178. {7,7,7,7,7,7,7,7,7},
  179. {8,8,8,8,8,8,8,8,8},
  180. {9,9,9,9,9,9,9,9,9}};
  181.  
  182. int[][][] output = get3DVersion(input);
  183.  
  184. for(int i=1; i<=output.length; i++)
  185. System.out.println("Validity of subArray #"+i+" : " +check(output[i-1]));
  186. }
  187.  
  188. public static boolean check(int[][] array)
  189. {
  190. ArrayList<Integer> soFar = new ArrayList<Integer>();
  191.  
  192. for(int j=0; j<3; j++)
  193. for(int k=0; k<3; k++)
  194. {
  195. if(soFar.contains(array[j][k]))
  196. return false;
  197. else
  198. soFar.add(array[j][k]);
  199. }
  200. return true;
  201. }
  202.  
  203. public static int[][][] get3DVersion(int[][] input)
  204. {
  205. int[][][] output = new int[9][3][3];
  206.  
  207. for(int i=0; i<9; i++)
  208. for(int j=0; j<3; j++)
  209. for(int k=0; k<3; k++)
  210. output[i][j][k] = input[i][j*3+k];
  211.  
  212. output[8][2][2] = input[8][8];
  213.  
  214. return output;
  215. }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment