Advertisement
Guest User

Untitled

a guest
Dec 30th, 2011
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.48 KB | None | 0 0
  1. import java.util.Stack;
  2. /*************************************************
  3. This is under the NFPL liciense (made up liciense :))
  4. States that you can use this under any liciense or no liciense at all
  5. Doesn't matter
  6.  
  7. Richard Stallman had a truely great idea with the opensource movement in the 80's
  8. This liciense is supposed to be the equivalent to that movement...
  9. Meaning this is an opensource liciense define it anyway you want :)
  10.  
  11. Written By BlackBox (my other alias sometime used is motherbrain)
  12. Have fun
  13.  
  14. compile with javac SudokuSolver.java
  15. run with java SudokuSolver
  16.  
  17. Note this program uses a backtracking algorithm that is depth first search based
  18. If you are putting it into graph theory
  19. You can use depth for search for checking connectivity it is pretty easy just watch the stack :)
  20.  
  21. **************************************************/
  22.  
  23. public class SudokuSolver {
  24.  
  25. class SudokuState {
  26.  
  27. public SudokuState( int cellnum , int board[][] )
  28. {
  29. for( int i = 0 ; i < 9 ; i++ )
  30. {
  31. this.possiblity[i] = true ;
  32. }
  33. this.cellnum = cellnum ;
  34. this.board = board ;
  35. }
  36.  
  37. public int[] getPossiblity() {
  38.  
  39. int num = numberofchoices();
  40. if( num == 0 )
  41. return null ;
  42. int pchoices[] = new int[num] ;
  43. int j = 0 ;
  44. for(int i = 0 ; i < 9 ; i++)
  45. {
  46. if(possiblity[i] == true)
  47. {
  48. pchoices[j] = i+1 ;
  49. j++ ;
  50. }
  51. }
  52.  
  53. return pchoices ;
  54. }
  55.  
  56.  
  57. public int numberofchoices()
  58. {
  59. int j = 0 ;
  60. for( int i = 0 ; i < 9 ; i++ )
  61. {
  62. if( possiblity[i] == true )
  63. {
  64. j++ ;
  65. }
  66. }
  67. return j ;
  68. }
  69.  
  70. public void setPossiblity(boolean choicestate , int choice) {
  71. this.possiblity[choice-1] = choicestate ;
  72. }
  73.  
  74. public int getcellnum()
  75. {
  76. return this.cellnum ;
  77. }
  78.  
  79. public void setBoardCell( int val , int cellnum )
  80. {
  81. int row = (cellnum-1)/9 ;
  82. //if( row == -1 )
  83. // row = 0 ;
  84. int col = cellnum%9 - 1;
  85. if( col == -1 )
  86. this.board[row][8] = val ;
  87. else
  88. this.board[row][col] = val ;
  89. }
  90.  
  91. public int getBoardCell( int cellnum )
  92. {
  93. int row = (cellnum-1)/9 ;
  94. //if( row == -1 )
  95. // row = 0 ;
  96. int col = (cellnum%9) - 1 ;
  97. if( col == -1 )
  98. col = 8 ;
  99. return this.board[row][col] ;
  100. }
  101.  
  102. public int[][] getBoard()
  103. {
  104. return this.board ;
  105. }
  106.  
  107. private boolean possiblity[] = new boolean[9] ;
  108. private int board[][] = null ;
  109. private int cellnum = -1 ;
  110.  
  111. }
  112.  
  113. public void calculatePossiblities( SudokuState state , int board[][] )
  114. {
  115. int cellnum = state.getcellnum() ;
  116. int row = (cellnum - 1) / board.length ;
  117. // if( row == -1)
  118. // row = 0 ;
  119. int col = ( cellnum % board.length ) - 1 ;
  120. if( col == -1 )
  121. col = 8 ;
  122.  
  123. rowPossiblities( state , board , row ) ;
  124. colPossiblities( state , board , col ) ;
  125. subPossiblities( state , board , row , col ) ;
  126.  
  127. }
  128.  
  129. public void rowPossiblities( SudokuState state , int board[][] , int row )
  130. {
  131. for( int i = 0 ; i < board.length ; i++ )
  132. {
  133. if( board[row][i] != -1 )
  134. state.setPossiblity(false, board[row][i] ) ;
  135. }
  136.  
  137. }
  138.  
  139. public void colPossiblities( SudokuState state , int board[][] , int col )
  140. {
  141. for( int i = 0 ; i < board.length ; i++ )
  142. {
  143. if( board[i][col] != -1 )
  144. state.setPossiblity(false, board[i][col] ) ;
  145. }
  146.  
  147. }
  148.  
  149. public void subPossiblities( SudokuState state , int board[][] , int row , int col )
  150. {
  151. if( row < 3 && col < 3 )
  152. {
  153. for( int i = 0 ; i < 3 ; i++ )
  154. for( int j = 0 ; j < 3 ; j++ )
  155. if( board[i][j] != -1 )
  156. state.setPossiblity(false , board[i][j]) ;
  157. return ;
  158.  
  159. }
  160.  
  161. if( (row > 2 && row < 6) && col < 3 )
  162. {
  163. for( int i = 3 ; i < 6 ; i++ )
  164. for( int j = 0 ; j < 3 ; j++ )
  165. if( board[i][j] != -1 )
  166. state.setPossiblity(false , board[i][j]) ;
  167. return ;
  168. }
  169.  
  170. if( row > 5 && col < 3 )
  171. {
  172. for( int i = 6 ; i < 9 ; i++ )
  173. for( int j = 0 ; j < 3 ; j++ )
  174. if( board[i][j] != -1 )
  175. state.setPossiblity(false , board[i][j]) ;
  176. return ;
  177.  
  178. }
  179.  
  180. if( (col > 2 && col < 6) && row < 3 )
  181. {
  182. for( int i = 0 ; i < 3 ; i++ )
  183. for( int j = 3 ; j < 6 ; j++ )
  184. if( board[i][j] != -1 )
  185. state.setPossiblity(false , board[i][j]) ;
  186. return ;
  187. }
  188.  
  189. if( col > 5 && (row > 2 && row < 6) )
  190. {
  191. for( int i = 3 ; i < 6 ; i++ )
  192. for( int j = 6 ; j < 9 ; j++ )
  193. if( board[i][j] != -1 )
  194. state.setPossiblity(false , board[i][j]) ;
  195. return ;
  196. }
  197.  
  198. if( col > 5 && row > 5 )
  199. {
  200. for( int i = 6 ; i < 9 ; i++ )
  201. for( int j = 6 ; j < 9 ; j++ )
  202. if( board[i][j] != -1 )
  203. state.setPossiblity(false , board[i][j]) ;
  204.  
  205. return ;
  206. }
  207.  
  208. if( (col > 2 && col < 6) && (row > 2 && row < 6) )
  209. {
  210. for( int i = 3 ; i < 6 ; i++ )
  211. for( int j = 3 ; j < 6 ; j++ )
  212. if( board[i][j] != -1 )
  213. state.setPossiblity(false , board[i][j]) ;
  214. return ;
  215. }
  216.  
  217. if( (col > 2 && col < 6) && row > 5 )
  218. {
  219. for( int i = 6 ; i < 9 ; i++ )
  220. for( int j = 3 ; j < 6 ; j++ )
  221. if( board[i][j] != -1 )
  222. state.setPossiblity(false , board[i][j]) ;
  223. return ;
  224. }
  225.  
  226. if( col > 5 && row < 3 )
  227. {
  228. for( int i = 0 ; i < 3 ; i++ )
  229. for( int j = 6 ; j < 9 ; j++ )
  230. if( board[i][j] != -1 )
  231. state.setPossiblity(false , board[i][j]) ;
  232.  
  233. return ;
  234. }
  235.  
  236.  
  237.  
  238. }
  239.  
  240. public int nextemptycell(int board[][])
  241. {
  242. for( int i = 0 ; i < board.length ; i++ )
  243. for( int j = 0 ; j < board.length ; j++ )
  244. if(board[i][j] == -1)
  245. return 9*i + j + 1 ;
  246. return -1;
  247. }
  248.  
  249. public int[][] solveSudoku( int board[][] )
  250. {
  251. Stack<SudokuState> statestack = new Stack<SudokuState>() ;
  252. SudokuState svar = new SudokuState(nextemptycell(board), board) ;
  253. while( true )
  254. {
  255. calculatePossiblities(svar , board) ;
  256. if( svar.numberofchoices() == 0 )
  257. {
  258. svar = statestack.pop();
  259. svar.setPossiblity(false , svar.getBoardCell(svar.getcellnum()) ) ;
  260. svar.setBoardCell( -1 , svar.getcellnum()) ;
  261. board = svar.getBoard() ;
  262. }
  263. else
  264. {
  265. svar.setBoardCell(svar.getPossiblity()[0] , svar.getcellnum()) ;
  266. // svar.setPossiblity(false, svar.getPossiblity()[0] ) ;
  267. statestack.push(svar) ;
  268. board = svar.getBoard() ;
  269. // printboard(board) ;
  270. //System.out.println("empty cell = " + nextemptycell(board)) ;
  271. if( nextemptycell(board) == -1 )
  272. break ;
  273.  
  274. svar = new SudokuState(nextemptycell(board), board) ;
  275.  
  276. }
  277. }
  278.  
  279. return board ;
  280. }
  281.  
  282. public void printboard(int[][] board) {
  283. // TODO Auto-generated method stub
  284. for( int i = 0 ; i < board.length ; i++ )
  285. { System.out.println();
  286. for( int j = 0 ; j < board.length ; j++ )
  287. System.out.print( " " + board[i][j]);
  288. }
  289. }
  290.  
  291. /**
  292. * @param args
  293. */
  294. public static void main(String[] args) {
  295. // TODO Auto-generated method stub
  296. //PLEASE READ THE COMMENTS BEFORE USING
  297. // place -1 in in board matrix array below if value is not known
  298. int board[][] = {{-1,2,-1,-1,9,-1,-1,-1,-1},{-1,6,5,8,2,-1,-1,-1,-1},{3,-1,-1,-1,5,-1,-1,-1,7},{-1,3,8,-1,-1,-1,-1,9,5},{6,7,-1,9,-1,5,-1,2,8},{5,1,-1,-1,-1,-1,6,7,-1},{9,-1,-1,-1,8,-1,-1,-1,1},{-1,-1,-1,-1,6,9,8,5,-1},{-1,-1,-1,-1,1,-1,-1,4,-1}} ;
  299. SudokuSolver ssolve = new SudokuSolver() ;
  300. ssolve.printboard(board) ;
  301. int sboard[][] = ssolve.solveSudoku( board ) ;
  302. System.out.println("\n\n solved soduko game \n\n") ;
  303. ssolve.printboard(sboard) ;
  304. System.exit(0) ;
  305. }
  306.  
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement