简单的数独游戏求解程序(matlab)
数独问题-数学建模

数独问题摘要本文是对数独问题进行求解。
结合数独生成的特点,立足于题中数独建模和WNF P函数和整数规划模型。
求解的要求,建立了数独难度分析()对于问题一,首先研究数独难度的影响因素,通过综合分析数独的特点结构,WNF P可以在常数时间内计算出来以衡量数独的难易程度。
通过计算可知得出()()0.04531WNF P=,根据数独难度的划分得到如下结论:数独难度系数为4,达到了极难的程度。
对于问题二,我们通过对此数独的分析和讨论,利用穷举法,通过matlab 软件编程求解,最终得出答案,如表1所示。
对于问题三,我们利用回溯法思想,建立求解模型,具体算法一般采用如下步骤:1).在此数独初盘选择一个空单元格;2).取这个单元格中一个可能的候选数;3).将这个候选数填入单元格中,迭代完成数独;4).若这个候选数推导得到一个无效数独终盘,返回此单元格取其他候选数;对于问题四采用整数规划模型,采用三维0-1 变量的方法,运用lingo软件编程求解。
最终得到答案,如表1所示。
关键词:数独数独难度分析穷举法回溯法整体规划1问题的重述前段时间芬兰一位数学家号称设计出全球最难的“数独游戏”,并刊登在报纸上,让大家去挑战。
该数独如下图所示:数独是根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行、每一列、每一个粗线宫内的数字均含1-9,且不重复。
每一道合格的数独谜题都有且仅有唯一答案,推理方法也以此为基础,任何无解或多解的题目都是不合格的。
根据以上描述,试完成以下问题:1. 分析此数独的难度;2. 用穷举算法求解数独;3. 设计此数独求解的较优的算法;4. 建立数独求解模型并给出此数独的答案。
2模型的基本假设1该数独问题存在唯一解。
3符号说明X表示空单元格候选数?()X的加权函数W n表示候选数数?()c X表示数独空单元格中的候选数数目函数nE p表示该数独的空格处()()WNF P表示该数独难度的函数x表示数k是否填入数独方中的(i,j)处ijkc表示往空格处填入0后数独方中(i,j)处的数ijy表示经过求解后数独方中(i,j)处的数ij4模型的建立与求解4.1 问题14.1.1数独难度的影响因素通过对数独的分析与研究,数独难度与数独候选数、逻辑推理方法、搜索步数、空格数以及空格的分布情况都有密切的关系。
数独游戏程序

%数独游戏程序,可以破解绝大部分数独题。
%用法:将所有文件复制到MATLAB工作目录下,或是将MATLAB工作目录修改为本文件夹所在路径。
%A是一个初始的99的方阵,空白处用0代替,调用TT=shudu(A),则生成结果。
function TT=fill1(A)T=A;i=1;n1=0;n2=0;flag1=judge1(T);while i<10n1=0;n2=0;for j=1:9if T(i,j)==0&i~=flag1(:,1)&j~=flag1(:,2)&iden(i,j,1,T)==0n1=n1+1;endendif n1==1for j=1:9if T(i,j)==0&i~=flag1(:,1)&j~=flag1(:,2)&iden(i,j,1,T)==0T(i,j)=1;flag1=judge1(T);endendendfor j=1:9if T(j,i)==0&j~=flag1(:,1)&i~=flag1(:,2)&iden(j,i,1,T)==0n2=n2+1;endendif n2==1for j=1:9if T(j,i)==0&j~=flag1(:,1)&i~=flag1(:,2)&iden(j,i,1,T)==0T(j,i)=1;flag1=judge1(T);endendendif n1==1|n2==1i=0;endi=i+1;endTT=T;function TT=fill2(A)T=A;i=1;n1=0;n2=0;flag2=judge2(T);while i<10n1=0;n2=0;for j=1:9if T(i,j)==0&i~=flag2(:,1)&j~=flag2(:,2)&iden(i,j,2,T)==0n1=n1+1;endendif n1==1for j=1:9if T(i,j)==0&i~=flag2(:,1)&j~=flag2(:,2)&iden(i,j,2,T)==0T(i,j)=2;flag2=judge2(T);endendendfor j=1:9if T(j,i)==0&j~=flag2(:,1)&i~=flag2(:,2)&iden(j,i,2,T)==0n2=n2+1;endendif n2==1for j=1:9if T(j,i)==0&j~=flag2(:,1)&i~=flag2(:,2)&iden(j,i,2,T)==0T(j,i)=2;flag2=judge2(T);endendendif n1==1|n2==1i=0;endi=i+1;endTT=T;function TT=fill3(A)T=A;i=1;n1=0;n2=0;flag3=judge3(T);while i<10n1=0;n2=0;for j=1:9if T(i,j)==0&i~=flag3(:,1)&j~=flag3(:,2)&iden(i,j,3,T)==0n1=n1+1;endendif n1==1for j=1:9if T(i,j)==0&i~=flag3(:,1)&j~=flag3(:,2)&iden(i,j,3,T)==0T(i,j)=3;flag3=judge3(T);endendendfor j=1:9if T(j,i)==0&j~=flag3(:,1)&i~=flag3(:,2)&iden(j,i,3,T)==0n2=n2+1;endendif n2==1for j=1:9if T(j,i)==0&j~=flag3(:,1)&i~=flag3(:,2)&iden(j,i,3,T)==0T(j,i)=3;flag3=judge3(T);endendendif n1==1|n2==1i=0;endi=i+1;endTT=T;function TT=fill4(A)T=A;i=1;n1=0;n2=0;flag4=judge4(T);while i<10n1=0;n2=0;for j=1:9if T(i,j)==0&i~=flag4(:,1)&j~=flag4(:,2)&iden(i,j,4,T)==0n1=n1+1;endendif n1==1for j=1:9if T(i,j)==0&i~=flag4(:,1)&j~=flag4(:,2)&iden(i,j,4,T)==0T(i,j)=4;flag4=judge4(T);endendendfor j=1:9if T(j,i)==0&j~=flag4(:,1)&i~=flag4(:,2)&iden(j,i,4,T)==0n2=n2+1;endendif n2==1for j=1:9if T(j,i)==0&j~=flag4(:,1)&i~=flag4(:,2)&iden(j,i,4,T)==0T(j,i)=4;flag4=judge4(T);endendendif n1==1|n2==1i=0;endi=i+1;endTT=T;function TT=fill5(A)T=A;i=1;n1=0;n2=0;flag5=judge5(T);while i<10n1=0;n2=0;for j=1:9if T(i,j)==0&i~=flag5(:,1)&j~=flag5(:,2)&iden(i,j,5,T)==0n1=n1+1;endendif n1==1for j=1:9if T(i,j)==0&i~=flag5(:,1)&j~=flag5(:,2)&iden(i,j,5,T)==0T(i,j)=5;flag5=judge5(T);endendendfor j=1:9if T(j,i)==0&j~=flag5(:,1)&i~=flag5(:,2)&iden(j,i,5,T)==0n2=n2+1;endendif n2==1for j=1:9if T(j,i)==0&j~=flag5(:,1)&i~=flag5(:,2)&iden(j,i,5,T)==0T(j,i)=5;flag5=judge5(T);endendendif n1==1|n2==1i=0;endi=i+1;endTT=T;function TT=fill6(A)T=A;i=1;n1=0;n2=0;flag6=judge6(T);while i<10n1=0;n2=0;for j=1:9if T(i,j)==0&i~=flag6(:,1)&j~=flag6(:,2)&iden(i,j,6,T)==0n1=n1+1;endendif n1==1for j=1:9if T(i,j)==0&i~=flag6(:,1)&j~=flag6(:,2)&iden(i,j,6,T)==0T(i,j)=6;flag6=judge6(T);endendendfor j=1:9if T(j,i)==0&j~=flag6(:,1)&i~=flag6(:,2)&iden(j,i,6,T)==0n2=n2+1;endendif n2==1for j=1:9if T(j,i)==0&j~=flag6(:,1)&i~=flag6(:,2)&iden(j,i,6,T)==0T(j,i)=6;flag6=judge6(T);endendendif n1==1|n2==1i=0;endi=i+1;endTT=T;function TT=fill7(A)T=A;i=1;n1=0;n2=0;flag7=judge7(T);while i<10n1=0;n2=0;for j=1:9if T(i,j)==0&i~=flag7(:,1)&j~=flag7(:,2)&iden(i,j,7,T)==0n1=n1+1;endendif n1==1for j=1:9if T(i,j)==0&i~=flag7(:,1)&j~=flag7(:,2)&iden(i,j,7,T)==0T(i,j)=7;flag7=judge7(T);endendendfor j=1:9if T(j,i)==0&j~=flag7(:,1)&i~=flag7(:,2)&iden(j,i,7,T)==0n2=n2+1;endendif n2==1for j=1:9if T(j,i)==0&j~=flag7(:,1)&i~=flag7(:,2)&iden(j,i,7,T)==0T(j,i)=7;flag7=judge7(T);endendendif n1==1|n2==1i=0;endi=i+1;endTT=T;function TT=fill8(A)T=A;i=1;n1=0;n2=0;flag8=judge8(T);while i<10n1=0;n2=0;for j=1:9if T(i,j)==0&i~=flag8(:,1)&j~=flag8(:,2)&iden(i,j,8,T)==0n1=n1+1;endendif n1==1for j=1:9if T(i,j)==0&i~=flag8(:,1)&j~=flag8(:,2)&iden(i,j,8,T)==0T(i,j)=8;flag8=judge8(T);endendendfor j=1:9if T(j,i)==0&j~=flag8(:,1)&i~=flag8(:,2)&iden(j,i,8,T)==0n2=n2+1;endendif n2==1for j=1:9if T(j,i)==0&j~=flag8(:,1)&i~=flag8(:,2)&iden(j,i,8,T)==0T(j,i)=8;flag8=judge8(T);endendendif n1==1|n2==1i=0;endi=i+1;endTT=T;function TT=fill9(A)T=A;i=1;n1=0;n2=0;flag9=judge9(T);while i<10n1=0;n2=0;for j=1:9if T(i,j)==0&i~=flag9(:,1)&j~=flag9(:,2)&iden(i,j,9,T)==0n1=n1+1;endendif n1==1for j=1:9if T(i,j)==0&i~=flag9(:,1)&j~=flag9(:,2)&iden(i,j,9,T)==0T(i,j)=9;flag9=judge9(T);endendendfor j=1:9if T(j,i)==0&j~=flag9(:,1)&i~=flag9(:,2)&iden(j,i,9,T)==0n2=n2+1;endendif n2==1for j=1:9if T(j,i)==0&j~=flag9(:,1)&i~=flag9(:,2)&iden(j,i,9,T)==0T(j,i)=9;flag9=judge9(T);endendendif n1==1|n2==1i=0;endi=i+1;endTT=T;function pp=iden(x,y,num,T)f=0;for k=0:2for m=0:2if x>=1+3*k&x<=3*(k+1)&y>=1+3*m&y<=3*(m+1) for i=1:3for j=1:3if T(i+3*k,j+3*m)==numf=1;endendendendendendpp=f;function flag1=judge1(A)T=A;k=1;for i=1:9for j=1:9if T(i,j)==1flag1(k,:)=[i,j];k=k+1;endendendfunction flag2=judge2(A)T=A;k=1;for i=1:9for j=1:9if T(i,j)==2flag2(k,:)=[i,j];k=k+1;endendendfunction flag3=judge3(A) T=A;k=1;for i=1:9for j=1:9if T(i,j)==3flag3(k,:)=[i,j];k=k+1;endendendfunction flag4=judge4(A) T=A;k=1;for i=1:9for j=1:9if T(i,j)==4flag4(k,:)=[i,j];k=k+1;endendendfunction flag5=judge5(A) T=A;k=1;for i=1:9for j=1:9if T(i,j)==5flag5(k,:)=[i,j];k=k+1;endendendfunction flag6=judge6(A) T=A;k=1;for i=1:9for j=1:9if T(i,j)==6flag6(k,:)=[i,j];k=k+1;endendendfunction flag7=judge7(A) T=A;k=1;for i=1:9for j=1:9if T(i,j)==7flag7(k,:)=[i,j];k=k+1;endendendfunction flag8=judge8(A) T=A;k=1;for i=1:9for j=1:9if T(i,j)==8flag8(k,:)=[i,j];k=k+1;endendendfunction flag9=judge9(A) T=A;k=1;for i=1:9for j=1:9if T(i,j)==9flag9(k,:)=[i,j];k=k+1;endendendfunction TT=shudu(A)T=A;f=1;n=0;while f==1T=fill1(T);T=fill2(T);T=fill3(T);T=fill4(T);T=fill5(T);T=fill6(T);T=fill7(T);T=fill8(T);T=fill9(T);t1=size(judge1(T));t2=size(judge2(T));t3=size(judge3(T));t4=size(judge4(T));t5=size(judge5(T));t6=size(judge6(T));t7=size(judge7(T));t8=size(judge8(T));t9=size(judge9(T));ift1(1)==9&t2(1)==9&t3(1)==9&t4(1)==9&t5(1)==9&t6(1)==9&t7(1)==9&t8(1)==9&t9(1)==9|n==1 00f=0;endn=n+1;endTT=T;。
Matlab实现的简易计算器程序代码

function varargout = jisuanqi(varargin)% JISUANQI M-file for jisuanqi.fig% JISUANQI, by itself, creates a new JISUANQI or raises the existing % singleton*.%% H = JISUANQI returns the handle to a new JISUANQI or the handle to % the existing singleton*.%% JISUANQI('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in JISUANQI.M with the given input arguments. %% JISUANQI('Property','Value',...) creates a new JISUANQI or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before jisuanqi_OpeningFcn gets called. An% unrecognized property name or invalid value makes property application % stop. All inputs are passed to jisuanqi_OpeningFcn via varargin. %% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)".%% See also: GUIDE, GUIDATA, GUIHANDLES% Edit the above text to modify the response to help jisuanqi% Last Modified by GUIDE v2.5 20-Jul-2011 09:45:20% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @jisuanqi_OpeningFcn, ...'gui_OutputFcn', @jisuanqi_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []);if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});elsegui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT% --- Executes just before jisuanqi is made visible.function jisuanqi_OpeningFcn(hObject, eventdata, handles, varargin) handles.current_str='';%´ËΪ´æ´¢µ±Ç°µÄ×Ö·û´®handles.L1_str='';handles.L2_str='';%´ËΪ´æ´¢ÉÏÒ»¸öÊý×ÖµÄ×Ö·û´®% This function has no output args, see OutputFcn.% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% varargin command line arguments to jisuanqi (see VARARGIN)% Choose default command line output for jisuanqihandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes jisuanqi wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line. function varargout = jisuanqi_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT);% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Get default command line output from handles structurevarargout{1} = handles.output;% --- Executes on button press in Number_0.function Number_0_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ0£¬Èç¹û°´ÁË0¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'0');handles.L1_str=strcat(handles.L1_str,'0');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_0 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --- Executes on button press in xiaoshudian.function xiaoshudian_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'.');guidata(hObject, handles);%СÊýµã% hObject handle to xiaoshudian (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --- Executes on button press in jia_jian.function jia_jian_Callback(hObject, eventdata, handles)% hObject handle to jia_jian (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Number_1.function Number_1_Callback(hObject, eventdata, handles)% hObject handle to Number_1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ1£¬Èç¹û°´ÁË1¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'1');handles.L1_str=strcat(handles.L1_str,'1');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% --- Executes on button press in Number_2.function Number_2_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ2£¬Èç¹û°´ÁË1¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'2');handles.L1_str=strcat(handles.L1_str,'2');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Number_3.function Number_3_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ3£¬Èç¹û°´ÁË3¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'3');handles.L1_str=strcat(handles.L1_str,'3');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_3 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Number_4.function Number_4_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ4£¬Èç¹û°´ÁË4¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'4');handles.L1_str=strcat(handles.L1_str,'4');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_4 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Number_5.function Number_5_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ5£¬Èç¹û°´ÁË5¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'5');handles.L1_str=strcat(handles.L1_str,'5');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_5 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Number_6.function Number_6_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ6£¬Èç¹û°´ÁË6¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'6');handles.L1_str=strcat(handles.L1_str,'6');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_6 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Number_7.function Number_7_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ7£¬Èç¹û°´ÁË7¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'7');handles.L1_str=strcat(handles.L1_str,'7');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_7 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Number_8.function Number_8_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ8£¬Èç¹û°´ÁË8¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'8');handles.L1_str=strcat(handles.L1_str,'8');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_8 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Number_9.function Number_9_Callback(hObject, eventdata, handles)%Á¬½Óµ±Ç°µÄ×Ö·û´®ºÍ9£¬Èç¹û°´ÁË9¼üµÄ»°¡£handles.current_str=strcat(handles.current_str,'9');handles.L1_str=strcat(handles.L1_str,'9');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Number_9 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in jiahao.function jiahao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'+');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to jiahao (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in denghao.function denghao_Callback(hObject, eventdata, handles)st = get(handles.edit1,'String');%¼ÆËã½á¹û£¬²¢´æ·Åµ½µÚ¶þ¸ö±à¼-¿òÖС£val = eval(st);s = num2str(val);set(handles.edit2,'String',s);guidata(hObject, handles);% hObject handle to denghao (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in jianhao.function jianhao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'-');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to jianhao (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in X_daoshu.function X_daoshu_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'\1');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to X_daoshu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in chenghao.function chenghao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'*');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to chenghao (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in baifenhao.function baifenhao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'/100');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);handles.L2_str=handles.L1_str;handles.L1_str = '';% hObject handle to baifenhao (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in chuhao.function chuhao_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'/');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to chuhao (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in Sqrt.function Sqrt_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'sqrt');handles.L1_str=strcat(handles.L1_str,'sqrt');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to Sqrt (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in X_lifang.function X_lifang_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'^3');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to X_lifang (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in x_pingfang.function x_pingfang_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'^2');set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to x_pingfang (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in zhengqie.function zhengqie_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'tan');handles.L1_str=strcat(handles.L1_str,'tan');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to zhengqie (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in zhengxian.function zhengxian_Callback(hObject, eventdata, handles)handles.current_str=strcat(handles.current_str,'sin');handles.L1_str=strcat(handles.L1_str,'sin');%ÔÚÊä³öµ±Ç°µÄ×Ö·û´®set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);% hObject handle to zhengxian (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in CE.function CE_Callback(hObject, eventdata, handles)% hObject handle to CE (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in C.function C_Callback(hObject, eventdata, handles)% hObject handle to C (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) handles.current_str = '';set(handles.edit1,'String','0.');set(handles.edit2,'String','0.');guidata(hObject, handles);% --- Executes on button press in Backspace.function Backspace_Callback(hObject, eventdata, handles)% hObject handle to Backspace (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)s1 = handles.current_str;handles.current_str = s1(1:length(s1)-1);set(handles.edit1,'String',handles.current_str);guidata(hObject, handles);function edit1_Callback(hObject, eventdata, handles)% hObject handle to edit1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit1 as text% str2double(get(hObject,'String')) returns contents of edit1 as a double% --- Executes during object creation, after setting all properties. function edit1_CreateFcn(hObject, eventdata, handles)% hObject handle to edit1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endif ispcset(hObject,'BackgroundColor','white');elseset(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')) ;end% --- Executes on button press in shujujiazai.function shujujiazai_Callback(hObject, eventdata, handles)% hObject handle to shujujiazai (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global YSData;[filename,pathname,FILTERINDEX]=uigetfile({'*.xls';'*.dat';'*.*'},'Ñ¡ÔñÊý¾ÝÎļþ');if(FILTERINDEX==0)return;endstr_filename=[pathname,filename];fid=fopen(str_filename,'rt');if(fid==-1)errordlg('Open file error!','Open error');return;endYSData=xlsread(str_filename);set(handles.lujingxianshi,'String',str_filename);set(handles.tuxingxianshi,'Enable','on');function lujingxianshi_Callback(hObject, eventdata, handles)% hObject handle to lujingxianshi (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(handles.lujingxianshi,'String',pathname);% Hints: get(hObject,'String') returns contents of lujingxianshi as text % str2double(get(hObject,'String')) returns contents of lujingxianshi as a double% --- Executes during object creation, after setting all properties. function lujingxianshi_CreateFcn(hObject, eventdata, handles)% hObject handle to lujingxianshi (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');end% --- Executes on button press in zhuxingtu.function zhuxingtu_Callback(hObject, eventdata, handles)% hObject handle to zhuxingtu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(handles.zhuxingtu,'Value',1);set(handles.zhexiantu,'Value',0);set(handles.yuanbingtu,'Value',0);% Hint: get(hObject,'Value') returns toggle state of zhuxingtu% --- Executes on button press in zhexiantu.function zhexiantu_Callback(hObject, eventdata, handles)% hObject handle to zhexiantu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(handles.zhuxingtu,'Value',0);set(handles.zhexiantu,'Value',1);set(handles.yuanbingtu,'Value',0);% Hint: get(hObject,'Value') returns toggle state of zhexiantu% --- Executes on button press in yuanbingtu.function yuanbingtu_Callback(hObject, eventdata, handles)% hObject handle to yuanbingtu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(handles.zhuxingtu,'Value',0);set(handles.zhexiantu,'Value',0);set(handles.yuanbingtu,'Value',1);% Hint: get(hObject,'Value') returns toggle state of yuanbingtu% --- Executes on button press in tuxingxianshi.function tuxingxianshi_Callback(hObject, eventdata, handles)global YSData;%Åжϡ®Í¼ÏñÀàÐÍ¡¯ÏÂ×éºÏ¿òÖеĸ÷µ¥Ñ¡°´Å¥µÄÑ¡ÖÐÇé¿ö£¬²¢´æ´¢ÔÚIndex_rad iobuttonÖÐIndex_radiobutton=get([handles.zhuxingtu,handles.zhexiantu,handles.yuan bingtu],'Value');%½øÐÐͼÐÎÉú³Éif(Index_radiobutton{1}==1)bar(YSData);title('¸÷Êý¾ÝÖ±·½Í¼');endif(Index_radiobutton{2}==1)plot(YSData);title('¸÷Êý¾ÝÕÛÏßͼ');endif(Index_radiobutton{3}==1)pie(YSData);title('¸÷Êý¾ÝÔ²±ýͼ');end% hObject handle to tuxingxianshi (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% global YSData;% --- Executes during object creation, after setting all properties. function tuxingxianshi_CreateFcn(hObject, eventdata, handles)% hObject handle to tuxingxianshi (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called function edit2_Callback(hObject, eventdata, handles)% hObject handle to edit2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit2 as text% str2double(get(hObject,'String')) returns contents of edit2 as a double% --- Executes during object creation, after setting all properties. function edit2_CreateFcn(hObject, eventdata, handles)% hObject handle to edit2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');end% --- Executes on button press in Quit.function Quit_Callback(hObject, eventdata, handles)% hObject handle to Quit (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)close(gcf);% --- Executes during object deletion, before destroying properties. function zhengxian_DeleteFcn(hObject, eventdata, handles)% hObject handle to zhengxian (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)function edit3_Callback(hObject, eventdata, handles)% hObject handle to edit3 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit3 as text% str2double(get(hObject,'String')) returns contents of edit3 as a double% --- Executes during object creation, after setting all properties. function edit3_CreateFcn(hObject, eventdata, handles)% hObject handle to edit3 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');end。
matlab程序算例

matlab程序算例Matlab程序算例Matlab是一种广泛应用于科学和工程领域的高级计算机编程语言及环境。
它的简洁、高效和强大的功能使得许多人选择使用Matlab来解决复杂的数学和工程问题。
在本文中,我将以一个具体的Matlab程序算例为例,详细说明每一步是如何完成的。
那么我们首先来看一下这个具体的Matlab程序算例。
假设我们希望计算并绘制一个二维正弦函数,代码如下:matlab设置步长,定义x轴的范围dx = 0.1;x = 0:dx:10;计算对应的y值y = sin(x);绘制图像plot(x, y);在这个例子中,我们通过定义一个步长`dx`和一个x轴的范围`x`来生成一系列的x值。
然后,我们使用`sin()`函数计算对应的y值,并将结果保存在`y`中。
最后,我们使用`plot()`函数绘制x和y的图像。
现在,让我们一步一步来回答这个程序算例中的问题。
第一步:设置步长和定义x轴的范围。
matlabdx = 0.1;x = 0:dx:10;这里我们设置步长`dx`为0.1,表示x轴上两个相邻点之间的间距为0.1。
然后,我们使用冒号运算符`:`创建一个从0到10的向量`x`,其中每个元素之间的间隔为`dx`。
也就是说,`x`中的元素为0, 0.1, 0.2, …, 9.9, 10。
第二步:计算对应的y值。
matlaby = sin(x);这里,我们使用`sin()`函数计算每个x值对应的正弦值,并将结果保存在`y`中。
例如,如果x的第一个元素为0,则使用`sin(0)`计算得到y的第一个元素的值。
第三步:绘制图像。
matlabplot(x, y);最后,我们使用`plot()`函数将x和y的值绘制成图像。
这样就可以观察到x和y之间的关系。
在这个例子中,由于x的范围是从0到10,并且y是对应的正弦值,因此我们将得到一个周期为2π的正弦函数的图像。
以上就是这个Matlab程序算例的每一步的解释。
数独求解程序

求解数独Fortran程序!******************************************************************* !编写:飞行棋编写时间:2013年儿童节!程序说明:!主程序采用回溯法,先按顺序将每个未知数的位置坐标存入x、y数组中,!共有step个未知数。
每个位置从1开始试验,若满足条件则t=t+1,试验!下一个未知数;若1~9均不能满足条件,则回溯到上一个未知数t=t-1,!将试验值+1,再进行循环。
当循环到最后一个未知数时,若满足条件则输!出解;若回溯到第一个未知数时,填入1~9均不能满足条件,则数独无解。
!子程序用来判断试验值是否满足条件。
!m,n=未知数在校宫格中的位置,step=未知数个数,t=试验值在未知数中!的次序,A=存放数独值的数组,x,y=存放未知数坐标的数组!******************************************************************** program sudokuimplicit noneinteger i,j,m,n,k,step,tinteger A(1:9,1:9),x(81),y(81)logical flagt=0open(8,file='soku.txt')do i=1,9 !读入数独初值,未知数赋0do j=1,9read(8,'(i2)') A(i,j)if(A(i,j)==0)thent=t+1step=t !计算未知数个数x(t)=iy(t)=jendifenddoenddoclose(8)print '(2x,9i2)',((A(i,j),j=1,9),i=1,9)t=1print*,stepdo while(t<=step)i=x(t)j=y(t)if(A(i,j)==9)then !若回溯到的上一个未知数值为9,则继续往前回溯 t=t-2A(i,j)=0goto 200endifdo k=A(i,j)+1,9A(i,j)=kcall judge(i,j,k,flag,A) !判断是否满足条件,若满足,退出循环,试验下一个未知数if(flag.eqv..true.)thenexitendifif(k==9.and.(flag.eqv..false.))then !若1~9均不能满足条件,回溯上一个未知数t=t-2A(i,j)=0endifenddo200 t=t+1if(t==step+1)thenprint*,'本数独解为:'exitendifif(t<=0.and.(flag.eqv..false.))thenprint*,'本数独无解,再检查一下题目吧!'exitendifenddoprint '(2x,9i2)',((A(i,j),j=1,9),i=1,9)10 format(1x,3I3)End子程序:subroutine judge(vi,vj,k,flag,A)implicit noneinteger i,j,vi,vj,k,m,nlogical flaginteger A(1:9,1:9)flag=.true.m=mod(vi,3)+3*int(1-mod(vi,3)/3.)n=mod(vj,3)+3*int(1-mod(vj,3)/3.)do i=1,9 !判断行条件if(A(i,vj)==k.and.i/=vi)thenflag=.false.endifenddodo j=1,9 !判断列条件if(A(vi,j)==k.and.j/=vj)thenflag=.false.endifenddodo i=vi-m+1,vi-m+3 !判断小宫格条件 do j=vj-n+1,vj-n+3if((i/=vi.or.j/=vj).and.A(i,j)==k)thenflag=.false.end ifenddoenddoend输入数据格式示例:8428518390 0 7 0 4 0 5 0 0 0 8 2 0 0 5 0 0 0 4 0 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 01每行一个数据,共82行。
数字数独游戏完整源代码程序

outtextxy(120, 99, _T("求解中......"));
if (SecondCheck() == true) // 第二次检查,防止不必要的死循环
{
trace(0, 0); // 溯回法判断
bool Point[3][3];
int main()
{
initgraph(WIDTH, HEIGHT);
drawframe(); // 绘制表格框架
while (true)
{
while (true)
{
int m_x, m_y, number;
void Output(); // 输出答案
int MouseNumber = 0;
int sum = 0; // 数独多解的记录
int trace(int x, int y); // 算法的核心回溯法
int check(int x, int y); // 每次判断
outtextxy(120, 99, _T("该数独无解!"));
continue;
}
if (sum == 1)
{
setbkmode(OPAQUE);
settextcolor(0xf4b1a4);
settextstyle(20, 0, _T("楷体"));
break;
}
save(number, m_x, m_y);
if (FirstCheck() == false)
{
settextstyle(20, 0, _T("楷体"));
求解数独的所有解法

求解数独的所有解法数独是⼀种考验眼⼒和逻辑的⼩游戏,关键在这个“独”字上,横竖不能重复,⽅块不能重复。
今天我给⼤家介绍⼀种利⽤“循环+递归+回溯”的办法来⽤Java程序替我们完成数独。
先给代码随后讲解:1import java.util.HashMap;2import java.util.Map;34public class T2 {5public static final int N=3;6public static void main(String[] args) {7int x[][]={8 {0,2,0,0,0,9,0,1,0,0},9 {5,0,6,0,0,0,3,0,9,0},10 {0,8,0,5,0,2,0,6,0,0},11 {0,0,5,0,7,0,1,0,0,0},12 {0,0,0,2,0,8,0,0,0,0},13 {0,0,4,0,1,0,8,0,0,0},14 {0,5,0,8,0,7,0,3,0,0},15 {7,0,2,3,0,0,4,0,5,0},16 {0,4,0,0,0,0,0,7,0,0},17 };1819 function(x,0,0);2021 }22 23private static void function(int[][] x, int r, int c) {24if (r>=x.length) {25 show(x);26return;27 }28if (c==0&&(r==x.length/N||r==x.length/N*2||r==x.length)) {29if (!checkedbox(x,r)) {30return;31 };3233 }34if (c>=x.length) {35 function(x, r+1, 0);36return;37 }3839if (x[r][c]==0) {40for (int i = 1; i <= x.length; i++) {41if (checked(x,r,c,i)) {42 x[r][c]=i;43 function(x, r, c+1);44 x[r][c]=0;45 }46 }47 }else{48 function(x, r, c+1);49 }50 }51private static boolean checkedbox(int[][] x, int r) {52for (int k = 0; k < x.length; k+=x.length/N) {53 Map<Integer, Integer> map=new HashMap<>();54for (int i = r-N; i < r; i++) {55for (int j = k; j < k+x.length/N; j++) {56if (map.containsKey(x[i][j])) {57return false;58 }59 map.put(x[i][j], 1);60 }61 }6263 }64return true;65 }6667private static boolean checked(int[][] x, int r, int c, int i) {68for (int j = 0; j < x.length; j++) {69if (x[j][c]==i) {70return false;71 }72if (x[r][j]==i) {73return false;74 }75 }76return true;77 }7879private static void show(int[][] x) {80for (int i = 0; i < x.length; i++) {81for (int j = 0; j < x.length; j++) {82 System.out.print(x[i][j]+" ");83 }84 System.out.println();85 }86 System.out.println();87 }8889 }类⾥有五个函数,⼀是主函数不多说;⼆是递归的主体函数function,是解决数独的关键,体现循环+递归+回溯的主要逻辑;三和四都是是⼀个辨识函数,⼀些较为复杂的判断逻辑把它抽出来写成辨识函数可以增加代码的可读性;五是⼀个打印函数很简单。
数独解题在EXCEL中用VBA编程实现

ห้องสมุดไป่ตู้
1 引言 数独是一种数字游戏,其是列出 9 行 9 列 共 81 个 格 ,类 似 一个 9×9 矩阵,称为大九宫格。 这 9×9 矩阵又平均划分为 9 个 小矩阵,每个小矩阵成为一个小九宫格。9 个小的九宫格就组成 了大九宫格。数独的填数规则如下:每一行只能填入 1 到 9 这 9 个数字,且同一行的 9 个格内所填数字不能重复。 同时每一列 的 9 个格和每个小九宫格的 9 个小格也都如此。 数独的填数规则简单明了, 但解题方法就相对丰富了,有 排除法,余数法,隐含唯一数法,数对法,回溯法等方法。 通常在 解题过程中都是多种方法并用。 2 算法思路 本程序解数独题的大体思路是,从大九宫格的第一行左边 第一格开始往右,逐个检查未填格,第一行检查完就转到第二 行检查,顺次往下全部检查未填格。 检查过程中,确定每个未填 格可选填数字是哪些。 按检查顺序找出第一个未填格,且这个 格满足的条件是可选填数只有 2 个,然后选择 2 个数中的一个 填入,再按数独填数规则检查所填是否满足规则,若满足规则, 则如上方法测试下一个未填格。 若不满足规则,则选择另一个 数填入。 如果两个可选数都不满足数独填数规则,则回退到前 一未填格更换选填数。 该方法是穷举法,假设条件是肯定有一 个数是满足规则可填入该格的。 3 存储结构 本程序运行过程的数据大部分直接在工作表中储存,只有 少量行号,列号和表名变量在内存内存储。 单个工作表分为四 个区。 第一个区是数独题目区,该区直接以大九宫格的形式展现 题目,设置占用工作表前 9 行前 9 列的 81 个单元格。 第二个区是“单元格选择情况痕迹数据区”,该区放在大九 宫格右边,存储的信息为本层级程序解题过程即将选择填入数 据的单元格的相关信息。 第三个区是辅助选数区,设置位于大九宫格正下方,该区 域从工作表的第 11 行开始,每行对应大九宫格的一 个 格 ,大 九 宫格的第 1 行第 1 格就对应工作表的第 11 行,以此类推。 辅助 选数区的每行左边第 1 个单元格,存储大九宫格对应格可选填 数个数,其他格填入具体可选数字。 第四个区为存储痕迹区,设置位于辅助选数区右边。 该区 域为辅助选数区有数据变动前或后,将辅助选数区所有数据原 样拷贝至痕迹区, 以便下一步某个格选填新数并校验规则后, 比较大九宫格各格可选填数中是否有被剔除的。 4 算法流程图
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
function S=sudoku(A)
%SUSOKU0 功能:求解数独
%调用格式:S=sudoku(A)
%A--需判断的数独矩阵(9×9),空格用0替代
%S--数独的解
%A=xlsread('sudoku.xls','A11:I19');
k=1;
s=1;
A1=cell(9);
C=cell(9);
n0=0 %n0>2
A_ti=A;%原始题目
while 1
[d,p]=find_variable(A);
C{p(1),p(2)}=d;
n=length(d);
if n>n0
n0=n
end
if n>1
k1(s)=9*(p(2)-1)+p(1);
s=s+1;
A1{p(1),p(2)}=A;
elseif n==0
s=s-1;
if A(k1(s))==C{k1(s)}(end)
s=s-1;
end
k=k1(s);%break
A=A1{k};
A(k)=C{k}(2);%目前为止没有超过2个可选元素的
continue
end
A(p(1),p(2))=C{p(1),p(2)}(1);
k=k+1;
if sum(sum(ceil(A/9)))==81%判断是否填完了
break
end
%if sum(sum(ceil(A/9)))==81
% break
%end
end
S=A
%A=xlswrite('sudoku.xls',S,'k11:S19');
function [D,P]=find_variable(A)
%Find_variable 功能:找出数独矩阵中可填值最少的位置及可填值
%调用格式:[D,P]=find_variable(A)
%S--需判断的数独矩阵(9×9),空处用0替代
%D--可填的值
%P--可填值最少的位置,P中第1个元素为所在行,第2个元素为所在列
global C
D=[1:9];
for k1=1:9
for k2=1:9
if A(k1,k2)~=0 %跳过已给数值
continue
else
n1=ceil(k1/3);
n2=ceil(k2/3);
m=A(3*n1-2:3*n1,3*n2-2:3*n2);
a=A(k1,:); %行
b=A(:,k2)'; %列
c=reshape(m',1,9); %宫
d0=setdiff(1:9,union(union(a,b),c)); %行列宫并集的补集,即可填数
if length(d0)<length(D) %选出元素最少的
D=d0;P=[k1,k2]; %返回元素值及位置
end
end
end
end。