Sudoku correctness checking

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jondlar
    New Member
    • Sep 2008
    • 9

    #16
    Originally posted by Rajesh V
    Thank you for the counter example. But I think except the input with all the numbers 5, the sum checking should work. The sum checking should be done for all rows, columns, and 3x3 squares.

    Thanks again,
    Rajesh V

    Imagine a row 1 2 3 4 5 6 6 9 9

    All you have to do is increase one number from a row, and decrease another from same row and bingo you have sum 45.

    Comment

    • ningxin
      New Member
      • Sep 2008
      • 5

      #17
      Originally posted by newb16
      ||0x080||0x100

      mind the space and semicolon after expression, and semicolon after } in enum declaration. Also you can't assign anything to bitflag or compare it with something - it is enum type, not variable.
      i have edited the mistakes but it seemed the program still contains errors. according to jos, i should use flags to check for errors. so what should be done so that the code can work? from what i checked on the internet, people assign characters (ie apple) to flag instead of integers. how do i go about giving a flag an integer value? for the last flag, is 0x100 actually a correct flag?

      in the meantime i have been asking around quite a few people too and there were many suggestions on what i should do. i tried them all out too, together with the ideas all of you kindly suggested to me (thanks very much). below is part of another code to test a particular row, which is to find duplicating numbers. but when i run it, it kept on giving me the same error no matter which input file i put it in. does anyone have any idea?

      [HTML] std::map<int, int> freq;

      for ( int i = 0; i < 9; i++ )
      ++freq[std::rand() % 9];

      std::cout<<"Dup licates:\n";
      std::map<int, int>::iterator it = freq.begin();

      while ( it != freq.end() ) {
      if ( it->second > 1 )
      std::cout<< it->first <<" appears "<< it->second <<" times in row 1\n ";
      ++it;
      }[/HTML]

      thanks again for all of your help. i really appreciate it, because my assignment is really going to be due saturday night, and my program is not really going anywhere after trying for 5 days.

      Comment

      • stragatto
        New Member
        • Sep 2008
        • 7

        #18
        The idea of checksum=45 is ok, but you should check that there is no row (or column, or square) filled with all "5". This could be done by converting the array (if the checksum is 45) into a string and checking that the string does not contain the substring "55".
        (if I remember well, the library function for substring matching should be strstr() )
        Sorry for being so short, but I must go to our monthly LUG meeting and it's late :)

        Comment

        • Jondlar
          New Member
          • Sep 2008
          • 9

          #19
          Originally posted by stragatto
          The idea of checksum=45 is ok, but you should check that there is no row (or column, or square) filled with all "5". This could be done by converting the array (if the checksum is 45) into a string and checking that the string does not contain the substring "55".
          (if I remember well, the library function for substring matching should be strstr() )
          Sorry for being so short, but I must go to our monthly LUG meeting and it's late :)
          This is a row with no "55"
          1 3 3 4 5 6 7 7 9

          The sum = 45

          Its not about repeated 5's. If you have any repeated number then the sudoku is incorrect. So you might want to check for all repeated numbers not just "55".

          Comment

          • stragatto
            New Member
            • Sep 2008
            • 7

            #20
            Originally posted by Jondlar
            This is a row with no "55"
            1 3 3 4 5 6 7 7 9

            The sum = 45

            Its not about repeated 5's. If you have any repeated number then the sudoku is incorrect. So you might want to check for all repeated numbers not just "55".
            Ah, right! I was in a hurry and I didn't take enough time to think :)
            Another algorythm could be as follows:
            1) Sort the items of the array
            (The result for a sudoku valid array should be ; 1 2 3 4 5 6 7 8 9)
            2) check that in the sorted array item[4]=5
            3) check that in the sorted array item[0]+item[8]=10
            4) check that in the sorted array item[1]+item[7]=10
            5) check that in the sorted array item[2]+item[6]=10
            6) check that in the sorted array item[3]+item[5]=10
            Finally, check that in the sorted array no item[j]=item[j+1]
            (this last, to check that there are not repeated couples whose sum is 10)
            Of course, the order of the checks should be optimized for efficiency: maybe the check I have put as last one should be done as first or second !
            Or maybe the checks could be done while sorting, with some addition to the bubblesort or quicksort algorythm !

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #21
              Originally posted by stragatto
              Or maybe the checks could be done while sorting, with some addition to the bubblesort or quicksort algorythm !
              There is no need to sort anything; just check if all the nine numbers are present
              in that sequence of nine numbers; also see my reply #7.

              kind regards,

              Jos

              Comment

              • stragatto
                New Member
                • Sep 2008
                • 7

                #22
                Originally posted by JosAH
                There is no need to sort anything; just check if all the nine numbers are present
                in that sequence of nine numbers; also see my reply #7.

                kind regards,

                Jos
                Yes, at the end, when going to real coding, I realized that the simplest way is to do two nested loops with a flag, and to check on the flag.
                Since I am lazy, I did it in perl, but I hope that the comments could ease an eventual translation into other languages.

                Code:
                ### sudotest.pl ###
                
                #reading the lines in the file#
                open(SUDOKU,"sudoku.txt") or die "No such file !";
                for($i=0;$i<9;$i++){
                $myline[$i]=<SUDOKU>;
                chomp $myline[$i]; #this cuts away the newline char
                }
                close(SUDOKU);
                
                # print $myline[0]; #this was for debugging
                
                # and now let's check a line
                check($myline[3]);
                
                sub check {
                    $line=$_[0]; 
                    # this is cryptic for non-perl-users:
                    # perl passes the parameters to the functions
                    # as an array called @_; $_[0] is the first
                    # and in this case the one and only item of @_
                    
                
                    #let us split the line into the array @digit 
                    @digit = split(//,$line); 
                
                    #and now start with digit-by-digit check
                    foreach $digit(@digit){
                        $flag=0;
                        foreach $item(@digit){
                        if($digit==$item){$flag++;}
                        #print $flag; #this for debugging only
                        if($flag>1){die "Invalid Sudoku!"};
                        }
                
                    }
                }
                Thanks.

                Comment

                Working...