Original Solution for this problem
Sorry for giving you guys solution in the Java. But similar approach can be taken for writing in C. Infact, I prefer to write in C but since i am professional java developer, so i had to write. It was asked in one of the interview question from me.
I have three files, test.java, Sudoku.java and Helper.java. Put all these three file in any defaul package and run the test.java and you have it.
Code:
**************************************
test.java
**************************************
import java.util.ArrayList;
import java.util.List;
/**
* @author Ajay.Singh
*
*/
public class test {
/**
* @param args
*/
public static void main(String[] args) {
int i,j;
int inputArray[][] = Sudoku.sudoku();
List<Helper> verticalList = new ArrayList<Helper>();
List<Helper> boxList = new ArrayList<Helper>();
for(i=0;i<9;i++){
List<Integer> horzList = new ArrayList<Integer>();
for(j=0;j<9;j++){
//To check the distinct value in the row
if(!horzList.contains(inputArray[i][j]))
horzList.add(new Integer(inputArray[i][j]));
else
System.out.println("Not distinct element in the row" + inputArray[i][j] );
//To create 9 columns - 9 list
if(i==0){
List<Integer> list = new ArrayList<Integer>();
Helper element = new Helper ();
element.setRow(list);
verticalList.add(element);
}
//To create 9 boxes - 9 list
if(i%3==0 && j%3==0){
List<Integer> boxesList = new ArrayList<Integer>();
Helper element = new Helper ();
element.setRow(boxesList);
boxList.add(element);
}
//To check for duplicate entry or else store in list
if(verticalList.get(j).getRow().contains(inputArray[i][j]))
System.out.println("Not distinct element in column" + inputArray[i][j] );
verticalList.get(j).getRow().add(inputArray[i][j]);
//To check for duplicate entry or else store in list
if(boxList.get((i/3!=0?3*(i/3):0)+j/3).getRow().contains(inputArray[i][j]))
System.out.println("Not distinct element in box" + inputArray[i][j] );
boxList.get((i/3!=0?3*(i/3):0)+j/3).getRow().add(inputArray[i][j]);
}
}
System.out.println("Yes this is perfect sudoku for 9 by 9");
}
}
Code:
*********************************************
Helper.java
*********************************************
import java.util.List;
public class Helper {
public List<Integer> row;
/**
* @return the row
*/
public List<Integer> getRow() {
return row;
}
/**
* @param row the row to set
*/
public void setRow(List<Integer> row) {
this.row = row;
}
}
Code:
***********************************************
Sudoku.java
***********************************************
/**
* @author Ajay.Singh
*
*/
public class Sudoku {
/**
* @return two dimensional array
*/
public static int[][] sudoku(){
final int sudoku[][]={
{7,6,2,5,8,9,3,4,1},
{1,5,3,2,6,4,9,7,8},
{4,8,9,1,3,7,5,6,2},
{6,4,1,3,2,8,7,5,9},
{5,9,7,6,4,1,2,8,3},
{2,3,8,7,9,5,4,1,6},
{9,2,4,8,7,6,1,3,5},
{8,7,5,9,1,3,6,2,4},
{3,1,6,4,5,2,8,9,7}
};
return sudoku;
}
}
If anybody is really interested for this to be written in C, just contact me ajay.pratap.singh@gmail.com and I will come back to you.
Good luck you student guys.
Ajay