軟體實作與計算實驗 1  SUDOKU  New  Play  Check Lecture 8II SUDOKU PUZZLE.

Slides:



Advertisements
Similar presentations
Prof. Muhammad Saeed Mathematical Modeling and Simulation UsingMATLAB Graphic User Interface (GUI)Basics.
Advertisements

COMP 116: Introduction to Scientific Programming Lecture 37: Final Review.
Introduction to Matlab
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
M AT L AB Programming: scripts & functions. Scripts It is possible to achieve a lot simply by executing one command at a time on the command line (even.
數值方法 2008, Applied Mathematics NDHU 1 Nonlinear systems Newton’s method The steepest descent method.
MATLAB Examples. CS 1112 MATLAB Examples Find the number of positive numbers in a vector x = input( 'Enter a vector: ' ); count = 0; for ii = 1:length(x),
II. Linear Block Codes. © Tallal Elshabrawy 2 Last Lecture H Matrix and Calculation of d min Error Detection Capability Error Correction Capability Error.
Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Lecture 6 Sept 15, 09 Goals: two-dimensional arrays matrix operations circuit analysis using Matlab image processing – simple examples.
Concatenation MATLAB lets you construct a new vector by concatenating other vectors: – A = [B C D... X Y Z] where the individual items in the brackets.
MATLAB Cell Arrays Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University.
Graphical Tree-Based Scientific Calculator: CalcuWiz Will Ryan Christian Braunlich.
SOLVING SUDOKU WITH MATLAB
SOLVING SUDOKU WITH MATLAB VERIFICATION FUNCTION correctness verification of the puzzle: checks if the current element appears twice in the same line,
Matlab tutorial course Lesson 2: Arrays and data types
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
By: Adam Hebert.  Why Sudoku?  Attempts at an App  MATLAB Implementation - Use of webcam - Algorithm  Problems with method  Demonstration  Questions.
A short intermission about function handles and cell arrays A MATLAB function may be referenced by a handle. >> sphere(100)
Introduction to the Graphical User Interface (GUI) in MATLAB
Introduction to Matlab & Data Analysis
SOLVING SUDOKU WITH MATLAB Raluca Marinescu, Andrea Garcia, Ivan Castro, Eduard Enoiu Mälardalen University, Västerås,
ARRAYS 1.Basic Ideas 2.The Array Type 3.Processing Arrays 4.Parallel Arrays 5.Two-dimensional Array 6.Arrays as Parameters.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
function varargout = MovieJoiner(varargin) % MOVIEJOINER M-file for MovieJoiner.fig % MOVIEJOINER, by itself, creates a new MOVIEJOINER or raises the.
Introduction to MATLAB Damon Tomlin February 22, 2008 Lecture 3: Data Visualization & User Interfaces.
Chapter 4 Review: Manipulating Matrices Introduction to MATLAB 7 Engineering 161.
CSE 201 – Elementary Computer Programming 1 Extra Exercises Source: Suggested but not selected midterm questions.
HKOI2006 Analysis and Solution Junior Q3 – Sudoku HKOI Training Team
Propositional Satisfiability A compound proposition is satisfiable if there is an assignment of truth values to its variables that make it true. When no.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
SOLVING SUDOKU WITH MATLAB Raluca Marinescu, Andrea Garcia, Ivan Castro, Eduard Enoiu Mälardalen University, Västerås,
軟體實作與計算實驗 1  Binary search  While loop  Root Finding Lecture 6II While Loop.
Introduction to Matlab & Data Analysis 2015 In this tutorial we will: Build a practical application using GUIDE Learn more about graphical user interface.
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
ADVANTAGE of GENERATOR MATRIX:
WEEK 6 Class Activities Lecturer’s slides.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Introduction to Matlab Part II 1Daniel Baur / Introduction to Matlab Part II Daniel Baur / Michael Sokolov ETH Zurich, Institut für Chemie- und Bioingenieurwissenschaften.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
EGR 115 Introduction to Computing for Engineers MATLAB Basics 6: Debugging in MATLAB Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers.
EGR 115 Introduction to Computing for Engineers
Fall 2008Simple Parallel Algorithms1. Fall 2008Simple Parallel Algorithms2 Scalar Product of Two Vectors Let a = (a 1, a 2, …, a n ); b = (b 1, b 2, …,
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Introduction to Matlab Patrice Koehl Department of Biological Sciences National University of Singapore
EGR 115 Introduction to Computing for Engineers Graphical User Interface Design in MATLAB - Part 2 Monday 24 Nov 2014 EGR 115 Introduction to Computing.
CS112 Scientific Computation Department of Computer Science Wellesley College Blind to change More on GUIs.
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 1 Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Computer Science and Engineering Computer System Security CSE 5339/7339 Lecture 7 September 9, 2004.
Lecture (7) Introduction to GUI Eng. Osama Talaat 1.
Using and Programming with MATLAB as an Engineering Tool [ Part III ]
MATLAB and SimulinkLecture 61 To days Outline Graphical User Interface (GUI) Exercise on this days topics.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
PATTERN RECOGNITION LAB 8 TA : Nouf Al-Harbi::
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline 1 Introduction 2 Arrays 3Declaring Arrays 4Processing Array Contents 5 Multiple-Subscripted.
Introduction to Matlab
EGR 115 Introduction to Computing for Engineers
CS1371 Introduction to Computing for Engineers
More on Graphical User Interfaces
Lecture 5 MATLAB programming (3)
Matlab tutorial course
funCTIONs and Data Import/Export
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Using Script Files and Managing Data
Sudoku.
Algorithms CSCI 235, Spring 2019 Lecture 27 Dynamic Programming II
Presentation transcript:

軟體實作與計算實驗 1  SUDOKU  New  Play  Check Lecture 8II SUDOKU PUZZLE

軟體實作與計算實驗 2 4x4 Sudoku row column } } block

軟體實作與計算實驗 3 Sudoku Puzzle Numbers in the puzzle belong {1,2,3,4} Constraints Each column must contain four different numbers Each row must be filled with four different numbers Each block must be filled with four different numbers

軟體實作與計算實驗 4 9-by-9 Sudoku

軟體實作與計算實驗 5 Neural Computation

軟體實作與計算實驗 6

7 GUI Design

軟體實作與計算實驗 8 Open

軟體實作與計算實驗 9 A puzzle sudoku.fig Sudoku.m

軟體實作與計算實驗 10 Random permutation >> p1=randperm(4) p1 =

軟體實作與計算實驗 11 Flow Chart: New P1=randperm(4) P2=randperm(4) for i=1:4 row=P1(i); col=P2(i); C(row,col)=i;

軟體實作與計算實驗 12 Procedure : New Use randperm to generate two random permutations, p and q for i=1:4 j = p(i) d = q(i) C(i,j) = d Return C

軟體實作與計算實驗 13 New Randomly assign four different digits to four cells in the Puzzle such that one and only one cell in each row and column is assigned p= randperm(4);q=randperm(4); for each i Assign q(i) to the joint cell of column i and row p(i) Result One number in each row One number in each column All four numbers assigned are different

軟體實作與計算實驗 14 Press NEW

軟體實作與計算實驗 15 PLAY Press CHECK

軟體實作與計算實驗 16 Flow Chart : Row Checking N=size(S,2); Err=0; xx=1:4; for i=1:4 x=sort(S(i,:)); n=sum(x==xx); Err=Err+((N-n)>0);

軟體實作與計算實驗 17 Procedure : Row Checking Input a 4-by-4 matrix S; N=4; Set err to zero for i=1:4 A.Set n to the number of different digits in S(i,:) B.err =err +(N>n); Return err

軟體實作與計算實驗 18 Procedure : Column Checking Let S be a 4-by-4 matrix S; N=4 Set err to zero for i=1:4 A.Set n to the number of different digits in S(:,i) B.err =err +(N>n); Return err

軟體實作與計算實驗 } } block } } } } }}

軟體實作與計算實驗 20 Indexing block Block 1: S(1:2,1:2) Block 2: S(1:2,3:4) Block 3: S(3:4,1:2) Block 4: S(3:4,3:4)

軟體實作與計算實驗 21 Indexing K=2; N=4=K 2 Block 1 is denoted by (a,b)=(1,1) S(1:2,1:2) Block 2 is denoted by (a,b)=(1,2) S(1:2,3:4) Block 3 is denoted by (a,b)=(2,1) S(3:4,1:2) Block 4 is denoted by (a,b)=(2,2) S(3:4,3:4) Block (a,b) S((a-1)*K+1:a*K, (b-1)*K+1:b*K)

軟體實作與計算實驗 22 Flow Chart: Block checking N=size(S,2);K=sqrt(N) Err=0; xx=1:4; for a=1:K for b=1:K Get the block (a,b) Reshape to a vector Sort the vector, x n=sum(x==xx); Err=Err+(N>n);

軟體實作與計算實驗 23 Procedure: Block Checking Input a 4x4 matrix S; N = 4; K=2; Set Err to zero for a=1:K for b=1:K B=S((a-1)*K+1:a*K, (b-1)*K+1:b*K); x=sort(reshape(B,1,N)); Set n to the number of different digits in x Err=Err+(N-n)>0 Return err

軟體實作與計算實驗 24 Get puzzle Get N 2 =16 digits in edit-cells of a puzzle Store 16 digits in a vector edit1 edit2 edit3 edit4 edit5 edit16

軟體實作與計算實驗 25 Get Puzzle function C=Get_Puzzle(hObject, eventdata, handles) C=zeros(1,16); for i=1:16 str=['get(handles.edit' int2str(i) ',''String'')']; z=eval(str); if length(z) > 0 C(i)=str2double(z); else C(i)=0; end

軟體實作與計算實驗 26 Store a command to a string >> str=['get(handles.edit' int2str(i) ',''String'')'] str = get(handles.edit0,'String')

軟體實作與計算實驗 27 eval Two equivalent codes z=get(handles.edit0,'String') z=eval(str);

軟體實作與計算實驗 28 str=['get(handles.edit' int2str(i) ',''String'')']; z=eval(str); Get the content of the ith cell. Cell names: edit1, edit2, …, edit16

軟體實作與計算實驗 29 Set Puzzle function Set_Puzzle(hObject, eventdata, handles,C) for i=1:16 str=['set(handles.edit' int2str(i) ',''FontSize'',' '20)']; eval(str); if C(i) > 0 str=['set(handles.edit' int2str(i) ',''ForegroundColor'',' '''Red''' ')']; eval(str); z=int2str(C(i)); str=['set(handles.edit' int2str(i) ',''String'',' z ')']; eval(str); end

軟體實作與計算實驗 30 z=int2str(C(i)); str=['set(handles.edit' int2str(i) ',''String'',' z ')']; eval(str); Set the content of the ith cell to the number stored in C(i)

軟體實作與計算實驗 31 Exercise Download sudoku.m and sudoku.fig Implement the procedure for SUDOKU_New Implement procedures for row, column and block checking Revise sudoku.m New a sudoku game if the button ‘NEW’ is pressed Calculate errors of row, column and block checking

軟體實作與計算實驗 32 Function calling Create a SUDOKU game Implement procedure SUDOKU_New Call New_Sudoku at the beginning of pushbutton2_Callback function S=New_Sudoku()

軟體實作與計算實驗 33 Function name New_Sudoku Input None Output C: a 4x4 matrix function C=New_Sudoku()

軟體實作與計算實驗 34 function pushbutton2_Callback(hObject, eventdata, handles) % hObject handle to pushbutton2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) C=zeros(1,16); C(1)=1;C(2)=2;C(3)=3;C(4)=4; Set_Puzzle(hObject, eventdata, handles,C) 1.Call function New_Sudoku 2.The function output is a 4x4 matrix. 3.Reshape the output to a row vector before calling Set_Puzzle

軟體實作與計算實驗 35 Column-major reshape S=zeros(4,4); S(1,:)=[ ]; C=reshape(S',1,16)

軟體實作與計算實驗 36 Function call S=New_Puzzle(); Store the return to S function Call to New_Puzzle()