简单的数独游戏求解程序(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 算法流程图
基于MATLAB的2048小游戏

基于MATLAB的2048小游戏第一章:实验目的以及玩法演示引言2048是一款广受欢迎的滑动拼图游戏。
玩家需要通过滑动屏幕来移动方块,当两个相同数字的方块碰撞时,它们会合并成一个更大的数字。
游戏的目标是创建一个具有数字2048的方块,但如果在移动过程中无法进行任何有效的移动,则游戏结束。
在本次课程设计中,我们将使用MATLAB语言来设计并实现一个简化版的2048游戏。
MATLAB是一种高效的编程语言,适用于算法开发、数据可视化以及数据分析等。
1.1实验目的:1、运用MATLAB设计2048小游戏。
2、提升MATLAB代码编写能力。
3、学会利用MATLAB GUI设计图形交互界面。
1.2演示:2048游戏规则:(1)点击键盘上的上下左右按钮,控制数字的滑动;(2)滑动的数字如果碰到相同数字,则合并为更高级数字;(3)有数字合并后,会在随机位置新增加一个随机的数字2或4;(4)游戏结束:游戏界面被数字填满不能再滑动;(5)游戏总分数:游戏过程中的数字相加总和。
第二章:设计思路以及流程2.1设计思路错误!未找到引用源。
:首先构成一个4成4的矩阵,在表格中生成最初的数字2,生成位置随机。
其次设计四个按键分别实现上下左右移动整个矩形里的数字,随后合并相同的数字,当矩阵中数字满了且无法继续合并时游戏结束。
最后设计出得分面板,用来记录游戏补数转化成得分。
2.2功能需求描述l、图形用户界面:2048的最大特点就是玩家对图形界面里的数字进行操作,也就是是玩家与游戏的互动2、当前分数scoRE与最高分数的显示:在我们设计的2048游戏中当前分数取了页面i内所有数字相加的值为分数,对玩家玩游戏的进展有直接性、客观性的展现,同时,最高分数取了以往玩家退出游戏时所保存分数的最高分3、数字颜色‘游戏中数宇的颜色以2为首项的等比数列变化,即2、4、8、l6、32、“、128、256、5l2、I024、2048_.对应的数字卡片变色4、游戏的退出:游戏退出时,我们采用弹出对话框的确认玩家是否真的要退出游戏,当然这样做更符合游戏人性化设计的观念。
数独游戏-如何用代码实现(思路分析)

数独游戏-如何⽤代码实现(思路分析)数独游戏-如何⽤代码实现最近开始喜欢起来玩数独,在⼿机上找来⼏个数独⼩游戏玩着玩着突然想起我是个程序员.........那我何不⾃⼰写⼀个数独软件,⽹上查了⼀下数独有6,670,903,752,021,072,936,960(约有6.67×10的21次⽅)种组合。
我xxxx.......算了算了不想那么多了,先⾃⼰尝试做⼀个简单的下⾯的是我设计思路⾸先给没有玩过的同学普及⼀下数独:数独(shù dú)是源⾃18世纪瑞⼠的⼀种数学游戏。
是⼀种运⽤纸、笔进⾏演算的逻辑游戏。
玩家需要根据9×9盘⾯上的已知数字,推理出所有剩余空格的数字,并满⾜每⼀⾏、每⼀列、每⼀个粗线宫(3*3)内的数字均含1-9,不重复那么第⼀件事考虑如何如何⽣成⼀个9*9并且每⼀⾏,每⼀列,每⼀宫都不重复的⼆维数组?突然我灵机⼀动,如果..我⼿写个⼆维数组怎么样.int[][] arrray1 = new int[9][];arrray1[0] = new int[9] { 5, 6, 4, 8, 9, 7, 2, 3, 1 };arrray1[1] = new int[9] { 9, 7, 8, 3, 1, 2, 6, 4, 5 };arrray1[2] = new int[9] { 3, 1, 2, 6, 4, 5, 9, 7, 8 };arrray1[3] = new int[9] { 6, 4, 5, 9, 7, 8, 3, 1, 2 };arrray1[4] = new int[9] { 7, 8, 9, 1, 2, 3, 4, 5, 6 };arrray1[5] = new int[9] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };arrray1[6] = new int[9] { 4, 5, 6, 7, 8, 9, 1, 2, 3 };arrray1[7] = new int[9] { 8, 9, 7, 2, 3, 1, 5, 6, 4 };arrray1[8] = new int[9] { 2, 3, 1, 5, 6, 4, 8, 9, 7 };嗯~ o( ̄▽ ̄)o感觉也不是不可以,如果每⾏扣去4个的话 C94 有126种那⼀共应该有1134种组合上⾯的数组是固定的如果在写⼏组数组呢?好像是可以有更多的组合但是成本太⾼,并且要找到每⼀宫每⼀列都不重复的组合也需要花费些时间⽽且也失去了乐趣,换⼀个⾓度想如果不能变当前这个⼆维数组,可不可以通过其他⽅式来改变这个数组,可以使⽤⼀个⼀位数组同样是1-9的数字,通过判断⼆维数组中和⼀维数组相等的数据并取下⼀个位置的值,⽬的就是让⼀维数组把⼆维数组中的值循环变⼀下,因为⼀维数组也是1-9的不重复数字因此不会影响到⾏和列及宫的组合,⼤家可以⾃⾏验证⼀下.private static int[][] creatSudokuArray(int[][] seedArray, List<int> randomList){for (int i = 0; i < 9; i++){for (int j = 0; j < 9; j++){for (int k = 0; k < 9; k++){if (seedArray[i][j] == randomList[k]){seedArray[i][j] = randomList[(k + 1) % 9];break;}}}}return seedArray;}这样⽣成的数独也就有9!=362880 在通过扣掉若⼲个格⼦那最终的结果OK 数独⽣成了那下⾯具体就是需要去画⼀个9*9的矩阵并且把⽣成的数独填充进去然后扣掉部分我这⾥使⽤的是winform实现的具体填充代码就不说了下⾯会给贴出github的源码地址⼤家可以去下载,主要还是说思路.扣数据部分也是⽤随机数来操作的单纯的去掉⼏个感觉太死板了,附代码/// <summary>/// ⽣成数独选择难度/// </summary>/// <param name="a"></param>private void printArray(int[][] a){int length = 4;switch (boBox1.Text){case "简单":length = 4;break;case "中等":length = 5;break;case "困难":length = 6;break;}Random random = new Random();for (int i = 0; i < 9; i++){for (int j = 0; j < 9; j++){int randomNum = random.Next(9);if (randomNum > length){tbArray[i, j].Text = a[i][j].ToString();}else{tbArray[i, j].ReadOnly = false;}}}}最后就是要去验证⾃⼰填进去的数字是否满⾜数独的要求那么我们就需要每⼀⾏,每⼀列,每⼀宫的检查,⾏和列很简单只要验证当前⾏或者列是否有重复数据或者去重之后的长度是否⼩于9即可,相信⼤家可以通过各种语⾔来实现了把,下⾯贴出我的代码/// <summary>/// 校验⾏/// </summary>public bool rowCheck(){for (int y = 0; y < 9; y++)List<int> lines = new List<int>();for (int x = 0; x < 9; x++){string value = tbArray[x, y].Text;if (!String.IsNullOrEmpty(value)){lines.Add(Convert.ToInt32(value));}else{MessageBox.Show("请确认是否填写完毕");return false;}}if (lines.Distinct().Count() < 9){MessageBox.Show("验证失败,请检查完重新提交");return false;}}return true;}/// <summary>/// 校验列/// </summary>public bool cellCheck(){for (int y = 0; y < 9; y++){List<int> lines = new List<int>();for (int x = 0; x < 9; x++){string value = tbArray[y, x].Text;if (!string.IsNullOrEmpty(value)){lines.Add(Convert.ToInt32(value));}else{MessageBox.Show("请确认是否填写完毕");return false;}}if (lines.Distinct().Count() < 9){MessageBox.Show("验证失败,请检查完重新提交");return false;}}return true;}那么每⼀宫怎么检验呢?其实和⾏列的⽅法相识我们可以吧每⼀宫的数据计算出来放到⼀个数组中最后相同的⽅式去判断即可 /// <summary>/// 校验九宫格每⼀宫是否有重复/// </summary>public bool palaceCheck(){int[,] newArrary = block();for (int y = 0; y < 9; y++){List<int> lines = new List<int>();for (int x = 0; x < 9; x++){lines.Add(newArrary[x, y]);}if (lines.Distinct().Count() < 9){MessageBox.Show("验证失败,请检查完重新提交");return false;}}return true;}//将每块的数字保存⾄⼀个⼆维数组public int[,] block(){int[,] b = new int[9, 9];for (int i = 0; i < 9; i++)for (int j = 0; j < 9; j++)//将数独从左⾄右从上⾄下分为9块,求该单元格属于第⼏块,将该块数字保存⾄b第⼏⾏ int rowOfB = i / 3 * 3 + j / 3;//每块有9个数字,求该数字属于第⼏个,保存⾄b第⼏列int columnOfB = i % 3 * 3 + j % 3;b[rowOfB, columnOfB] = Convert.ToInt32(tbArray[i, j].Text);}return b;}整个的实现思路就完成了,下⾯的就是具体优化了,下⾯是我的最终效果源码:⼩伙伴们有更好的⽅式可以分享出来⼤家⼀起讨论。
求解数独的程序代码

求解数独的程序代码求解数独所有解(适合所有数独)的PASCAL程序var a:packed array[1..9,1..9] of longint;i,j,k,p,l,m,n,ii,ans,mm,oo:longint;s,s1:packed array[0..100,1..4] of longint;x,y,xy:packed array[0..9,-1..9] of boolean; //横向,纵向,九宫的检验t,tt,u:boolean;opo:longint;ll:packed array[0..9,0..9,-1..11] of longint; //存储每个空格可能出现的数字提高程序效率function max(a,b:longint):longint;beginif b>a then exit(b)else exit(a);end;function choose2(x:longint):longint;begincase x of0..3:exit(1);4..6:exit(2);7..9:exit(3);end;end;function iff:boolean; //搜索结束条件beginif (k<1) then exit(false)else exit(true);end;function pa(i,j:longint):longint; //得到九宫格编号var o,kk,jj,ii:longint;begino:=choose2(i);kk:=choose2(j);case o of1:jj:=0;2:jj:=3;3:jj:=6;end;exit(jj+kk);end;beginfillchar(x,sizeof(x),true);fillchar(y,sizeof(y),true);fillchar(xy,sizeof(xy),true);for i:=1 to 9 dofor j:=1 to 9 dobeginread(ii);a[i,j]:=ii;if ii=0 then begininc(n);s1[n,1]:=j;s1[n,2]:=i;endelse beginx[i,ii]:=false;y[j,ii]:=false;xy[pa(i,j),ii]:=false;end;end;for i:=1 to 9 dofor j:=1 to 9 dobeginfor oo:=1 to 9 doif x[i,oo]and y[j,oo] and xy[pa(i,j),oo] then begininc(ll[i,j,-1]);ll[i,j,ll[i,j,-1]]:=oo;end;end;for i:=1 to n dos[i]:=s1[n-i+1];k:=1; i:=0;t:=true;tt:=false;while iff dobeginif t thenbeginfor i:=ll[s[k,2],s[k,1],-1] downto 0 doif (x[s[k,2],ll[s[k,2],s[k,1],i]]and y[s[k,1],ll[s[k,2],s[k,1],i]]) and xy[pa(s[k,2],s[k, 1]),ll[s[k,2],s[k,1],i]] thenbegint:=true;break;end;endelse begin i:=s[k,4]; tt:=false;repeatdec(i);until ((x[s[k,2],ll[s[k,2],s[k,1],i]]and y[s[k,1],ll[s[k,2],s[k,1],i]]) and xy[pa(s[k,2], s[k,1]),ll[s[k,2],s[k,1],i]])or (i<1);end;if i<1 then begin s[k,3]:=0; a[s[k,2],s[k,1]]:=0;dec(k); t:=false;i:=s[k,3]; x[s[k,2],i]:=true;y[s[k,1],i]:=true; //向上回溯xy[pa(s[k,2],s[k,1]),i]:=true;endelse t:=true;if t thenbegins[k,4]:=i;s[k,3]:=ll[s[k,2],s[k,1],i];a[s[k,2],s[k,1]]:=s[k,3];x[s[k,2],s[k,3]]:=false;y[s[k,1],s[k,3]]:=false;xy[pa(s[k,2],s[k,1]),s[k,3]]:=false;inc(k);end;tt:=false;if k>n thenbegininc(opo);writeln(opo); // 计数器for i:=1 to 9 dobeginfor j:=1 to 9 dowrite(a[i,j],' ');writeln;end;writeln;mm:=0;x[s[k-1,2],s[k-1,3]]:=true;y[s[k-1,1],s[k-1,3]]:=true;xy[pa(s[k-1,2],s[k-1,1]),s[k-1,3]]:=true;dec(k);t:=false;end;end;writeln(opo);end.求解数独的简单C语言程序(适合仅有唯一解的数独)/*数独求解*/ #include <stdio.h>void print(int a[9][9]) /*格式化输出数独*/{int i,j;for(i=0;i<9;i++){for(j=0;j<9;j++)printf("%d ",a[i][j]);printf("\n");}}void ini_logo(int logo[10][9][9],int arr[9][9]) /*初始化标志数组*/ {int i,j,k,p,r,s,t;for(i=0;i<9;++i)for(j=0;j<9;++j)if(arr[i][j]!=0)for(k=1;k<=9;++k)logo[k][i][j]=1;for(i=0;i<9;++i)for(j=0;j<9;++j)if(arr[i][j]!=0){p=arr[i][j];for(r=0;r<9;++r){logo[p][i][r]=1;logo[p][r][j]=1;}for(s=(i/3)*3;s<(i/3)*3+3;++s)for(t=(j/3)*3;t<(j/3)*3+3;++t)logo[p][s][t]=1;}}int add(int arr[9][9],int logo[10][9][9],int m,int n,int k) /*arr[m][n]插入数字,修改arr,logo数组*/{int i,s,p,t;arr[m][n]=k;for(p=1;p<=9;++p)logo[p][m][n]=1;for(i=0;i<9;++i){logo[k][m][i]=1;logo[k][i][n]=1;}for(s=(m/3)*3;s<(m/3)*3+3;++s)for(t=(n/3)*3;t<(n/3)*3+3;++t)logo[k][s][t]=1;}int check(int logo[10][9][9],int arr[9][9]) /*检测行列和小九宫格*/{int i,j,k,p,q,r,s,t,m,n,tag=0; /*tag标志本轮是否修改*/for(k=1;k<=9;++k){for(i=0;i<9;++i){p=0;q=0;for(j=0;j<9;++j){if(logo[k][i][j]==0){r=j;p++;} /*检测行*/if(logo[k][j][i]==0){s=j;q++;} /*检测列*/}if(p==1){tag=1;add(arr,logo,i,r,k);}if(q==1){tag=1;add(arr,logo,s,i,k);} /*满足一个添加的条件,修改arr,logo数组和标志tag*/}for(i=0;i<9;i=i+3) /*检测小九宫格*/for(j=0;j<9;j=j+3){t=0;for(m=i;m<i+3;++m)for(n=j;n<j+3;++n)if(logo[k][m][n]==0){q=m;s=n;t++;}if(t==1){tag=1;add(arr,logo,q,s,k);}}}return(tag);}main(){int arr[9][9]={0,0,0,0,0,0,0,0,0, /*数独初始化,其中0表示数字未给出*/0,2,3,0,0,0,7,8,0,1,0,0,4,0,6,0,0,9,4,0,0,0,5,0,0,0,1,9,0,0,0,0,0,0,0,6,0,6,0,0,0,0,0,9,0,0,0,5,0,0,0,8,0,0,0,0,0,3,0,1,0,0,0,0,0,0,0,9,0,0,0,0},logo[10][9][9]={0},i,j;ini_logo(logo,arr);while(check(logo,arr)==1) /*当一轮没有检测出,即结束*/{}print(arr);}============================================================ =====================Java解法(循环递归法):private boolean counting(int row, int col){// Fill the number as 1 to 9for (int num = 1; num < 10; num++){if (isLegal(row, col, num)){// Check whether the number is legalmatrix[row][col] = num;int nextRow = (row + 1 > 8) ? 0 : (row + 1);int nextCol = (col + 1 > 8) ? 0 : (col + 1);if (nextCol != 0) {// Not last columnif (counting(row, nextCol))return true;} else if (nextRow != 0) {// Last columnif (counting(nextRow, nextCol))return true;} else {// Last cellreturn true;}// Get false with the current selection, clear it and go onmatrix[row][col] = 0;}}// From 1 to 9, no number is legal, return falsereturn false;}上面只列出了主函数,如果要调用,还需要初始化matrix二维数组,然后写以下语句:if (counting(0, 0) == true)//有解else//无解。
matlab作业

1、猜数游戏。
首先由计算机产生[1,100]之间的随机整数,然后由用户猜测所产生的随机数。
根据用户猜测的情况给出不同提示,如猜测的数大于产生的数,则显示“High”,小于则显示“Low”,等于则显示“You won”,同时退出游戏。
用户最多可以猜7次。
程序文件:nu m=fix(rand(1)*100);n=1;while n<=7a=input('请输入一个1到100的随机整数');n=n+1;if a>nu mdisp('High');elseif a<nu mdisp('low');elsedisp('won');break;endend2、从键盘输入5个数,求其中最大数和最小数。
并在屏幕上提示输入“请输入您要显示数字的序号”,输入后,回显“您要显示数字为……(此处为实际需要显示序号对应的数字)”。
程序文件:disp('请输入5个数字')for n=1:5;x=input('');A(1,n)=x;endAB=sort(A);Bdisp('最大值是');B(1,5)disp('最小值是');B(1,1)disp('请输入您要显示数字的序号');nu m=input('');disp('您要显示数字为');A(1,nu m)3、求Fibonacci数列(1)大于4000的最小项。
(2)5000之内的项数。
程序文件:(1)大于4000的最小项f=[1,1];j=1;while f(1,j+1)<4000f(1,j+2)=f(1,j)+f(1,j+1);j=j+1;endfdisp('大于4000的最小项是');f(1,j+1)运行结果f =Colu m ns 1 through 101 123 5 8 13 21 34 55Colu m ns 11 through 1989 144 233 377 610 987 1597 2584 4181大于4000的最小项是ans =4181(2)5000之内的项数f=[1,1];j=1;while f(1,j+1)<5000f(1,j+2)=f(1,j)+f(1,j+1);j=j+1;endfdisp('5000之内的项数');j运行结果f =Colu m ns 1 through 101 123 5 8 13 21 34 55Colu m ns 11 through 2089 144 233 377 610 987 1597 2584 4181 67655000之内的项数j =19。
Matlab解数独游戏(DOC)

Matlab解数独游戏(DOC)中北大学中北大学课程设计任务书2016/2017 学年第一学期学院:信息与通信工程学院_____________ 专业: _________ 信息对抗技术_______________ 学生姓名:燕飞宇_______________ 学号:1405054217学生姓名:毕广宇_______________ 学号:1405054235课程设计题目:Matlab课程设计起迄日期:2016年12月5日〜2016年12月9日课程设计地点:信息对抗技术专业综合实验室指导教师:李凯、刘宾、杨志良、李沅学科部主任:_________________ 张丕状下达任务书日期:2016 年11月30日课程设计任务书&求信号的窗(1秒、2秒)自相关分析9、采用平均幅度差函数(AMD 法)提取求信号的基音周期、谱估计;10、根据数字特征对信号进行断句分割注:每个同学可以选择1和2、1和3、1和4、5和6、5和7、5和8 5和9、5 0的一组作为题目,8名同学完成该题目。
2:傅里叶变换设计 自己设计傅里叶变换函数(不能使用 Matlab 函数);3:数独游戏设计1、 根据数独游戏规则,设计数独判断程序;2、 自动生成数独表;3、 根据输入给出输出;注:2个同学一组。
4: Matlab 绘图界面设计1、 设计GUI 界面;2、 通过下拉菜单选择绘制正弦、正切信号;3、 通过编辑框输入命令,根据命令绘制信号;4、 通过按钮在图形中输出标注(标题、各轴名称); 注:1个同学一组。
5: Matlab 信号分析界面设计1、 设计GUI 界面;2、 通过按钮输入信号;3、 对信号进行频谱分析;4、 输出信号的频谱;注:1个同学一组。
6: Matlab 信号滤波分析界面设计2、 通过按钮输入信号;3、 对信号进行低通、高通、带通分析(FIR 、IIR 、巴特沃斯、切比雪夫);4、 输出处理信号的结果、频谱;和1题目 题目 题目题目注:每个同学选择一个滤波器,4名同学完成该题目 题目7: Matlab 智能信号发生器1、输出方波、矩形波、三角波和正弦波信号;2、 输出随时间变化频率的信号;3、 输出随时间变化幅值的信号4、 输出随时间变化斜率的等幅值的信号;5、 输出处理信号的结果、频谱;通过按钮选择计算方法,表格录入数据;对录入数据判断是否可算,实现算法如高斯-赛德尔、列主元消去法、高斯消 法等;注:每个同学选择一种算法,4名同学完成该题目。
matlab猜数字游戏程序,matlab猜数字小游戏

matlab猜数字游戏程序,matlab猜数字⼩游戏matlab 猜数字1 ~100内猜数游戏规则如下:(1)分为三个等级:选1为10次机会,选2为7次机会,选3为5次机会。
直接回车默认为1(2)如果猜对,⽴即推出,并且输出猜对所⽤的次数。
(3)如果所有机会⽤完并且没有猜对,则输出正确答案。
提⽰是否重玩,选0为继续,选1为退出:若选择0,回到选择等级界⾯。
直接回车默认为1matlab 猜数字1 ~100内猜数游戏规则如下:(1)分为三个等级:选1为10次机会,选2为7次机会,选3为5次机会。
直接回车默认为1(2)如果猜对,⽴即推出,并且输出猜对所⽤的次数。
(3)如果所有机会⽤完并且没有猜对,则输出正确答案。
提⽰是否重玩,选0为继续,选1为退出:若选择0,回到选择等级界⾯。
直接回车默认为1具体代码如下:disp('game begin')disp('please choose level')m=1;while(m==1)leval=input('I choose leval:');if isempty(leval)|leval==1i=1;rd=fix(rand.*100);while(i>=1)&(i<=10)disp('please input the number')num=input('the number=');if (num>rd)disp('oh, big');endif (num==rd)disp('oh, you are right')str=['cishu=',num2str(i)];eval(str)break;endif(numdisp('oh,little')endi=i+1;if (i==11)disp('game over')str=['result=',num2str(rd)]; eval(str)disp('try again?');choose=input('0/1');if choose==0clc;m=1;endif isempty(choose)|choose==1 m=0;clc;endendendendif (leval==2)i=1;rd=fix(rand.*100);while(i>=1)&(i<=7)disp('please input the number') num=input('the number=');if (num>rd)disp('oh, big');endif (num==rd)disp('oh, you are right')str=['cishu=',num2str(i)];eval(str)break;endif(numdisp('oh,little')endi=i+1;if (i==8)disp('game over')str=['result=',num2str(rd)]; eval(str)disp('try again?');choose=input('0/1');if choose==0clcm=1;endif isempty(choose)|choose==1 m=0;clcendendendendif (leval==3)i=1;rd=fix(rand.*100);while(i>=1)&(i<=5)disp('please input the number') num=input('the number=');if (num>rd)disp('oh, big');endif (num==rd)disp('oh, you are right')str=['cishu=',num2str(i)];eval(str)break;endif(numdisp('oh,little')endi=i+1;if (i==6)disp('game over')str=['result=',num2str(rd)]; eval(str)disp('try again?');choose=input('0/1');if choose==0clcm=1;endif isempty(choose)|choose==1 m=0;clcendendendendend。
matlab解九宫格数独

matlab解九宫格数独
要使用MATLAB解决九宫格数独问题,可以使用回溯算法来求解。
下面是一个基本的解决方案:
1. 定义数独棋盘:
创建一个9x9的矩阵来表示数独棋盘。
用数字0表示空格,
其他数字表示已知的初始数字。
2. 实现回溯算法:
编写一个函数,例如solveSudoku,该函数将接受数独矩阵
作为输入参数。
在函数内部,使用嵌套的循环遍历数独矩阵的每个位置。
对于每个空格,尝试填入数字1到9,并进行以下判断:
检查当前数字是否满足数独规则,即当前数字在所在行、
所在列以及所在3x3的小方格中是否唯一。
如果满足规则,将当前数字填入空格,并递归调用solveSudoku函数来填充下一个空格。
如果递归调用返回true,表示数独已经解决,返回true。
如果递归调用返回false,表示当前数字不符合要求,尝
试下一个数字。
如果所有数字都尝试过仍然无法解决数独,则返回false。
3. 完善主程序:
创建一个数独矩阵,并将初始已知数字填入。
调用solveSudoku函数来解决数独。
如果solveSudoku函数返回true,表示数独已解决,打印
解决方案。
如果solveSudoku函数返回false,表示数独无解。
这是一个基本的框架,你可以根据需要进行修改和优化。
回溯算法是一种经典的解决数独问题的方法,但在处理大规模数独时可能会有性能问题。
可以考虑使用其他优化算法来提高求解效率。
希望以上信息对你有帮助!。
简单的数独游戏求解程序

简单的数独游戏求解程序(m a t l a b)(总2页)--本页仅作为文档封面,使用时请直接删除即可----内页可以根据需求调整合适字体及大小--function S=sudoku(A)%SUSOKU0 功能:求解数独%调用格式:S=sudoku(A)%A--需判断的数独矩阵(9×9),空格用0替代%S--数独的解%A=xlsread('','A11:I19');k=1;s=1;A1=cell(9);C=cell(9);n0=0 %n0>2A_ti=A;%原始题目while 1[d,p]=find_variable(A);C{p(1),p(2)}=d;n=length(d);if n>n0n0=nendif n>1k1(s)=9*(p(2)-1)+p(1);s=s+1;A1{p(1),p(2)}=A;elseif n==0s=s-1;if A(k1(s))==C{k1(s)}(end)s=s-1;endk=k1(s);%breakA=A1{k};A(k)=C{k}(2);%目前为止没有超过2个可选元素的 continueendA(p(1),p(2))=C{p(1),p(2)}(1);k=k+1;if sum(sum(ceil(A/9)))==81%判断是否填完了breakend%if sum(sum(ceil(A/9)))==81% break%endendS=A%A=xlswrite('',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 CD=[1:9];for k1=1:9for k2=1:9if A(k1,k2)~=0 %跳过已给数值continueelsen1=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 endend。
matlab小程序

输入5个分数,小于60分的显示‘No pass!’,大于或等于60分显示‘Pass!’。
>> for i=1:5S=input('Enter the score')if S<60disp('No passed!')elsedisp('Passed!')endendEnter the score43S =43No passed!Enter the score55S =55No passed!Enter the score86S =86Passed!Enter the score84S =84Passed!Enter the score57S =57No passed! >> 99ans =99输入分数,大于90的显示‘Good!’,其余的显示‘Ok!’。
>> S=input('Enter the score')if S>90disp('Good!')elsedisp('Ok!')endEnter the score91S =91Good!>>求定积分sin24t txdt。
>> x=sym('x');t=sym('t');>> f=8*t*x;>> b=sin(t);>> Ix=int(f,x,2,b)Ix =- 4*t*cos(t)^2 - 12*t求微分方程dx/dt=6x-y,dy/dt=2x-y的通解。
>> [x,y]=dsolve('Dx=6*x-y','Dy=2*x-y','t')x =(7*C1*exp((5*t)/2 + (41^(1/2)*t)/2))/4 + (7*C2*exp((5*t)/2 - (41^(1/2)*t)/2))/4 + (41^(1/2)*C1*exp((5*t)/2 + (41^(1/2)*t)/2))/4 - (41^(1/2)*C2*exp((5*t)/2 - (41^(1/2)*t)/2))/4y =C1*exp((5*t)/2 + (41^(1/2)*t)/2) + C2*exp((5*t)/2 - (41^(1/2)*t)/2)绘制曲线y1=sinx,y2=x e。
求解数独的计算机程序

//刘天一,2013年1月29日,THU,DPI。
编程风格比较烂,大家见谅//划数法解数独,仿照人的思维#include<stdio.h>#include<stdlib.h>#include<math.h>#include<string.h>typedef unsigned int uint;typedef struct{uint x;uint y;char num[9];char all;} able;//用于记录可以填充的数FILE* mopen(uint filenum);//打开文件void save(char buffer[10][10],uint zushu);//显示结果void read(char buffer[10][10],uint zushu);//读取已知条件void numprint(char buffer[10][10],uint zushu);//显示结果void mat(char matnum[9][2],uint x,uint y);//返回某点所属矩阵的9个点bool ok(char buffer[10][10]);//判断一个排列是否符合规则bool ok_mat(char buffer[10][10],uint mat_x,uint mat_y);//判断一个小矩阵内的9个数是否符合规则void huashu1(char buffer0[10][10],char buffer[10][10][10]);//划掉所有数void huashu0(char buffer[10][10][10],uint x,uint y,char num);//划去一个数void fill3(char buffer[10][10][10],uint x,uint y,uint z);//填充0void fillable(able* a,char buffer[10][10][10],char buffer0[10][10]);//生成记录表,哪些数可以填充bool easyfill(able* a,char buffer0[10][10],char buffer[10][10][10]);//简单空格填充int tryfill(able* a,char buffer[10][10][10],char trymat[10][10],uint trynum);//复杂空格填充尝试uint trymax(able* a);//尝试的最大次数(小于,不能小于等于)void copy2(char buffer0[10][10],char trymat[10][10]);void copy3(char buffer1[10][10][10],char buffer2[10][10][10]);void copya(able* a1,able* a2);int main(){//标记0是质数1合数char buffer0[10][10];char buffer1[10][10][10];able a[81];fill3(buffer1,10,10,10);read(buffer0,0);numprint(buffer0,0);huashu1(buffer0,buffer1);fillable(a,buffer1,buffer0);while(easyfill(a,buffer0,buffer1)){fillable(a,buffer1,buffer0);}if(ok(buffer0)){numprint(buffer0,0);save(buffer0,1);printf("\nFINISHED\n\n");system("PAUSE");return 0;}//uint max=trymax(a);uint try0=0;able newa[81];char newmat[10][10];char newbuffer[10][10][10];while(1){copy2(buffer0,newmat);copy3(buffer1,newbuffer);copya(a,newa);if(tryfill(newa,newbuffer,newmat,try0))break;if(ok(newmat)){numprint(newmat,try0);save(newmat,try0);}try0++;}printf("\nFINISHED\n\n");system("PAUSE");return 0;}void num2str(uint x,char* str){uint x2;uint p=0;x2=x;while(x2>=10){str[p]=x2%10;x2=(x2-str[p])/10;str[p]+='0';p++;}str[p]=x2%10+'0';p++;str[p]=0;}FILE* mopen(uint filenum){char numchar[10];num2str(filenum,numchar);int t=0;while(numchar[t])t++;numchar[t]='.';numchar[t+1]='t';numchar[t+2]='x';numchar[t+3]='t';numchar[t+4]=0;FILE* fp;fp=fopen(numchar,"a+");if(fp!=0);else{printf("wrong\n");system("PAUSE");exit(5);}return fp;}void save(char buffer[10][10],uint zushu) {FILE* fpnum;fpnum=mopen(zushu);uint x,y;for(x=1;x<=9;x++){for(y=1;y<=9;y++){fprintf(fpnum,"%d\t",buffer[x][y]);}fprintf(fpnum,"\n");}fclose(fpnum);}void read(char buffer[10][10],uint zushu){FILE* fpnum;fpnum=mopen(zushu);uint x,y;uint temp;for(x=1;x<=9;x++){for(y=1;y<=9;y++){fscanf(fpnum,"%d",&temp);buffer[x][y]=(char)temp;}}fclose(fpnum);}void numprint(char buffer[10][10],uint zushu) {uint x,y;char numchar[10];num2str(zushu,numchar);printf(numchar);printf("\n");for(x=1;x<=9;x++){for(y=1;y<=9;y++){printf("%d\t",buffer[x][y]);}printf("\n");}printf("\n");}void mat(char matnum[9][2],uint x,uint y){switch (x){case 2:case 3:matnum[0][0]=1;matnum[1][0]=1;matnum[2][0]=1;matnum[3][0]=2;matnum[4][0]=2;matnum[5][0]=2;matnum[6][0]=3;matnum[7][0]=3;matnum[8][0]=3;break;case 4:case 5:case 6:matnum[0][0]=4;matnum[1][0]=4;matnum[2][0]=4;matnum[3][0]=5;matnum[4][0]=5;matnum[5][0]=5;matnum[6][0]=6;matnum[7][0]=6;matnum[8][0]=6;break;case 7:case 8:case 9:matnum[0][0]=7;matnum[1][0]=7;matnum[2][0]=7;matnum[3][0]=8;matnum[4][0]=8;matnum[5][0]=8;matnum[6][0]=9;matnum[7][0]=9;matnum[8][0]=9;break;}switch (y){case 1:case 3:matnum[0][1]=1;matnum[1][1]=2;matnum[2][1]=3;matnum[3][1]=1;matnum[4][1]=2;matnum[5][1]=3;matnum[6][1]=1;matnum[7][1]=2;matnum[8][1]=3;break;case 4:case 5:case 6:matnum[0][1]=4;matnum[1][1]=5;matnum[2][1]=6;matnum[3][1]=4;matnum[4][1]=5;matnum[5][1]=6;matnum[6][1]=4;matnum[7][1]=5;matnum[8][1]=6;break;case 7:case 8:case 9:matnum[0][1]=7;matnum[1][1]=8;matnum[2][1]=9;matnum[3][1]=7;matnum[4][1]=8;matnum[5][1]=9;matnum[6][1]=7;matnum[7][1]=8;matnum[8][1]=9;break;}}void fill(char* num,uint n) {for(x=0;x<n;x++)num[x]=0;}bool ok(char buffer[10][10]){char num[10];uint x,y,p;for(x=1;x<=9;x++){fill(num,10);for(y=1;y<=9;y++){num[buffer[x][y]]++;}for(p=1;p<=9;p++){if(num[p]!=1)return false;}}for(y=1;y<=9;y++){fill(num,10);for(x=1;x<=9;x++){num[buffer[x][y]]++;}for(p=1;p<=9;p++){if(num[p]!=1)return false;}}for(x=1;x<=3;x++){for(y=1;y<=3;y++){if(ok_mat(buffer,x,y)==false)return false;}}return true;}bool ok_mat(char buffer[10][10],uint mat_x,uint mat_y)//判断一个小矩阵内的9个数是否符合规则{char num[10];char matxy[9][2];uint p;fill(num,10);mat(matxy,mat_x*3,mat_y*3);for(p=0;p<9;p++){num[buffer[matxy[p][0]][matxy[p][1]]]++;}for(p=1;p<=9;p++){if(num[p]!=1)return false;}return true;}void huashu0(char buffer[10][10][10],uint x,uint y,char num){//1表示被划掉了uint p;char matxy[9][2];mat(matxy,x,y);for(p=0;p<=9;p++){buffer[x][p][num]=1;buffer[p][y][num]=1;buffer[matxy[p][0]][matxy[p][1]][num]=1;}}void huashu1(char buffer0[10][10],char buffer[10][10][10]){uint x,y;for(x=1;x<=9;x++){for(y=1;y<=9;y++){huashu0(buffer,x,y,buffer0[x][y]);}}}void fill3(char buffer[10][10][10],uint x,uint y,uint z){uint r,s,t;for(r=0;r<x;r++)for(s=0;s<y;s++)for(t=0;t<z;t++)buffer[r][s][t]=0;}void fillable0(able* a){uint p;a->x=0;a->y=0;for(p=0;p<9;p++)a->num[p]=0;a->all=0;}void fillable(able* a,char buffer[10][10][10],char buffer0[10][10]) {uint x,y,p;uint pa=0;uint t;for(x=1;x<=9;x++){for(y=1;y<=9;y++){if(buffer0[x][y]==0){fillable0(&a[pa]);a[pa].x=x;a[pa].y=y;t=0;for(p=1;p<=9;p++){if(buffer[x][y][p]==0){a[pa].num[t]=p;t++;}}a[pa].all=t;if(t>0)pa++;//信息表的末尾,all=0}}}fillable0(&a[pa]);}bool easyfill(able* a,char buffer0[10][10],char buffer[10][10][10]) {uint pa=0;uint sum=0;while(a[pa].all){if (a[pa].all==1){buffer0[a[pa].x][a[pa].y]=a[pa].num[0];huashu0(buffer,a[pa].x,a[pa].y,a[pa].num[0]);sum++;}pa++;}if (sum>0){return true;}elsereturn false;}void copy2(char buffer0[10][10],char trymat[10][10]){uint x,y;for(x=0;x<10;x++)for(y=0;y<10;y++)trymat[x][y]=buffer0[x][y];}/*uint maxpa(able* a){uint pa=0;while(a[pa].all)pa++;return pa;}*/void copya(able* a1,able* a2){uint pa=0;while(a1[pa].all){a2[pa]=a1[pa];pa++;}}void copy3(char buffer1[10][10][10],char buffer2[10][10][10]){uint r,s,t;for(r=0;r<10;r++)for(s=0;s<10;s++)for(t=0;t<10;t++)buffer2[r][s][t]=buffer1[r][s][t];}int tryfill(able* a,char buffer[10][10][10],char trymat[10][10],uint trynum) {// uint pa=0;uint c=100;uint tryn=trynum;if(a[0].all){c=tryn%a[0].all;trymat[a[0].x][a[0].y]=a[0].num[c];tryn=(tryn-c)/a[0].all;huashu0(buffer,a[0].x,a[0].y,a[0].num[c]);fillable(a,buffer,trymat);while(easyfill(a,trymat,buffer)){fillable(a,buffer,trymat);}return tryfill(a,buffer,trymat,tryn);}else return trynum;}uint trymax(able* a){uint pa=0;double max=1;while(a[pa].all){max*=a[pa].all;pa++;}return (uint)max;}//刘天一,2013年1月29日,THU,DPI*******************************************************已知条件保存在0.txt文件中,形如:0 2 0 4 5 6 7 0 00 5 0 7 8 9 1 0 07 8 0 1 2 3 4 0 02 0 0 5 6 7 8 0 00 0 0 8 0 1 2 0 00 0 0 2 3 4 5 0 00 0 0 6 7 8 9 0 00 0 0 9 1 2 3 0 00 0 0 3 4 5 6 0 0其中,0表示未知的空格。
- 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。