您好,登錄后才能下訂單哦!
數獨問題解釋
一、數獨問題的基本規則
規則一:同一行數字不同
規則二:同一列數字不同
規則三:同一宮數字不同
二、解決數獨的策略
1,簡單方法
第一步,在每個格子中按照基本規則MT4寫入可能的結果
第二步,判斷每個格子中結果的個數,如果結果唯一,則寫入該數字。
第三步,返回步驟一,循環
停止的標志:填滿所有格子,或格子中可能的結果最小數大于1。
2,中級方法
第一步,使用簡單方法
第二步,在每一宮中,某個數字出現的次數只有一次,直接填入該數字
第三步,返回步驟一,循環
停止的標志:不在有數字填入
3,終極方法
第一步,使用中級方法
第二步,對每個位置的每個可能的答案進行假設,并推導矛盾,排除。
(1)在第一個位置上,填入第一個可能的答案
(2)使用中級方法,推導填入數字
(3)如果矛盾A出現,假設不成立,返回該位置,填入第二個答案。
如果矛盾B出現,假設暫時成立,進入下一位置,填入答案。
循環結束標志:填滿數字并符合基本規則
三 代碼如下
import pandas as pd
import numpy as np
shudu_data=pd.read_csv('data/shudu.csv')
data=shudu_data.copy()
def block(i,data):
ru3_1=data.iloc[0:3,0:3]
ru3_2=data.iloc[0:3,3:6]
ru3_3=data.iloc[0:3,6:]
ru3_4=data.iloc[3:6,0:3]
ru3_5=data.iloc[3:6,3:6]
ru3_6=data.iloc[3:6,6:]
ru3_7=data.iloc[6:,0:3]
ru3_8=data.iloc[6:,3:6]
ru3_9=data.iloc[6:,6:]
ru3_list=[ru3_1,ru3_2,ru3_3,ru3_4,ru3_5,ru3_6,ru3_7,ru3_8,ru3_9]
ru3=ru3_list[i]
ru3=ru3.values.tolist()
lis=[]
for l in ru3:
lis+=l
ru3=set(lis)-{0}
return ru3
def Block_dataframe(i,data):
ru3_1=pd.DataFrame(data=data.iloc[0:3,0:3],index=[0,1,2],columns=['0','1','2'])
ru3_2=pd.DataFrame(data=data.iloc[0:3,3:6],index=[0,1,2],columns=['3','4','5'])
ru3_3=pd.DataFrame(data=data.iloc[0:3,6:],index=[0,1,2],columns=['6','7','8'])
ru3_4=pd.DataFrame(data=data.iloc[3:6,0:3],index=[3,4,5],columns=['0','1','2'])
ru3_5=pd.DataFrame(data=data.iloc[3:6,3:6],index=[3,4,5],columns=['3','4','5'])
ru3_6=pd.DataFrame(data=data.iloc[3:6,6:],index=[3,4,5],columns=['6','7','8'])
ru3_7=pd.DataFrame(data=data.iloc[6:,0:3],index=[6,7,8],columns=['0','1','2'])
ru3_8=pd.DataFrame(data=data.iloc[6:,3:6],index=[6,7,8],columns=['3','4','5'])
ru3_9=pd.DataFrame(data=data.iloc[6:,6:],index=[6,7,8],columns=['6','7','8'])
ru3_list=[ru3_1,ru3_2,ru3_3,ru3_4,ru3_5,ru3_6,ru3_7,ru3_8,ru3_9]
return ru3_list[i]
def jianyan(data):
flag=True
for ind in range(9):
li_1=list(data.iloc[ind].values)
li_=[]
for li in li_1:
if li!=0:
li_.append(li)
flag=(len(set(li_))==len(li_))
if flag==False:
return flag
for col in ['0','1','2','3','4','5','6','7','8']:
li_2=list(data.iloc[:][col].values)
li_=[]
for li in li_2:
if li!=0:
li_.append(li)
#print(set(li_))
#print(li_)
flag=(len(set(li_))==len(li_))
if flag==False:
return flag
for i in range(9):
block_dataframe=Block_dataframe(i,data)
li_3=block_dataframe.values.tolist()
li_q=[]
for li_ in li_3:
li_q=li_q+li_
li_=[]
for li in li_q:
if li!=0:
li_.append(li)
flag=(len(set(li_))==len(li_))
if flag==False:
return flag
return flag
def full(data):
for i in range(9):
for j in ['0','1','2','3','4','5','6','7','8']:
if data[j][i]==0:
return False
return True
def Block_num(index,colums,data):
if index in [0,1,2]:
if colums in ['0','1','2']:
block_num=0
elif colums in ['3','4','5']:
block_num=1
else:
block_num=2
if index in [3,4,5]:
if colums in ['0','1','2']:
block_num=3
elif colums in ['3','4','5']:
block_num=4
else:
block_num=5
if index in [6,7,8]:
if colums in ['0','1','2']:
block_num=6
elif colums in ['3','4','5']:
block_num=7
else:
block_num=8
return block_num
def len_re(index,colums,data):
result={1,2,3,4,5,6,7,8,9}
ru1=set(data.iloc[index])
ru2=set(data[colums])
if index in [0,1,2]:
if colums in ['0','1','2']:
ru3=block(0,data)
elif colums in ['3','4','5']:
ru3=block(1,data)
else:
ru3=block(2,data)
if index in [3,4,5]:
if colums in ['0','1','2']:
ru3=block(3,data)
elif colums in ['3','4','5']:
ru3=block(4,data)
else:
ru3=block(5,data)
if index in [6,7,8]:
if colums in ['0','1','2']:
ru3=block(6,data)
elif colums in ['3','4','5']:
ru3=block(7,data)
else:
ru3=block(8,data)
re=result-ru1-ru2-ru3-{0}
return re
class result():
def result(self,data):
#計算每個位置上可能的結果,返回pos,ans
index=[0,1,2,3,4,5,6,7,8]
colums=['0','1','2','3','4','5','6','7','8']
pos=[]
ans=[]
for ind in index:
for col in colums:
if data[col][ind] ==0:
re=len_re(ind,col,data)
ans.append(list(re))
pos.append((ind,col))
return zip(pos,ans)
def result_min_len(data):
R=result()
c=R.result(data)
reslut_len=[]
reslut_list=[]
try:
for pos,ans in c:
reslut_list.append(ans)
reslut_len.append(len(ans))
return min(reslut_len)
except:
return False
class paichu():
def result(self,data):
data_copy=data.copy()
#如果一個位置出現多個結果,循序試錯,排除錯誤答案:
i=0
j=0
if result_min_len(data_copy)==0:
print('傳入數據錯誤,計算中止')
else:
result_=result()
result_1=result_.result(data_copy)
pos_=[]
ans_1_b=[]
for pos,ans_list in result_1:
ind=pos[0]
col=pos[1]
ans_1_a=[]
for ans in ans_list:
j+=1
data_copy[col][ind]=ans
f=result_min_len(data_copy)
if f:
i+=1
ans_1_a.append(ans)
data_copy[col][ind]=0
ans_1_b.append(ans_1_a)
pos_.append((ind,col))
print('已經排除{}個數'.format(j-i))
return zip(pos_,ans_1_b)
class rule_1():
def result(self,data):
#如果ind=0,按行,ind=1,按列排除
ind=0
result_000=result()
answer_zip=result_000.result(data)
#rule1 如果n個相同的list在同一行/列,且元素數量等于list的個數,則該行其他待定cell可以排除list中所有元素
index_0=[]
index_1=[]
index_2=[]
index_3=[]
index_4=[]
index_5=[]
index_6=[]
index_7=[]
index_8=[]
ans_0=[]
ans_1=[]
ans_2=[]
ans_3=[]
ans_4=[]
ans_5=[]
ans_6=[]
ans_7=[]
ans_8=[]
for pos,ans in answer_zip:
if pos[ind]==0:
index_0.append(pos)
ans_0.append(ans)
elif pos[ind]==1:
index_1.append(pos)
ans_1.append(ans)
elif pos[ind]==2:
index_2.append(pos)
ans_2.append(ans)
elif pos[ind]==3:
index_3.append(pos)
ans_3.append(ans)
elif pos[ind]==4:
index_4.append(pos)
ans_4.append(ans)
elif pos[ind]==5:
index_5.append(pos)
ans_5.append(ans)
elif pos[ind]==6:
index_6.append(pos)
ans_6.append(ans)
elif pos[ind]==7:
index_7.append(pos)
ans_7.append(ans)
elif pos[ind]==8:
index_8.append(pos)
ans_8.append(ans)
index=[index_0,index_1,index_2,index_3,index_4,index_5,index_6,index_7,index_8]
ans=[ans_0,ans_1,ans_2,ans_3,ans_4,ans_5,ans_6,ans_7,ans_8]
ans_=[]
for ans_array in ans:
l2=[]
for i in ans_array:
l2.append(set(i))
for i in np.unique(l2):
if l2.count(i)==len(i):
for k,j in enumerate(l2):
if i!=j:
j=j-i
l2[k]=j
ans_.append(list(l2))
pos_=[]
for i in index:
pos_=pos_+i
ans_list=[]
for i in ans_:
ans_list=ans_list+i
ans_listn=[]
for i in ans_list:
ans_listn.append(list(i))
return zip(pos_,ans_listn)
class rule_2():
def result(self,data):
#如果ind=0,按行,ind=1,按列排除
ind=1
result_000=result()
answer_zip=result_000.result(data)
#rule1 如果n個相同的list在同一行/列,且元素數量等于list的個數,則該行其他待定cell可以排除list中所有元素
index_0=[]
index_1=[]
index_2=[]
index_3=[]
index_4=[]
index_5=[]
index_6=[]
index_7=[]
index_8=[]
ans_0=[]
ans_1=[]
ans_2=[]
ans_3=[]
ans_4=[]
ans_5=[]
ans_6=[]
ans_7=[]
ans_8=[]
for pos,ans in answer_zip:
if pos[ind]=='0':
index_0.append(pos)
ans_0.append(ans)
elif pos[ind]=='1':
index_1.append(pos)
ans_1.append(ans)
elif pos[ind]=='2':
index_2.append(pos)
ans_2.append(ans)
elif pos[ind]=='3':
index_3.append(pos)
ans_3.append(ans)
elif pos[ind]=='4':
index_4.append(pos)
ans_4.append(ans)
elif pos[ind]=='5':
index_5.append(pos)
ans_5.append(ans)
elif pos[ind]=='6':
index_6.append(pos)
ans_6.append(ans)
elif pos[ind]=='7':
index_7.append(pos)
ans_7.append(ans)
elif pos[ind]=='8':
index_8.append(pos)
ans_8.append(ans)
index=[index_0,index_1,index_2,index_3,index_4,index_5,index_6,index_7,index_8]
ans=[ans_0,ans_1,ans_2,ans_3,ans_4,ans_5,ans_6,ans_7,ans_8]
ans_=[]
for ans_array in ans:
l2=[]
for i in ans_array:
l2.append(set(i))
for i in np.unique(l2):
if l2.count(i)==len(i):
for k,j in enumerate(l2):
if i!=j:
j=j-i
l2[k]=j
ans_.append(list(l2))
pos_=[]
for i in index:
pos_=pos_+i
ans_list=[]
for i in ans_:
ans_list=ans_list+i
ans_listn=[]
for i in ans_list:
ans_listn.append(list(i))
return zip(pos_,ans_listn)
class rule_3():
def result(self,data):
result_000=result()
answer_zip=result_000.result(data)
#rule3 如果n個相同的list在同一9宮格,且元素數量等于list的個數,則該行其他待定cell可以排除list中所有元素
block_0=[]
block_1=[]
block_2=[]
block_3=[]
block_4=[]
block_5=[]
block_6=[]
block_7=[]
block_8=[]
index_0=[]
index_1=[]
index_2=[]
index_3=[]
index_4=[]
index_5=[]
index_6=[]
index_7=[]
index_8=[]
for pos,ans in answer_zip:
index=pos[0]
colums=pos[1]
if index in [0,1,2]:
if colums in ['0','1','2']:
block_0.append(ans)
index_0.append(pos)
elif colums in ['3','4','5']:
block_1.append(ans)
index_1.append(pos)
else:
block_2.append(ans)
index_2.append(pos)
if index in [3,4,5]:
if colums in ['0','1','2']:
block_3.append(ans)
index_3.append(pos)
elif colums in ['3','4','5']:
block_4.append(ans)
index_4.append(pos)
else:
block_5.append(ans)
index_5.append(pos)
if index in [6,7,8]:
if colums in ['0','1','2']:
block_6.append(ans)
index_6.append(pos)
elif colums in ['3','4','5']:
block_7.append(ans)
index_7.append(pos)
else:
block_8.append(ans)
index_8.append(pos)
index=[index_0,index_1,index_2,index_3,index_4,index_5,index_6,index_7,index_8]
block=[block_0,block_1,block_2,block_3,block_4,block_5,block_6,block_7,block_8]
ans_=[]
for index_array in block:
l2=[]
for i in index_array:
l2.append(set(i))
for i in np.unique(l2):
if l2.count(i)==len(i):
for k,j in enumerate(l2):
if i!=j:
j=j-i
l2[k]=j
ans_.append(list(l2))
pos_=[]
for i in index:
pos_=pos_+i
ans_list=[]
for i in ans_:
ans_list=ans_list+i
ans_listn=[]
for i in ans_list:
ans_listn.append(list(i))
return zip(pos_,ans_listn)
class rule_():
def result(self,data):
Rule_1=rule_1()
Rule_2=rule_2()
Rule_3=rule_3()
result_dict_1={}
for pos,ans in Rule_1.result(data):
result_dict_1[pos]=set(ans)
result_dict_2={}
for pos,ans in Rule_2.result(data):
result_dict_2[pos]=set(ans)
result_dict_3={}
for pos,ans in Rule_3.result(data):
result_dict_3[pos]=set(ans)
#三本字典根據key值 取交集融合為最終結果
key_list=result_dict_1.keys()
result_dict={}
for key in key_list:
result_dict[key]=(result_dict_1[key])&(result_dict_2[key])&(result_dict_3[key])
ans=[]
pos=[]
for key,value in result_dict.items():
k=list(value)
k.sort()
ans.append(k)
pos.append(key)
return zip(pos,ans)
def fill_pinlv(result_zip,data):
#輸入數據類型為zip
result_zip=result_zip
result_list=list(result_zip)
#print(result_zip)
block_num_list=[]
for tup in result_list:
pos=tup[0]
ans=tup[1]
block_num=Block_num(pos[0],pos[1],data)
block_num_list.append(block_num)
#print(block_num_list)
ind_list_0=[]
ind_list_1=[]
ind_list_2=[]
ind_list_3=[]
ind_list_4=[]
ind_list_5=[]
ind_list_6=[]
ind_list_7=[]
ind_list_8=[]
ind_block_array=[]
for ind,num in enumerate(block_num_list):
if num==0:
ind_list_0.append(ind)
if num==1:
ind_list_1.append(ind)
if num==2:
ind_list_2.append(ind)
if num==3:
ind_list_3.append(ind)
if num==4:
ind_list_4.append(ind)
if num==5:
ind_list_5.append(ind)
if num==6:
ind_list_6.append(ind)
if num==7:
ind_list_7.append(ind)
if num==8:
ind_list_8.append(ind)
ind_block_array=[ind_list_0,ind_list_1,ind_list_2,ind_list_3,ind_list_4,ind_list_5,ind_list_6,ind_list_7,ind_list_8]
#print(ind_block_array)
result_block_array=[]
for ind_list in ind_block_array:
result_block_list=[]
for ind in ind_list:
result_block_list.append(result_list[ind])
result_block_array.append(result_block_list)
#print(result_block_array)
#生成block_counter_array
block_counter_array=[]
for block_list in result_block_array:
counter_list=[]
for i in range(1,10):
counter=0
for pos,ans in block_list:
if i in ans:
counter+=1
counter_list.append(counter)
block_counter_array.append(counter_list)
#print(block_counter_array)
#更新數據data
jishuqi=0
for block_index,block in enumerate(block_counter_array):
processing_num=0
processing_num_pos=False
for num,count in enumerate(block):
if count==1:
processing_num=num+1
for tup in result_list:
pos=tup[0]
ans=tup[1]
if block_index==Block_num(pos[0],pos[1],data):
#print(block_index)
if processing_num in ans:
#print(pos)
processing_num_pos=pos
jishuqi+=1
data[processing_num_pos[1]][processing_num_pos[0]]=processing_num
if jianyan(data)==False:
data[processing_num_pos[1]][processing_num_pos[0]]=0
return jishuqi
return jishuqi
def fill_jilian(data):
jishuqi=0
for i in range(1,10):
index_list=[]
col_list=[]
block_dataframe=pd.DataFrame()
for block_num in range(9):
#print(block_num)
block_dataframe=Block_dataframe(block_num,data)
index_list=list(block_dataframe.index)
col_list=list(block_dataframe.columns)
if i not in block_dataframe.values:
#排除行 index
for ind in index_list:
for col in range(9):
if i==data.iloc[ind][col]:
index_list.remove(ind)
#排除列 col
for col in col_list:
for ind in range(9):
if i ==data[col][ind]:
col_list.remove(col)
#排除有數的cell
pos_list=[]
for ind in index_list:
for col in col_list:
pos=(ind,col)
pos_list.append(pos)
for pos in pos_list:
if data[pos[1]][pos[0]]!=0:
pos_list.remove(pos)
#print(pos_list)
#填入數字
if len(pos_list)==1:
pos=pos_list[0]
data[pos[1]][pos[0]]=i
jishuqi+=1
#print('已經填入{}個數字'.format(jishuqi))
return jishuqi
def fill(data,classname):
#print('執行中。。。')
data=data
if classname =='result':
cla=result()
elif classname=='paichu':
cla=paichu()
elif classname=='rule':
cla=rule()
#如果位置上只有一種可能,就直接填入data
i=0
m=0
flag_1=True
while(flag_1):
if result_min_len(data)==0:
#print('上一輪輸入的數據有誤')
flag_1=False
m=i
for pos,ans in cla.result(data):
#print(len(ans))
if len(ans)==1:
i+=1
ind=pos[0]
col=pos[1]
data[col][ind]=ans[0]
if jianyan(data)==False:
#print('填入數據出錯,已停止填寫')
flag_1=False
break
if m == i:
break
#如果位置上只有多種可能,就使用fill_jilian()填入data
j=0
m=0
flag_2=True
while(flag_2):
if result_min_len(data)==0:
#print('上一輪輸入的數據有誤')
flag_2=False
if jianyan(data)==False:
#print('填入數據出錯,已停止填寫')
flag_2=False
m=j
j=j+fill_pinlv(cla.result(data),data)
if m == j:
break
def labelinit(data):
R=rule()
label_list=[]
for pos,ans in R.result(data):
label_list.append([pos,ans,[None]*len(ans)])
return label_list
def label_change_B(pos,ans,label_list):
#label_list_copy=label_list.copy()
for l in label_list:
if l[0]==pos:
for a in range(len(l[2])):
l[2][a]=False
if ans == l[1][a]:
l[2][a]=True
return
def label_change_A(pos,ans,label_list):
#label_list_copy=label_list.copy()
for l in label_list:
if l[0]==pos:
for a in range(len(l[2])):
l[2][a]=False
if ans == l[1][a]:
l[2][a]=False
return
def label_rechange(data,label_list):
try:
#更新label_list
for ind,l in enumerate(label_list):
if (None not in l[2]) and (True not in l[2]):
#清空最后一行
process_1=[None]*len(l[1])
#上一行True后移一個單位
process_2=label_list[ind-1][2]
index=process_2.index(True)+1
process_2.pop()
process_2.insert(0,False)
label_list[ind][2]=process_1
label_list[ind-1][2]=process_2
return True
except:
print('label_rechange 出錯')
print('請確認輸入數據正確!!!')
return False
def data_change(data,label_list):
#根據label_list更新數據data
try:
for l in label_list:
if True in l[2]:
pos=l[0]
ans=l[1]
marker=l[2]
data[pos[1]][pos[0]]=ans[marker.index(True)]
else:
print('data 按照label假設完成')
break
except:
print('data_change 出錯')
def AB_test(data_copy):
flag=None
#AB判斷(3種方向,1成功,2返回,3繼續,)
flag_1=jianyan(data_copy)
flag_2=full(data_copy)
flag_3=result_min_len(data)
if flag_1==True:
if flag_2==True:
#1成功
print('C:成功找到結果')
print(data_copy)
flag='C'
if flag_2==False:
if flag_3==0:
#2返回
print('A:遍歷下一個ans')
flag='A'
#label_change(pos,ans,result_label_list)
if flag_3>0:
#3繼續
print('B:進入下一位置')
flag='B'
#label_change(pos,ans,result_label_list)
if flag_1==False:
#2返回
print('A:遍歷下一個ans')
flag='A'
#label_change(pos,ans,result_label_list)
return flag
def jiashe(data,label_list_array,lunci):
data_copy=data.copy()
R=result()
ans_list=label_list_array[lunci][1]
pos=label_list_array[lunci][0]
for ans in ans_list:
#假設
data_copy[pos[1]][pos[0]]=ans
#推導
fill(data_copy,'result')
fill(data_copy,'rule_')
fill(data_copy,'rule_')
label=AB_test(data_copy)
if label=='B':
#假設下一位置 重啟假設函數
label_change_B(pos,ans,label_list_array)
return 'B'
elif label=='A':
#假設下一ans 遍歷下一個ans
label_change_A(pos,ans,label_list_array)
elif label=='C':
#print('success')
return data_copy
def jie(data,label_list_array):
ind=0
for i in range(10):
f_1=jiashe(data,label_list_array,ind)
if type(f_1)==pd.core.frame.DataFrame:
return f_1
f_2=label_rechange(data,label_list_array)
data_change(data,label_list_array)
ind+=1
if f_2:
ind-=1
print('B:返回上一位置')
if f_2==False:
return False
#print(label_list_array[0:5])
def main(data):
datacopy=data.copy()
fill(data,'result')
fill(data,'rule')
label_list_array=label_init(data)
shudu_jie=jie(data,label_list_array)
print('原始數據')
print(data_copy)
print('結果是')
print(shudu_jie)
main(data)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。