Files
sudokusolver/to cpu intensive
T

359 lines
7.0 KiB
Plaintext
Raw Normal View History

2014-02-15 09:29:09 +01:00
//LinkedListSudoku LLS[];
/*protected class LinkedListSudoku
{
//Total list.
LinkedListSudoku previouse = null;
LinkedListSudoku next = null;
//Row
LinkedListSudoku left = null;
LinkedListSudoku right = null;
//Column
LinkedListSudoku top = null;
LinkedListSudoku bottom = null;
//Box
LinkedListSudoku boxPreviouse = null;
LinkedListSudoku boxNext = null;
//Value
Integer value = null;
Integer calculatedValue = null;
LinkedListSudoku(int iValue)
{
value = Integer.valueOf(iValue);
}
public String GetValue()
{
return (value!=Integer.valueOf(0)?value:calculatedValue!=null?calculatedValue:Integer.valueOf(0)).toString();
}
public boolean IsInput()
{
return value==0?false:true;
}
private boolean HasDuplicates(Integer[] arrayList)
{
ArrayList<Integer> vals = new ArrayList<Integer>();
boolean returnValue = false;
for(int i=0; i<arrayList.length; i++)
{
if(vals.contains(arrayList[i]))
{
returnValue = true;
break;
}
if (arrayList[i] != 0)
vals.add(arrayList[i]);
}
return returnValue;
}
public LinkedListSudoku GetLeft()
{
if (this.left == null)
return this;
else
return left.GetLeft();
}
public LinkedListSudoku GetTop()
{
if (this.top == null)
return this;
else
return top.GetTop();
}
public LinkedListSudoku GetFirstBox()
{
if (this.boxPreviouse == null)
return this;
else
return boxPreviouse.GetFirstBox();
}
//returns false if valid
public boolean CheckRow()
{
LinkedListSudoku begin = this.GetLeft();
LinkedListSudoku next;
Integer[] temp = new Integer[9];
int i = 0;
next = begin;
do{
temp[i] = next.value;
i++;
next = next.right;
}while(next != null);
return HasDuplicates(temp);
}
public boolean CheckColumn()
{
LinkedListSudoku begin = this.GetTop();
LinkedListSudoku next;
Integer[] temp = new Integer[9];
int i = 0;
next = begin;
do{
temp[i] = next.value;
i++;
next = next.bottom;
}while(next != null);
return HasDuplicates(temp);
}
//returns false if valid.
public boolean CheckBox()
{
LinkedListSudoku begin = this.GetFirstBox();
LinkedListSudoku next;
Integer[] temp = new Integer[9];
int i = 0;
next = begin;
do{
temp[i] = next.value;
i++;
next = next.boxNext;
}while(next != null);
return HasDuplicates(temp);
}
//returns false if found valid number.
public boolean GetValidNummer()
{
if (!this.IsInput())
{
//Pre-Check if grid is valid.
if (!this.CheckRow())
{
if (!this.CheckColumn())
{
if (!this.CheckBox())
{
//valid.
HashSet<Integer> vals = new HashSet<Integer>();
for (int i=1; i<=9; i++) vals.add(i);
//remove row
LinkedListSudoku begin = this.GetLeft();
LinkedListSudoku next;
next = begin;
do{
vals.remove(next.value);
vals.remove(next.calculatedValue);
next = next.right;
}while(next != null);
//remove column
begin = this.GetTop();
next = begin;
do{
vals.remove(next.value);
vals.remove(next.calculatedValue);
next = next.bottom;
}while(next != null);
//remove box
begin = this.GetFirstBox();
next = begin;
do{
vals.remove(next.value);
vals.remove(next.calculatedValue);
next = next.boxNext;
}while(next != null);
//remove last value
if (this.calculatedValue != null)
for (int i=1; i<=this.calculatedValue; i++)
vals.remove(i);
//return value
if (!vals.isEmpty())
{
this.calculatedValue = vals.iterator().next();
return false;
}
else
{
this.calculatedValue = 0;
return true;
}
}
}
}
}
else // is input
return false;
//not valid
return true;
}
}*/
//returns false if grid is valid
/*protected boolean CheckGridForValidation(LinkedListSudoku iLLS[])
{
//check grid for errors
for (int i=0;i<9;i++)
{
if ( iLLS[i*10].CheckRow() )
return true;
if ( iLLS[i*10].CheckColumn() )
return true;
if ( iLLS[6].CheckBox() )
return true;
}
for (int i=0; i<81; i+=27)
{
for (int x=0; x<9; x+=3)
{
if ( iLLS[i+x].CheckBox() )
return true;
}
}
return false;
}
*/
/*public void Send(View v)
{
AssembleLL();
Toast.makeText(this, CheckGridForValidation(LLS)?"Your Sudoku is Invalid.":"Your Sudoku is Valid.", Toast.LENGTH_LONG).show();
for (int i=0;i<81;i++)
{
if (LLS[i].GetValidNummer())
{
if (i==0)
{
if (LLS[i].IsInput())
break;
break;
}
i-=2;
}
}
SetGridData(GetListOfLL());
}*/
/*protected void AssembleLL()
{
ArrayList<String> list = GetGridData();
LLS = new LinkedListSudoku[81];
LLS[0] = new LinkedListSudoku(Integer.parseInt(list.get(0)));
//list
for (int i=1; i<81; i++)
{
LLS[i] = new LinkedListSudoku(Integer.parseInt(list.get(i)));
LLS[i-1].next = LLS[i];
LLS[i].previouse = LLS[i-1];
}
//rows
for (int r=0;r<9;r++)
{
for (int i=(r*9)+1;i<(r*9)+9;i++)
{
LLS[i-1].right = LLS[i];
LLS[i].left = LLS[i-1];
}
}
//columns
for (int c=0;c<9;c++)
{
for (int i=1;i<9;i++)
{
LLS[c+((i-1)*9)].bottom = LLS[c+(i*9)];
LLS[c+(i*9)].top = LLS[c+((i-1)*9)];
}
}
//boxes
LinkedListSudoku temp = null;
for (int e=0; e<81; e+=27)
{
temp = null;
for (int i=e+0; i<e+27; i+=9)
{
for (int x=0;x<3;x++)
{
if (temp != null)
{
temp.boxNext = LLS[i+x];
LLS[i+x].boxPreviouse = temp;
}
temp = LLS[i+x];
}
}
}
for (int e=0; e<81; e+=27)
{
temp = null;
for (int i=e+0; i<e+27; i+=9)
{
for (int x=3;x<6;x++)
{
if (temp != null)
{
temp.boxNext = LLS[i+x];
LLS[i+x].boxPreviouse = temp;
}
temp = LLS[i+x];
}
}
}
for (int e=0; e<81; e+=27)
{
temp = null;
for (int i=e+0; i<e+27; i+=9)
{
for (int x=6;x<9;x++)
{
if (temp != null)
{
temp.boxNext = LLS[i+x];
LLS[i+x].boxPreviouse = temp;
}
temp = LLS[i+x];
}
}
}
}*/
/*protected ArrayList<String> GetListOfLL()
{
if (LLS != null)
{
ArrayList<String> list = new ArrayList<String>();
for (int i=0;i<81;i++)
{
list.add(LLS[i].GetValue());
}
return list;
}
return null;
}*/