Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Automatic Solver

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Automatic Solver

    Between the 2 methods below it should solve a Sudoku puzzle automatically,
    Expected output is this : http://moritz.faui2k3.org/images/sudoku/default.png
    My output the same but only with the number one.

    row and col both = 0 when starting
    public boolean solve(int row, int col, char[][] board) {
    		if (row == 9) 
    		{
    	        row = 0;
    		    col++;
    		}
    	    if ((col) == 9)
    	    {
    	      return true;                //Base case - puzzle solved.
    	    }       
    	  if(board[row][col] =='1' || board[row][col] =='2' || board[row][col] =='3' || board[row][col] =='4'
    				     || board[row][col] =='5' |board[row][col] =='6' ||
    				 board[row][col] =='7' ||board[row][col] =='8' ||board[row][col] =='9') // skip filled cells
    	{
     
    	        solve(row+1,col,board);   		    //recursive call
    	}
     
    	for(int val = 1; val <10;val++) 
    	{
    		if (valid(row,col,val,board))  //checks if value is legal
    		{
    			Object input = val;
    			String s = input.toString().trim();
     
     
    	                board[row][col] = s.charAt(0);;
    	                fireTableCellUpdated(row, col);
    		}
    	                if (solve(row+1,col,board))     //call solve starting at next cell
    	                {
    	                    return true;
    	                }
     
    	}      
     
     
    	board[row][col] = '0'; // reset on backtrack
    	return false;
     
     
     
    	}

    public Boolean valid(int row, int col, int val, char[][] board) {
     
    		Object input = val;
    		String s = input.toString().trim();
    		//Check Rows
     
    		for(int i = 0; i <9; i++)
    		{
     
    		    //Check Rows
    	            if(  s.charAt(0) == board[i][col])
    	            		{
    	                return false;
    	            		}
    	            //Check Cols
    	            if (s.charAt(0) == board[row][i])
    	            		{
    	                return false;
    	            		}
    		}
     
    		//check box
    	        int boxRowOffset = (row / 3)*3;
    	        int boxColOffset = (col / 3)*3;
    	        for (int k = 0; k < 3; k++) // box
    	            for (int m = 0; m < 3; m++)
    	            {
    	            	{
    	                if (boxRowOffset+k!=row && boxColOffset+m!=col)
    	            	if (s.charAt(0) == board[boxRowOffset+k][boxColOffset+m])
    	            	{{
    	                    return false;
    	            	}}
    	            }
    	            }
     
    	        return true; // no violations, so it's legal
    	    }
    Last edited by ppme; December 5th, 2011 at 04:05 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Automatic Solver

    Do you have a question?

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Automatic Solver

    Quote Originally Posted by Norm View Post
    Do you have a question?
    Can anyone see why my output wont match the correct output

  4. #4
    Junior Member
    Join Date
    Dec 2011
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Automatic Solver

    Got it. Finally.

Similar Threads

  1. KeyListeners: Automatic Focus?
    By bgroenks96 in forum Java Theory & Questions
    Replies: 32
    Last Post: June 24th, 2011, 09:03 PM
  2. Replies: 0
    Last Post: December 26th, 2010, 10:15 AM
  3. Sudoku solver
    By Elliott50 in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 22nd, 2009, 02:03 PM
  4. Sudoku Solver
    By MysticDeath in forum Java Theory & Questions
    Replies: 5
    Last Post: September 19th, 2009, 09:33 PM
  5. Automatic correction of brightness and contrast
    By Mirko in forum Java Theory & Questions
    Replies: 1
    Last Post: September 16th, 2009, 06:26 PM