Advertisement
mixeila

Untitled

Apr 18th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.38 KB | None | 0 0
  1. package assign3;
  2.  
  3. import java.util.*;
  4.  
  5. /*
  6. * Encapsulates a Sudoku grid to be solved.
  7. * CS108 Stanford.
  8. */
  9. public class Sudoku {
  10. // Provided grid data for main/testing
  11. // The instance variable strategy is up to you.
  12.  
  13. // Provided easy 1 6 grid
  14. // (can paste this text into the GUI too)
  15. public static final int[][] easyGrid = Sudoku.stringsToGrid(
  16. "1 6 4 0 0 0 0 0 2",
  17. "2 0 0 4 0 3 9 1 0",
  18. "0 0 5 0 8 0 4 0 7",
  19. "0 9 0 0 0 6 5 0 0",
  20. "5 0 0 1 0 2 0 0 8",
  21. "0 0 8 9 0 0 0 3 0",
  22. "8 0 9 0 4 0 2 0 0",
  23. "0 7 3 5 0 9 0 0 1",
  24. "4 0 0 0 0 0 6 7 9");
  25.  
  26.  
  27. // Provided medium 5 3 grid
  28. public static final int[][] mediumGrid = Sudoku.stringsToGrid(
  29. "530070000",
  30. "600195000",
  31. "098000060",
  32. "800060003",
  33. "400803001",
  34. "700020006",
  35. "060000280",
  36. "000419005",
  37. "000080079");
  38.  
  39. // Provided hard 3 7 grid
  40. // 1 solution this way, 6 solutions if the 7 is changed to 0
  41. public static final int[][] hardGrid = Sudoku.stringsToGrid(
  42. "3 7 0 0 0 0 0 8 0",
  43. "0 0 1 0 9 3 0 0 0",
  44. "0 4 0 7 8 0 0 0 3",
  45. "0 9 3 8 0 0 0 1 2",
  46. "0 0 0 0 4 0 0 0 0",
  47. "5 2 0 0 0 6 7 9 0",
  48. "6 0 0 0 2 1 0 4 0",
  49. "0 0 0 5 3 0 9 0 0",
  50. "0 3 0 0 0 0 0 5 1");
  51.  
  52.  
  53. public static final int SIZE = 9; // size of the whole 9x9 puzzle
  54. public static final int PART = 3; // size of each 3x3 part
  55. public static final int MAX_SOLUTIONS = 100;
  56.  
  57. // Provided various static utility methods to
  58. // convert data formats to int[][] grid.
  59.  
  60. /**
  61. * Returns a 2-d grid parsed from strings, one string per row.
  62. * The "..." is a Java 5 feature that essentially
  63. * makes "rows" a String[] array.
  64. * (provided utility)
  65. * @param rows array of row strings
  66. * @return grid
  67. */
  68. public static int[][] stringsToGrid(String... rows) {
  69. int[][] result = new int[rows.length][];
  70. for (int row = 0; row<rows.length; row++) {
  71. result[row] = stringToInts(rows[row]);
  72. }
  73. return result;
  74. }
  75.  
  76.  
  77. /**
  78. * Given a single string containing 81 numbers, returns a 9x9 grid.
  79. * Skips all the non-numbers in the text.
  80. * (provided utility)
  81. * @param text string of 81 numbers
  82. * @return grid
  83. */
  84. public static int[][] textToGrid(String text) {
  85. int[] nums = stringToInts(text);
  86. if (nums.length != SIZE*SIZE) {
  87. throw new RuntimeException("Needed 81 numbers, but got:" + nums.length);
  88. }
  89.  
  90. int[][] result = new int[SIZE][SIZE];
  91. int count = 0;
  92. for (int row = 0; row<SIZE; row++) {
  93. for (int col=0; col<SIZE; col++) {
  94. result[row][col] = nums[count];
  95. count++;
  96. }
  97. }
  98. return result;
  99. }
  100.  
  101.  
  102. /**
  103. * Given a string containing digits, like "1 23 4",
  104. * returns an int[] of those digits {1 2 3 4}.
  105. * (provided utility)
  106. * @param string string containing ints
  107. * @return array of ints
  108. */
  109. public static int[] stringToInts(String string) {
  110. int[] a = new int[string.length()];
  111. int found = 0;
  112. for (int i=0; i<string.length(); i++) {
  113. if (Character.isDigit(string.charAt(i))) {
  114. a[found] = Integer.parseInt(string.substring(i, i+1));
  115. found++;
  116. }
  117. }
  118. int[] result = new int[found];
  119. System.arraycopy(a, 0, result, 0, found);
  120. return result;
  121. }
  122.  
  123.  
  124. // Provided -- the deliverable main().
  125. // You can edit to do easier cases, but turn in
  126. // solving hardGrid.
  127. public static void main(String[] args) {
  128. Sudoku sudoku;
  129. sudoku = new Sudoku(hardGrid);
  130.  
  131. // System.out.println(sudoku); // print the raw problem
  132. int count = sudoku.solve(0);
  133. System.out.println("da pasuxiaaa: "+count);
  134. // System.out.println("solutions:" + count);
  135. // System.out.println("elapsed:" + sudoku.getElapsed() + "ms");
  136. // System.out.println(sudoku.getSolutionText());
  137. }
  138.  
  139. private Spot[][] base;
  140. private ArrayList<Spot> spotList;
  141. private int[][] resultGrid;
  142.  
  143. /**
  144. * Sets up based on the given ints.
  145. */
  146. public Sudoku(int[][] ints) {
  147. base = new Spot[SIZE][SIZE];
  148. spotList = new ArrayList<Spot>();
  149. for(int i=0; i<SIZE; i++) {
  150. for(int j=0; j<SIZE; j++) {
  151. base[i][j] = new Spot(i, j, ints[i][j]);
  152. if(base[i][j].value==0)
  153. spotList.add(base[i][j]);
  154. }
  155. }
  156. for(int i=0; i<SIZE; i++) {
  157. for(int j=0; j<SIZE; j++) {
  158. int currVar = getValidList(i, j).size();
  159. base[i][j].setVariance(currVar);
  160. }
  161. }
  162. Collections.sort(spotList, new sortByVariance());
  163. }
  164.  
  165. class sortByVariance implements Comparator<Spot> {
  166. public int compare(Spot first, Spot second) {
  167. return first.compareByVariance(second);
  168. }
  169. }
  170.  
  171. /**
  172. * Solves the puzzle, invoking the underlying recursive search.
  173. */
  174. public int solve(int index) {
  175. if(index==spotList.size()) {
  176. if(resultGrid == null) {
  177. resultGrid = new int[SIZE][SIZE];
  178. for(int i=0; i<SIZE; i++) {
  179. for(int j=0; j<SIZE; j++) {
  180. resultGrid[i][j] = base[i][j].getValue();
  181. }
  182. }
  183. }
  184. return 1;
  185. }
  186. Spot curr = spotList.get(index);
  187. ArrayList<Integer> variances = getValidList(curr.getRow(), curr.getColumn());
  188. int result = 0;
  189. for(int i=0; i<variances.size(); i++) {
  190. curr.setValue(variances.get(i));
  191. result += solve(index+1);
  192. }
  193. curr.setValue(0);
  194. return result;
  195. }
  196.  
  197. public String getSolutionText() {
  198. return ""; // YOUR CODE HERE
  199. }
  200.  
  201. public long getElapsed() {
  202. return 0; // YOUR CODE HERE
  203. }
  204.  
  205. private class Spot {
  206. private int value;
  207. private int column;
  208. private int row;
  209. private int variance;
  210.  
  211. public Spot(int row, int column, int value) {
  212. this.value = value;
  213. this.column = column;
  214. this.row = row;
  215. }
  216.  
  217. public void setValue(int value) {
  218. this.value = value;
  219. }
  220.  
  221. public int getValue() {
  222. return value;
  223. }
  224.  
  225. public int getRow() {
  226. return row;
  227. }
  228.  
  229. public int getColumn() {
  230. return column;
  231. }
  232.  
  233. public void setVariance(int variance) {
  234. this.variance = variance;
  235. }
  236.  
  237. public int getVariance() {
  238. return variance;
  239. }
  240.  
  241. public int compareByVariance(Spot second) {
  242. return second.variance-this.variance;
  243. }
  244. }
  245. public ArrayList<Integer> getValidList(int row, int column){
  246. ArrayList<Boolean> check = new ArrayList<Boolean>();
  247. for(int i=0; i<SIZE; i++) {
  248. check.add(false);
  249. }
  250. checkSquare(check, row, column);
  251. checkColumn(check, row, column);
  252. checkRow(check, row, column);
  253. ArrayList<Integer> result = new ArrayList<Integer>();
  254. for(int i=0; i<check.size(); i++) {
  255. if(!check.get(i)) result.add(i+1);
  256. }
  257. return result;
  258. }
  259. private void checkSquare(ArrayList<Boolean> arr, int row, int column) {
  260. for(int i=0; i<PART; i++) {
  261. int currRow = row-row%PART+i;
  262. for(int j=0; j<PART; j++) {
  263. int currCol = column-column%PART+j;
  264. int currValue = base[currRow][currCol].value;
  265. if(currValue!=0) arr.set(currValue-1, true);
  266. }
  267. }
  268. }
  269. private void checkRow(ArrayList<Boolean> arr, int row, int column) {
  270. int bound = row-row%PART;
  271. for(int i=0; i<bound; i++) {
  272. int currValue = base[i][column].value;
  273. if(currValue!=0)
  274. arr.set(currValue-1, true);
  275. }
  276. for(int i=bound+PART; i<SIZE; i++) {
  277. int currValue = base[i][column].value;
  278. if(currValue!=0)
  279. arr.set(currValue-1, true);
  280. }
  281. }
  282. private void checkColumn(ArrayList<Boolean> arr, int row, int column) {
  283. int bound = column-column%PART;
  284. for(int i=0; i<bound; i++) {
  285. int currValue = base[row][i].value;
  286. if(currValue!=0)
  287. arr.set(currValue-1, true);
  288. }
  289. for(int i=bound+PART; i<SIZE; i++) {
  290. int currValue = base[row][i].value;
  291. if(currValue!=0)
  292. arr.set(currValue-1, true);
  293. }
  294. }
  295.  
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement