Advertisement
mixeila

Untitled

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