QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 5351|回复: 15
打印 上一主题 下一主题

每日科技报告 第17期 Cleve’s Corner: Solving Sudoku with MATLAB

[复制链接]
字体大小: 正常 放大

1253

主题

442

听众

-586

积分

复兴中华数学头子

  • TA的每日心情
    开心
    2011-9-26 17:31
  • 签到天数: 3 天

    [LV.2]偶尔看看I

    自我介绍
    数学中国网站(www.madio.cn)是目前中国最大的数学建模交流社区

    邮箱绑定达人 优秀斑竹奖 发帖功臣 元老勋章 新人进步奖 原创写作奖 最具活力勋章 风雨历程奖

    群组越狱吧

    群组湖南工业大学数学建模同盟会

    群组四川农业大学数学建模协会

    群组重庆交通大学数学建模协会

    群组中国矿业大学数学建模协会

    跳转到指定楼层
    1#
    发表于 2010-1-30 03:46 |只看该作者 |倒序浏览
    |招呼Ta 关注Ta |邮箱已经成功绑定
    Cleve’s Corner: Solving Sudoku with MATLAB
    By Cleve Moler
    Human puzzle-solvers and computer programs use very different Sudoku-solving techniques. The fascination with solving Sudoku by hand derives from the discovery and mastery of a myriad of subtle combinations and patterns that provide hints about the final solution. It is not easy to program a computer to duplicate these human pattern-recognition capabilities. For this reason, most Sudoku-solving programs take a very different approach, relying on the computer’s almost limitless capacity to carry out brute-force trial and error. That is the approach that I used for the MATLAB® program. The Sudoku Challenge
    As you probably know, solving a Sudoku involves filling in a 9-by-9 grid so that each row, column, and major 3-by-3 block contains all the digits 1 through 9. The initial grid is populated with a few digits, known as clues. In contrast to magic squares and other numeric puzzles, no arithmetic is involved; the elements in a Sudoku grid could just as well be letters of the alphabet or any other symbols.
    Figure 1 shows an initial grid. I especially like the symmetry in this example, which is due to Gordon Royle of the University of Western Australia. Figure 2 shows the solution.
    Figure 1. An example puzzle, with the clues shown in blue. This example has especially pleasing symmetry. Figure 2. The completed puzzle. The other digits have been inserted so that each row, column, and major 3-by-3 block contains the digits 1 through 9.
    Solving Sudoku with Recursive Backtracking
    Our MATLAB program uses only one pattern—singletons—together with a basic computer science technique, recursive backtracking.
    To see how the program works, we can use a **r 4-by-4 grid with 2-by-2 blocks. Such puzzles are called Shidoku instead of Sudoku because “Shi” is Japanese for “four.”
    Figure 3 shows our first Shidoku puzzle. Figures 4 through 6 show its solution. In Figure 4, the possible entries, or candidates, are represented as small digits. For example, row two contains a “3” and column one contains a “1”, so the candidates in position (2,1) are “2” and “4.” Four of the cells contain only one candidate. These cells are the singletons. This first example can be solved easily by filling in the singletons. In Figure 5, we have inserted one of the singletons and recomputed the candidates. In Figure 6, we have inserted the remaining singletons as they appear to complete the solution.
    Figure 3. Shidoku is Sudoku on a 4-by-4 grid. Figure 4. The candidates. The red candidates are the singletons.
    cc_fig5_w.gif
    Figure 5. Inserting the singleton “3” and recomputing the candidates. Figure 6. Insert the remaining singletons to complete the puzzle.

    An easy puzzle could be defined as one that can be solved by just inserting the singletons. Within this definition, our first example is easy, but the next example is not.
    The input array for the puzzle shown in Figure 7 is generated by the MATLAB statement  X = diag(1:4)
    cc_fig7_w.gif
    Figure 7. shidoku(diag(1:4))

    Because there are no singletons in this puzzle (Figure 8), we will use recursive backtracking. We select one of the empty cells and tentatively insert one of its candidates. We have chosen to consider the cells in the order implied by MATLAB one-dimensional subscripting, X(, and try the candidates in numerical order. So we insert a “3” in cell (2,1), creating a new puzzle (Figure 9). We then call the program recursively.
    Figure 8. The candidates. There are no singletons. Figure 9. Tentatively insert a “3” to create a new puzzle. Then backtrack.

    The new puzzle is easy; the result is shown in Figure 10. However, this solution depends upon the choices that we made before the recursive call. Other choices could produce different solutions. For this ** diagonal initial condition, there are two possible solutions, which happen to be matrix transposes of each other. Since the solution is not unique, the grid in Figure 7 is not a valid puzzle.
    Figure 10. The resulting solution. This solution is not unique; its transpose is another solution.
    Existence and Uniqueness
    As mathematicians, we seek to prove that a solution to a problem exists, and that it is unique. With Sudoku, neither existence nor uniqueness can easily be determined from the initial clues. For example, with the puzzle shown in Figure 1, if we were to insert a “1”, “5,” or “7” in the (1,1) cell, the row, column, and block conditions would be satisfied but the resulting puzzle would have no solution. It would be very frustrating if such a puzzle were to show up in your news**.
    Backtracking generates many impossible configurations. Our program terminates the recursion when it encounters a cell that has no candidates. Such puzzles have no solution.
    Uniqueness is an elusive property. Most descriptions of Sudoku do not specify that there must be only one solution. Again, it would be frustrating to discover a solution different from the one given. Some of the puzzle-generating programs on MATLAB Central do not check uniqueness. The only way that I know to check for uniqueness is to exhaustively enumerate all possible solutions. The Sudoku-Solving Algorithm
    Our MATLAB program involves just four steps:
    1. Fill in all singletons.
    2. Exit if a cell has no candidates.
    3. Fill in a tentative value for an empty cell.
    4. Call the program recursively. The key internal function is candidates. Each empty cell starts with z = 1:9 and uses the numeric values in the associated row, column, and block to zero elements in z. The nonzeros that remain are the candidates. For example, consider the (1,1) cell in Figure 1. We start with
    z = 1 2 3 4 5 6 7 8 9 The values in the first row change z to  z = 1 0 0 0 5 6 7 8 9 Then the first column changes z to
    z = 1 0 0 0 5 0 7 0 9The (1,1) block does not make any further changes, so the candidates for this cell are
    C{1,1} = [1 5 7 9]
    A Difficult Puzzle
    The puzzle shown in Figure 1 is actually very difficult to solve, either by hand or by computer. Figures 11 and 12 are snapshots of the computation. Initially, there are no singletons, so the first recursive step happens immediately. We try a “1” in the (1,1) cell. Figure 11 shows how the first column is then filled by step 22. But we’re still a long way from the solution. After 3,114 steps, the recursion puts a “5” in the (1,1) cell, and after 8,172 steps, it tries a “7.”
    cc_fig12_w.gif
    Figure 11. The situation after just 22 steps towards the solution of Figure 1. Values colored cyan are tentative choices made by the backtracking, and values colored green are the singletons implied by those choices. The “1” is the wrong choice for the (1,1) cell. Click on image to see enlarged view. Figure 12. After 14,781 steps, we appear to be close to a solution, but it is impossible to continue because there are no candidates for cell (1,9). The “7” is the wrong choice for the (1,1) cell. Click on image to see enlarged view.

    Figure 12 shows the situation after 14,781 steps. We appear to be close to a solution because 73 of the 81 cells have been assigned values. But the first row and last column, taken together, contain all the digits from 1 through 9, so there are no values left for the (1,9) cell in the upper right corner. The candidate list for this cell is empty, and the recursion terminates. Finally, after 19,229 steps, we try a “9” in the first cell. This “9” is a good idea because less than 200 steps later, after 19,422 steps, the program reaches the solution shown in Figure 2. This is many more steps than most puzzles require.
    Solving Sudoku Using Recursive Backtracking
    1. function X = sudoku(X)
    2. % SUDOKU  Solve Sudoku using recursive backtracking.
    3. %   sudoku(X), expects a 9-by-9 array X.
    4. % Fill in all “singletons”.
    5. % C is a cell array of candidate vectors for each cell.
    6. % s is the first cell, if any, with one candidate.
    7. % e is the first cell, if any, with no candidates.
    8. [C,s,e] = candidates(X);
    9. while ~isempty(s) && isempty(e)
    10.     X(s) = C{s};
    11.     [C,s,e] = candidates(X);
    12. end
    13. % Return for impossible puzzles.
    14. if ~isempty(e)
    15.     return
    16. end
    17. % Recursive backtracking.
    18. if any(X(:) == 0)
    19.     Y = X;
    20.     z = find(X(:) == 0,1);    % The first unfilled cell.
    21.     for r = [C{z}]            % Iterate over candidates.
    22.        X = Y;
    23.        X(z) = r;              % Insert a tentative value.
    24.        X = sudoku(X);         % Recursive call.
    25.        if all(X(:) > 0)       % Found a solution.
    26.           return
    27.        end
    28.      end
    29.    end
    30. % ------------------------------
    31.    function [C,s,e] = candidates(X)
    32.       C = cell(9,9);
    33.       tri = @(k) 3*ceil(k/3-1) + (1:3);
    34.       for j = 1:9
    35.          for i = 1:9
    36.             if X(i,j)==0
    37.                z = 1:9;
    38.                z(nonzeros(X(i,:))) = 0;
    39.                z(nonzeros(X(:,j))) = 0;
    40.                z(nonzeros(X(tri(i),tri(j)))) = 0;
    41.                C{i,j} = nonzeros(z)’;
    42.             end
    43.          end
    44.       end
    45. L = cellfun(@length,C);   % Number of candidates.
    46. s = find(X==0 & L==1,1);
    47. e = find(X==0 & L==0,1);
    48. end % candidates
    49. end % sudoku
    The Origins of Sudoku
    Most people assume that Sudoku originated in Japan. In fact, it is an American invention. It first appeared, with the name Number Place, in Dell Puzzle Magazine in 1979. In 1984, a Japanese publisher, Nikoli, took the puzzle to Japan and gave it the name Sudoku, which is a kanji acronym for “numbers should be single, unmarried.” The Times of London began publishing the puzzle in 2004, and it was not long before it spread to the U.S. and around the world.

    cc_fig11_wl.gif (79.2 KB, 下载次数: 49)

    cc_fig11_wl.gif

    cc_fig12_wl.gif (78.1 KB, 下载次数: 37)

    cc_fig12_wl.gif

    zan
    转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持1 反对反对0 微信微信
    数学中国网站是以数学中国社区为主体的综合性学术社区,下分建模、编程、学术理论、工程应用等版块。从2003年11月建站以来一直致力于数学建模的普及和推广工作,目前已经发展成国内会员最多,资源最丰富,流量最大的数学建模网络平台。我们始终秉承服务大众的理念,坚持资源共享、共同进步的原则,努力营造出严肃、认真、务实、合作的学术氛围,为中国数学的发展做出应有的贡献。

    1253

    主题

    442

    听众

    -586

    积分

    复兴中华数学头子

  • TA的每日心情
    开心
    2011-9-26 17:31
  • 签到天数: 3 天

    [LV.2]偶尔看看I

    自我介绍
    数学中国网站(www.madio.cn)是目前中国最大的数学建模交流社区

    邮箱绑定达人 优秀斑竹奖 发帖功臣 元老勋章 新人进步奖 原创写作奖 最具活力勋章 风雨历程奖

    群组越狱吧

    群组湖南工业大学数学建模同盟会

    群组四川农业大学数学建模协会

    群组重庆交通大学数学建模协会

    群组中国矿业大学数学建模协会

    回复

    使用道具 举报

    olh2008 实名认证       

    88

    主题

    42

    听众

    1万

    积分

    船长

  • TA的每日心情
    开心
    2018-9-1 14:36
  • 签到天数: 86 天

    [LV.6]常住居民II

    邮箱绑定达人 优秀斑竹奖 新人进步奖 发帖功臣 最具活力勋章 元老勋章 原创写作奖 风雨历程奖

    群组Latex研学群

    群组数学建模

    群组Mathematica研究小组

    群组LINGO

    群组Matlab讨论组

    回复

    使用道具 举报

    52121wolf 实名认证       

    0

    主题

    4

    听众

    129

    积分

    升级  14.5%

    该用户从未签到

    自我介绍
    我是一个学生 好好学习 天天向上
    回复

    使用道具 举报

    mnpfc 实名认证      会长俱乐部认证 

    131

    主题

    38

    听众

    1万

    积分

    升级  0%

  • TA的每日心情
    开心
    2018-12-4 08:49
  • 签到天数: 282 天

    [LV.8]以坛为家I

    邮箱绑定达人 新人进步奖 最具活力勋章 风雨历程奖 元老勋章

    群组2010MCM

    群组数学建模

    群组中国矿业大学数学建模协会

    群组华中师大数模协会

    群组Mathematica研究小组

    回复

    使用道具 举报

    wqt2958 实名认证       

    9

    主题

    5

    听众

    267

    积分

    升级  83.5%

  • TA的每日心情
    开心
    2016-11-14 10:57
  • 签到天数: 11 天

    [LV.3]偶尔看看II

    自我介绍
    相信自己,不再犹豫~

    新人进步奖

    群组数学建摸协会

    回复

    使用道具 举报

    wqt2958 实名认证       

    9

    主题

    5

    听众

    267

    积分

    升级  83.5%

  • TA的每日心情
    开心
    2016-11-14 10:57
  • 签到天数: 11 天

    [LV.3]偶尔看看II

    自我介绍
    相信自己,不再犹豫~

    新人进步奖

    群组数学建摸协会

    回复

    使用道具 举报

    bb_wang 实名认证       

    18

    主题

    4

    听众

    2170

    积分

    学生

    该用户从未签到

    新人进步奖

    真的好强大,学习数学建模模型和将模型运用到赛题中解决实际问题还是有很大差距的,努力~~~
    回复

    使用道具 举报

    32

    主题

    6

    听众

    1077

    积分

  • TA的每日心情
    开心
    2011-12-15 19:30
  • 签到天数: 1 天

    [LV.1]初来乍到

    自我介绍
    活泼开朗,数学是兴趣,不是专业

    新人进步奖

    回复

    使用道具 举报

    nwpucrh 实名认证       

    3

    主题

    5

    听众

    153

    积分

    升级  26.5%

  • TA的每日心情
    无聊
    2014-8-14 19:11
  • 签到天数: 6 天

    [LV.2]偶尔看看I

    新人进步奖

    群组中学生数学

    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册地址

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

    关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

    手机版|Archiver| |繁體中文 手机客户端  

    蒙公网安备 15010502000194号

    Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

    GMT+8, 2025-8-5 10:57 , Processed in 3.103025 second(s), 104 queries .

    回顶部