Lecture 11

Lecture 11 - 14:440:127– Introduction to Computers for...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 14:440:127– Introduction to Computers for Engineers Notes for Lecture 11 Rutgers University, Fall 2009 Instructor- Blase E. Ur 1 Last thoughts on Sudoku You guys came up with a number of interesting solutions. Here was my shortest one, which was definitely not the fastest way to approach the problem. This is a depth first search with backtracking, which means it starts filling in numbers from the beginning and goes as far as it can until things fall apart, at which point it goes backwards. The idea of our function is that we’ll find all of the places where the puzzle is blank (0) and only consider those. We’ll keep track of which position, pos , we’re on among those places where there are zeros . We’ll loop until all of the zeros are filled in: • Whatever number we’re currently at, add one. If we’re seeing a number for the first time, it’ll be zero, so we’ll try 1 first. Otherwise, if we return to a number, whatever was there previously must not have worked. We don’t want to start from 1 again since we already discovered those numbers don’t work. We pick up where we left off. • If, by adding one, we’re past 9, the previous positions must be wrong since 1-9 all didn’t work here. Therefore, move backwards. • Otherwise, if whatever number we just put in this spot works (yes, that’s a long test), move forwards. function p = sudokuiterativeshort(p) [zr zc] = find(p==0); pos = 1; while(pos<=length(zr)) p(zr(pos),zc(pos)) = p(zr(pos),zc(pos))+1; if(p(zr(pos),zc(pos))>9) p(zr(pos),zc(pos)) = 0; pos = pos - 1; elseif(sum(p(zr(pos),:)==p(zr(pos),zc(pos)))==1 & sum(p(:,zc(pos))==p(zr(pos),zc(pos)))==1 & sum(sum(p((3*(ceil(zr(pos)/3))-2):(3*(ceil(zr(pos)/3))), (3*(ceil(zc(pos)/3))-2):(3*(ceil(zc(pos)/3))))==p(zr(pos),zc(pos))))==1) pos = pos + 1; end end 1.1 Depth First Search vs. Brute Force A brute force algorithm would have tried all of the possibilities for each spot, as below. (Actually, here, I’m randomly trying possibilities, but it’s a similar idea): 1 function p = sudokurand(p) z = find(p==0); doesntwork = 1; while(doesntwork) doesntwork = 0; p(z) = ceil(9*rand(1,length(z))); for g = 1:9 if(length(unique(p(g,:)))~=9 | length(unique(p(:,g)))~=9 | length(unique(reshape(p((ceil(g/3)*3-2):(ceil(g/3)*3), ((rem(g,3)+1)*3-2):((rem(g,3)+1)*3)),1,9)))~=9) doesntwork = 1; break end end end Note that this is not a good way to code a solution to this problem. I started running this on an easy puzzle, went and had dinner at Cinco de Mayo, came back, wrote these lecture notes... and it still hasn’t solved an easy puzzle. 2 One more GUI function Want to have the user choose a file on their computer? [f path] = uigetfile; %% now, [path f] is the full location of the file 3 Recursion There’s a very interesting technique in computer programming called recursion, in which you write a function that calls a variation of itself to get the answer. Eventually, this string of functions calling itself stops at some base case , and the answers work their way back up. Of course, in order to understand recursion, you must understand, and the answers work their way back up....
View Full Document

{[ snackBarMessage ]}

Page1 / 10

Lecture 11 - 14:440:127– Introduction to Computers for...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online