Bug in sudoku solver

Okay, so finally I found a bug in the sudoku solver code. The problem is, given an unsolvable grid, this solution will go over infinite loop. And this is crazy right? I mean it should come out by saying that this grid is unsolvable. And I was expecting that my solve routing will return -1 in case of unsolvable sudoku, but it doesn’t. I need to fix it somehow.

Advertisements

2 thoughts on “Bug in sudoku solver

  1. Okay, there is no bug in sudoku code, actually I was trying it with a very strange grid. Something like:

    1 0 0 …….. 0
    0 1 0 …….. 0
    0 0 0 …….. 0

    So, here the 1st cube is containing two 1s, which is invalid and such sudoku can’t be completed ever. The code is taking so much of time to report failure that made me think that there is a bug in the code. Suddenly I tried the same grid with 2×2 sudoku, and it worked. The code is bug free.

  2. Shell output:
    vishal@ubuntu:~/progs/c/sudoku\ 02:01:34 PM >$ ./a.out
    1 0 0 0
    0 1 0 0
    0 0 0 0
    0 0 0 0
    ===
    ===
    ===
    Sudoku Error: i = 1, j = 1
    Failed attemps:39
    vishal@ubuntu:~/progs/c/sudoku\ 02:01:35 PM >$ vi new.c
    vishal@ubuntu:~/progs/c/sudoku\ 02:02:40 PM >$ gcc new.c
    vishal@ubuntu:~/progs/c/sudoku\ 02:02:41 PM >$ ./a.out
    1 0 0 0
    0 0 0 0
    0 0 0 0
    1 0 0 0
    ===
    ===
    ===
    Sudoku Error: i = 2, j = 3
    Failed attemps:267

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s