Question

Hi there I need help writing an algorithm to solve Sudoku puzzles through integer linear programming...

Hi there I need help writing an algorithm to solve Sudoku puzzles through integer linear programming using MATLAB.

If you could please provide the code and an explanation of how it works I'd be really grateful.

Any assistance would be much appreciated, many thanks in advance!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solving Sudoku Using Recursive Backtracking

function X = sudoku(X) 
% SUDOKU  Solve Sudoku using recursive backtracking. 
%   sudoku(X), expects a 9-by-9 array X. 
 % Fill in all “singletons”. 
 % C is a cell array of candidate vectors for each cell. 
 % s is the first cell, if any, with one candidate. 
 % e is the first cell, if any, with no candidates. 
 [C,s,e] = candidates(X); 
 while ~isempty(s) && isempty(e) 
    X(s) = C{s}; 
    [C,s,e] = candidates(X); 
 end 
 % Return for impossible puzzles. 
 if ~isempty(e) 
    return 
 end 
 % Recursive backtracking. 
 if any(X(:) == 0) 
    Y = X; 
    z = find(X(:) == 0,1);    % The first unfilled cell. 
    for r = [C{z}]            % Iterate over candidates. 
       X = Y; 
       X(z) = r;              % Insert a tentative value. 
       X = sudoku(X);         % Recursive call. 
       if all(X(:) > 0)       % Found a solution. 
          return 
       end 
     end 
   end 
% ------------------------------ 
   function [C,s,e] = candidates(X) 
      C = cell(9,9); 
      tri = @(k) 3*ceil(k/3-1) + (1:3); 
      for j = 1:9 
         for i = 1:9 
            if X(i,j)==0 
               z = 1:9; 
               z(nonzeros(X(i,:))) = 0; 
               z(nonzeros(X(:,j))) = 0; 
               z(nonzeros(X(tri(i),tri(j)))) = 0; 
               C{i,j} = nonzeros(z)’; 
            end 
         end 
      end 
 L = cellfun(@length,C);   % Number of candidates. 
 s = find(X==0 & L==1,1); 
 e = find(X==0 & L==0,1); 
 end % candidates 
end % sudoku
Add a comment
Know the answer?
Add Answer to:
Hi there I need help writing an algorithm to solve Sudoku puzzles through integer linear programming...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi, I have Java programming problem: Please solve using Java. Thanks. Best Regards. In the army,...

    Hi, I have Java programming problem: Please solve using Java. Thanks. Best Regards. In the army, each soldier has an assigned rank. A soldier of rank X has to report to (any) soldier of rank X + 1. Many soldiers can report to the same superior. Write a function: class Solution { public int solution(int[] ranks); } that, given an array ranks consisting of soldiers' ranks, returns the number of soldiers who can report to some superior. Examples: 1. Given...

  • C++ Programming Hi! Sorry for all the material attached. I simply need help in writing the...

    C++ Programming Hi! Sorry for all the material attached. I simply need help in writing the Facility.cpp file and the other files are included in case they're needed for understanding. I was able to complete the mayday.cpp file but am stuck on Facility. The following link contains a tar file with the files provided by the professor. Thank you so much in advanced! http://web.cs.ucdavis.edu/~fgygi/ecs40/homework/hw4/ Closer.h: #ifndef CLOSER_H #define CLOSER_H #include <string> #include "gcdistance.h" struct Closer { const double latitude, longitude;...

  • Programming Traversal Methods in C++ (depth first & breadth first) Need solution ASAP any help is...

    Programming Traversal Methods in C++ (depth first & breadth first) Need solution ASAP any help is much appreciated. read a set of data representing a directed, unweighted graph build an in-memory graph structure using the data display the graph using depth-first traversal display the graph using breadth-first traversal Input data - The data consists of records like this: 16 3 15 4 -1 This represents a vertex 16 with neighbors 3, 15, and 4. The -1 is the indicator that...

  • Need C programming help. I've started to work on the program, however I struggle when using...

    Need C programming help. I've started to work on the program, however I struggle when using files and pointers. Any help is appreciated as I am having a hard time comleting this code. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE 100 #define MAX_NAME 30 int countLinesInFile(FILE* fPtr); int findPlayerByName(char** names, char* target, int size); int findMVP(int* goals, int* assists, int size); void printPlayers(int* goals, int* assists, char** names, int size); void allocateMemory(int** goals, int** assists, char*** names, int size);...

  • Assignment 5 will be way different. It will be more like what you will receive in...

    Assignment 5 will be way different. It will be more like what you will receive in a programming shop. By that I mean that some things are built for you and others you will need to create or finish. P.S. part of your grade will include: Did you add in the required files? Did you rename your project? does your linear searches work? Did you put your code in the correct modules? did you change modules that you weren't supposed...

  • Hi there! I need to compare two essay into 1 essay, and make it interesting and...

    Hi there! I need to compare two essay into 1 essay, and make it interesting and choose couple topics which im going to talk about in my essay FIRST ESSAY “Teaching New Worlds/New Words” bell hooks Like desire, language disrupts, refuses to be contained within boundaries. It speaks itself against our will, in words and thoughts that intrude, even violate the most private spaces of mind and body. It was in my first year of college that I read Adrienne...

  • I need help with my very last assignment of this term PLEASE!!, and here are the instructions: After reading Chapter T...

    I need help with my very last assignment of this term PLEASE!!, and here are the instructions: After reading Chapter Two, “Keys to Successful IT Governance,” from Roger Kroft and Guy Scalzi’s book entitled, IT Governance in Hospitals and Health Systems, please refer to the following assignment instructions below. This chapter consists of interviews with executives identifying mistakes that are made when governing healthcare information technology (IT). The chapter is broken down into subheadings listing areas of importance to understand...

  • Identify 8 issues in regards to Recruitment and Selection, 3 issues for Labour Relation in the...

    Identify 8 issues in regards to Recruitment and Selection, 3 issues for Labour Relation in the following case study : You have recently been hired as an HR Consultant in the new HR Department of Outrage Video Games. Outrage is a five year old, upstart company, run by two very bright young men - Will Bates – President, and his best friend Steve Cobbs, Vice President. This is a very exciting change for you because Outrage, which literally started in...

  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

  • Actions that damage a company and its employees should be stamped out, everyone would agree. But ...

    Actions that damage a company and its employees should be stamped out, everyone would agree. But should the people responsible be stamped out, too? HBR CASE STUDY The Reign of Zero Tolerance by Ben Gerson "Mr. Pemberton?" manager. The guards had radioed her that the "Yes, that's me," Simon replied distractedly, his back turned. target wasn't putting up much resistance. "Your personal belongings will be messen The two burly gentlemen who had suddenly gered to your home later today," Sallie...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT