| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
import math |
|---|
| 5 |
|
|---|
| 6 |
class gridModell(object): |
|---|
| 7 |
|
|---|
| 8 |
def __init__(self,set=[]): |
|---|
| 9 |
modell=[] |
|---|
| 10 |
for x in set: |
|---|
| 11 |
line=[] |
|---|
| 12 |
for z in set: |
|---|
| 13 |
line.append(0) |
|---|
| 14 |
modell.append(line) |
|---|
| 15 |
self.modell=modell |
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
def fill(self,value,position): |
|---|
| 19 |
ret=True |
|---|
| 20 |
line=int(position/len(self.modell)) |
|---|
| 21 |
col=position-line*len(self.modell) |
|---|
| 22 |
if self.modell[line][col]==0: |
|---|
| 23 |
self.modell[line][col]=value |
|---|
| 24 |
else: |
|---|
| 25 |
ret=False |
|---|
| 26 |
return ret |
|---|
| 27 |
|
|---|
| 28 |
def string(self): |
|---|
| 29 |
return str(self.modell) |
|---|
| 30 |
|
|---|
| 31 |
def getModell(self): |
|---|
| 32 |
return self.modell |
|---|
| 33 |
|
|---|
| 34 |
def toString(self): |
|---|
| 35 |
ret='' |
|---|
| 36 |
sqrt=math.sqrt(len(self.modell)) |
|---|
| 37 |
colIndex=0 |
|---|
| 38 |
for x in self.modell: |
|---|
| 39 |
lineIndex=0 |
|---|
| 40 |
for z in x : |
|---|
| 41 |
ret+=str(z)+' ' |
|---|
| 42 |
if lineIndex%(sqrt)==sqrt-1: |
|---|
| 43 |
ret+='| ' |
|---|
| 44 |
lineIndex+=1 |
|---|
| 45 |
ret+='\n' |
|---|
| 46 |
if colIndex%sqrt==sqrt-1: |
|---|
| 47 |
for do in range(0, int(len(self.modell)+sqrt)): |
|---|
| 48 |
ret+='--' |
|---|
| 49 |
ret+='\n' |
|---|
| 50 |
colIndex+=1 |
|---|
| 51 |
return ret |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
class sudokuGridModell(object): |
|---|
| 55 |
|
|---|
| 56 |
def __init__(self,set=[]): |
|---|
| 57 |
modells={} |
|---|
| 58 |
for x in set: |
|---|
| 59 |
modells[x]=gridModell(set) |
|---|
| 60 |
modells['total']=gridModell(set) |
|---|
| 61 |
self.modells=modells |
|---|
| 62 |
self.set=set |
|---|
| 63 |
|
|---|
| 64 |
def testAndFill(self,value,position): |
|---|
| 65 |
ret=True |
|---|
| 66 |
myLen=len(self.modells.keys())-1 |
|---|
| 67 |
sqrt=int(math.sqrt(myLen)) |
|---|
| 68 |
if position in range(0, myLen*myLen): |
|---|
| 69 |
line=int(position/myLen) |
|---|
| 70 |
col=position-line*myLen |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
test=self.isPossible(value, position) |
|---|
| 77 |
ret=test |
|---|
| 78 |
if test: |
|---|
| 79 |
self.modells['total'].modell[line][col]=value |
|---|
| 80 |
for nc in range(0,myLen): |
|---|
| 81 |
self.modells[value].modell[line][nc]=value |
|---|
| 82 |
for nl in range(0,myLen): |
|---|
| 83 |
self.modells[value].modell[nl][col]=value |
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
firstLine=int(col/sqrt)*sqrt |
|---|
| 90 |
lastLine=int(firstLine+sqrt) |
|---|
| 91 |
firstCol=int(line/sqrt)*sqrt |
|---|
| 92 |
lastCol=int(firstCol+sqrt) |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
for nc in range(firstLine, lastLine): |
|---|
| 97 |
for nl in range(firstCol, lastCol): |
|---|
| 98 |
if nl<myLen and nc<myLen: |
|---|
| 99 |
self.modells[value].modell[nl][nc]=value |
|---|
| 100 |
for x in self.modells.keys(): |
|---|
| 101 |
if x==value or x=='total': |
|---|
| 102 |
print self.modells[x].toString() |
|---|
| 103 |
else: |
|---|
| 104 |
ret=False |
|---|
| 105 |
return ret |
|---|
| 106 |
|
|---|
| 107 |
def isPossible(self,value,position): |
|---|
| 108 |
ret=True |
|---|
| 109 |
myLen=len(self.modells.keys())-1 |
|---|
| 110 |
sqrt=int(math.sqrt(myLen)) |
|---|
| 111 |
if position in range(0, myLen*myLen): |
|---|
| 112 |
line=int(position/myLen) |
|---|
| 113 |
col=position-line*myLen |
|---|
| 114 |
if self.modells['total'].modell[line][col]>0 or not(self.modells.has_key(value) and self.modells[value].modell[line][col]==0): |
|---|
| 115 |
ret=False |
|---|
| 116 |
else: |
|---|
| 117 |
ret=False |
|---|
| 118 |
return ret |
|---|
| 119 |
|
|---|
| 120 |
def fill(self,value,position): |
|---|
| 121 |
ret=True |
|---|
| 122 |
myLen=len(self.modells.keys())-1 |
|---|
| 123 |
sqrt=int(math.sqrt(myLen)) |
|---|
| 124 |
if position in range(0, myLen*myLen): |
|---|
| 125 |
line=int(position/myLen) |
|---|
| 126 |
col=position-line*myLen |
|---|
| 127 |
self.modells['total'].modell[line][col]=value |
|---|
| 128 |
for nc in range(0,myLen): |
|---|
| 129 |
self.modells[value].modell[line][nc]=value |
|---|
| 130 |
for nl in range(0,myLen): |
|---|
| 131 |
self.modells[value].modell[nl][col]=value |
|---|
| 132 |
firstLine=int(col/sqrt)*sqrt |
|---|
| 133 |
lastLine=int(firstLine+sqrt) |
|---|
| 134 |
firstCol=int(line/sqrt)*sqrt |
|---|
| 135 |
lastCol=int(firstCol+sqrt) |
|---|
| 136 |
for nc in range(firstLine, lastLine): |
|---|
| 137 |
for nl in range(firstCol, lastCol): |
|---|
| 138 |
|
|---|
| 139 |
self.modells[value].modell[nl][nc]=value |
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
else: |
|---|
| 144 |
ret=False |
|---|
| 145 |
return ret |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
def isFinished(self): |
|---|
| 149 |
ret=True |
|---|
| 150 |
for line in self.modells['total'].modell: |
|---|
| 151 |
for col in line: |
|---|
| 152 |
if col==0: |
|---|
| 153 |
ret=False |
|---|
| 154 |
return ret |
|---|
| 155 |
|
|---|
| 156 |
def restInLine(self, position): |
|---|
| 157 |
ret=[] |
|---|
| 158 |
ret.extend(self.set) |
|---|
| 159 |
myLen=len(self.modells.keys())-1 |
|---|
| 160 |
sqrt=int(math.sqrt(myLen)) |
|---|
| 161 |
if position in range(0, myLen*myLen): |
|---|
| 162 |
line=int(position/myLen) |
|---|
| 163 |
for val in self.modells['total'].modell[line]: |
|---|
| 164 |
if val in ret: |
|---|
| 165 |
ret.remove(val) |
|---|
| 166 |
return ret |
|---|
| 167 |
|
|---|
| 168 |
def restInCol(self, position): |
|---|
| 169 |
ret=[] |
|---|
| 170 |
ret.extend(self.set) |
|---|
| 171 |
myLen=len(self.modells.keys())-1 |
|---|
| 172 |
sqrt=int(math.sqrt(myLen)) |
|---|
| 173 |
if position in range(0, myLen*myLen): |
|---|
| 174 |
line=int(position/myLen) |
|---|
| 175 |
col=position-line*myLen |
|---|
| 176 |
for newPosition in range(col,myLen*myLen, myLen): |
|---|
| 177 |
newLine=int(newPosition/myLen) |
|---|
| 178 |
newCol=newPosition-newLine*myLen |
|---|
| 179 |
val=self.modells['total'].modell[newLine][newCol] |
|---|
| 180 |
|
|---|
| 181 |
if val in ret: |
|---|
| 182 |
ret.remove(val) |
|---|
| 183 |
return ret |
|---|
| 184 |
|
|---|
| 185 |
def restInCell(self, position): |
|---|
| 186 |
ret=[] |
|---|
| 187 |
ret.extend(self.set) |
|---|
| 188 |
myLen=len(self.modells.keys())-1 |
|---|
| 189 |
sqrt=int(math.sqrt(myLen)) |
|---|
| 190 |
if position in range(0, myLen*myLen): |
|---|
| 191 |
line=int(position/myLen) |
|---|
| 192 |
col=position-line*myLen |
|---|
| 193 |
firstLine=int(col/sqrt)*sqrt |
|---|
| 194 |
lastLine=int(firstLine+sqrt) |
|---|
| 195 |
firstCol=int(line/sqrt)*sqrt |
|---|
| 196 |
lastCol=int(firstCol+sqrt) |
|---|
| 197 |
for nc in range(firstLine, lastLine): |
|---|
| 198 |
for nl in range(firstCol, lastCol): |
|---|
| 199 |
|
|---|
| 200 |
val=self.modells['total'].modell[nl][nc] |
|---|
| 201 |
|
|---|
| 202 |
if val in ret: |
|---|
| 203 |
ret.remove(val) |
|---|
| 204 |
return ret |
|---|
| 205 |
|
|---|
| 206 |
def restAtPosition(self, position): |
|---|
| 207 |
ret=[] |
|---|
| 208 |
myLen=len(self.modells.keys())-1 |
|---|
| 209 |
sqrt=int(math.sqrt(myLen)) |
|---|
| 210 |
if position in range(0, myLen*myLen): |
|---|
| 211 |
line=int(position/myLen) |
|---|
| 212 |
col=position-line*myLen |
|---|
| 213 |
if self.modells['total'].modell[line][col]==0: |
|---|
| 214 |
for line in self.restInLine(position): |
|---|
| 215 |
for col in self.restInCol(position): |
|---|
| 216 |
for cell in self.restInCell(position): |
|---|
| 217 |
if line==col and line==cell: |
|---|
| 218 |
ret.append(line) |
|---|
| 219 |
return ret |
|---|
| 220 |
|
|---|
| 221 |
def nextCase(self, position): |
|---|
| 222 |
ret=position |
|---|
| 223 |
return ret |
|---|
| 224 |
|
|---|
| 225 |
def restInCase(self, position): |
|---|
| 226 |
ret=postion |
|---|
| 227 |
return ret |
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
set=[1,2,3,4,5,6,7,8,9] |
|---|
| 231 |
|
|---|
| 232 |
myLen=int(len(set)) |
|---|
| 233 |
sqrt=int(math.sqrt(myLen)) |
|---|
| 234 |
newSudokuGrid=sudokuGridModell(set) |
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
positionDone=[] |
|---|
| 238 |
offset=int(0) |
|---|
| 239 |
for x in range(0,int((myLen*myLen)+(sqrt*sqrt))): |
|---|
| 240 |
offset=int(x) |
|---|
| 241 |
step=int(offset/myLen) |
|---|
| 242 |
rest=int(offset%myLen) |
|---|
| 243 |
position=(x*myLen+step+rest)%(myLen*myLen) |
|---|
| 244 |
val=newSudokuGrid.restAtPosition(int(position)) |
|---|
| 245 |
if len(val)>0: |
|---|
| 246 |
add=val[0] |
|---|
| 247 |
newSudokuGrid.fill(add,int(position)) |
|---|
| 248 |
|
|---|
| 249 |
positionDone.append(position) |
|---|
| 250 |
else: |
|---|
| 251 |
print ".......... why ????" |
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
print positionDone |
|---|
| 255 |
print newSudokuGrid.modells['total'].toString() |
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
|
|---|