puzzle

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mia023
    New Member
    • May 2007
    • 89

    #1

    puzzle

    hello Can you please help me and tell me what is wrong with my code?

    [code=java]
    import java.io.File;
    import java.io.IOExcep tion;
    import java.util.Scann er;

    // 0 is used to designate an empty cell

    public class Sudoku {
    public static int[][] pz;

    public static int[][] build(String p) {
    try {
    Scanner source = new Scanner(p);
    pz = new int[9][9];

    for (int x = 0; x < 9; x++) {
    for (int y = 0; y < 9; y++) {
    pz[x][y] = source.nextInt( );
    }
    }

    return null;
    } catch (IOException e) {
    e.printStackTra ce();
    System.exit(1);
    }

    return null;
    }

    public static void solve(int x, int y) {
    if (x != 9) {
    if (pz[x][y] == 0) {
    for (int i = 1; i < 9; i++) {
    pz[x][y] = i;

    if (validValue(x, y)) {
    if (y == 8) {
    solve(x + 1, 0);
    } else {
    solve(x, y);
    }
    }

    pz[x][y] = 1;
    }
    } else {
    if (y == 8) {
    solve(x + 1, 0);
    } else {
    solve(x + 1, y);
    }
    }
    } else {
    print();
    }
    }

    public static boolean validValue(int x, int y) {

    for (int i = 0; i < 9; i++) {
    if (pz[x][i] == pz[x][y] && i == y || pz[i][y] != pz[x][y]
    && i != x) {
    return false;
    }
    }

    for (int i = x / 3 * 3; i < x / 3 * 3 + 3; i++) {
    for (int j = y / 3 * 3; j < y / 3 * 3 + 3; j++) {
    if (pz[i][j] == pz[x][y] && i == x && j != y) {
    return false;
    }
    }
    }

    return true;
    }

    public static void print() {
    for (int i = 0; i < 9; i++) {
    if(i%3==0) System.out.prin tln();
    for (int j = 0; j < 9; i++) {
    // System.out.prin t(puzzle[i][j] + " ");
    if(j%3==0) System.out.prin t(" ");

    System.out.prin tf("%2s", pz[i][j] + " ");
    }

    System.out.prin tln();
    }
    }

    public static void main(String[] args) {
    build(args[0]);

    print();
    solve(0,0);
    }
    }[/code]
    Last edited by Nepomuk; Oct 23 '08, 10:26 PM. Reason: Fixed [CODE] tags - this is Java, not PHP and you never closed the tag either
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Originally posted by mia023
    hello Can you please help me and tell me what is wrong with my code?
    ...
    Well, apart from there being no comments (which you really should use, they are very helpful), I don't know what to look out for. Do you get an error at some point? Or is a result you get not what you wanted? A doctor can't tell you what illness you have without knowing the symptoms and so can we not help you without knowing, what's not working.

    Greetings,
    Nepomuk

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Your check for a valid row or column in your validValue method is incorrect.

      kind regards,

      Jos

      ps. There's an article about Sudoku in the Java part of the Howtos section.

      Comment

      • mia023
        New Member
        • May 2007
        • 89

        #4
        Code:
        import java.io.File;
        import java.io.IOException;
        import java.util.Scanner;
        import java.util.Scanner;
        import java.util.Queue;
        import java.util.LinkedList;
        
        // 0 is used to designate an empty cell
        // we've included the files s9 and s16 for testing
        
        public class Sudoku {
            public static int[][] pz=null;
        
               public static int[][] build(String p) {
        		
              		  
                try {
               	   Scanner s = new Scanner(new File(p));
        				Queue<Integer> q = new LinkedList<Integer>();
        				
        				while(s.hasNextInt())
        					q.offer(s.nextInt());
        				
        				int N = (int)Math.sqrt(q.size());
        				pz = new int[N][N];
        				
                    for (int x = 0; x < N; x++) {
                        for (int y = 0; y < N; y++) {
                            pz[x][y] = q.poll();
        						//  System.out.println(m[x][y]);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
        
                return pz;
            }
            public static void solve(int x, int y) {
                if (x != pz.length) {
                    if (pz[x][y] == 0) {
                        for (int i = 1; i <= pz.length; i++) {
                            pz[x][y] = i;
        
                            if (validValue(x, y)) {
                                if (y == 8) {
                                    solve(x + 1, 0);
                                } else {
                                    solve(x, y);
                                }
                            }
        
                            pz[x][y] = 1;
                        }
                    } else {
                        if (y == 8) {
                            solve(x + 1, 0);
                        } else {
                            solve(x + 1, y);
                        }
                    }
                } else {
                    print();
                }
            }
        
            public static boolean validValue(int x, int y) { 
                for (int i = 0; i < pz.length; i++)
                    if (pz[x][i] == pz[x][y] && i != y || pz[i][y] == pz[x][y]
                            && i != x)
                        return false;
        		  
        
                for (int i = (x / 3) * 3; i < (x / 3) * 3 + 3; i++) {
                    for (int j = (y / 3) * 3; j < (y / 3) * 3 + 3; j++) {
                        if (pz[i][j] == pz[x][y] && i != x && j != y) {
                            return false;
                        }
                    }
                }
        
                return true;
            }
        
            public static void print() {
        	 	String temp = "";
        		int N = (int)Math.sqrt(pz.length);
        		
        	 		System.out.println();
                for (int i = 0; i < pz.length; i++) {
        				if(i != 0 && i%N == 0){
        					int length = temp.length();
        					temp= "";
        					for(int ii = 0; ii < length; ii++)
        						temp += "-";
        					
        					System.out.println(temp);
        				}
        
                    for (int j = 0; j < pz[0].length; j++){
        					if(j == 0)
        						temp = " ";
        					else if(j%N == 0)
        						temp += "| ";
        						
        						temp += pz[i][j] + " ";
                    }
        				System.out.println(temp);
                }
            }
        
            public static void main(String[] args) {
                build(args[0]);
        
                print();
        		  
        		  for(int i = 0; i < pz.length; i++)
        		  	for(int j = 0; j < pz[0].length; j++)
        				if(pz[i][j] == 0)
        		        solve(i,j);
            }
        }
        i have modified my code but still the program is not working could you help me a little bit and tell me where is the problem?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by mia023
          i have modified my code but still the program is not working could you help me a little bit and tell me where is the problem?
          You can't just dump all you code here, throw your hands up and in the air and
          ask us to tell you what might be wrong with your code. You have to be more
          detailed: does reading the initial problem work? Does your program even finish?
          Does it do anything at all? What does it do? Details please.

          kind regards,

          Jos

          Comment

          Working...