在MATLAB中生成随机的'su doku'类型矩阵 [英] Generating random 'su doku' type matrices in MATLAB

查看:90
本文介绍了在MATLAB中生成随机的'su doku'类型矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在MATLAB中生成一些5x6矩阵.它们需要由1-6范围内的随机生成的整数组成,但是,整数在特定的行或列中不能出现一次以上.

I need to generate some 5x6 matrices in MATLAB. They need to consist of randomly generated integers in the range 1-6, however, an integer cannot occur more than once in a particular row or column.

这是我当前用于生成5x6随机矩阵的脚本:

Here is the script I am currently using to generate random 5x6 matrices:

mat=zeros(5,6);

rows=5;
columns=6;
for i=1:rows
  for j=1:columns
      mat(i,j)=round(rand*(high-low)+low);
  end
end
disp(mat)

但是我不知道如何在其中插入有关重复的规则.

But I don't know how to insert the rule about repeats into this.

我确定这是一个相对简单的问题,但是我对MATLAB非常陌生,还无法生成满足这些条件的东西.我将非常感谢任何人都能提供的任何帮助.

I'm sure this is a relatively simple problem but I'm very new to MATLAB and haven't been able to generate something that satisfies these conditions. I'd be greatful for any assistance anyone can give.

推荐答案

不要尝试一次用完全随机的整数填充矩阵.成为有效拼图网格的可能性正在逐渐降低.

Don't try to fill the matrix with completely random ints all at once. The likelihood of that being a valid puzzle grid is vanishingly low.

相反,使用与Sudoku生成器相同的方法-从空白矩阵开始并根据您的规则一次填充一个元素.

Instead, use the same method as used by Sudoku generators - start with a blank matrix and fill in elements one at a time, as restricted by your rules.

如果条目有多个选择,请随机选择其中之一.

Where you have more than one choice for the entry, pick one of them at random.

您可能会进行如下操作(为简便起见,示例为4x4-允许的数字1-4)

You might progress something like this (4x4 example for brevity - allowable numbers 1-4)

x x x x
x x x x
x x x x
x x x x

按骰子选择第一个数字:3.

Pick first number by dice roll: 3.

3 x x x
x x x x
x x x x
x x x x

从允许的数字列表中选择第二个数字:[1、2、4].

Pick second number from list of allowable numbers: [1, 2, 4].

3 1 x x
x x x x
x x x x
x x x x

从允许的数字列表中选择第三个数字,[1,4]:

Pick third number from list of allowable numbers, [1, 4]:

3 1 4 x
x x x x
x x x x
x x x x

以此类推.

如果在某个插入步骤中的允许数字列表"为空集,则无法挽回矩阵,您可能需要重新开始.

If your "list of allowable numbers" at some insertion step is an empty set, then your matrix can't be salvaged and you may need to start again.

此外,显然不可能有5个唯一整数的10x10矩阵-插入一些逻辑来测试这种微不足道的错误情况.

Also a 10x10 matrix with 5 unique integers is clearly impossible - insert some logic to test for this trivial error case.

由于它不是传统意义上的作业,而且是一个有趣的问题....

Since it's not homework in the traditional sense, and since it was an interesting problem....

function arena = generate_arena(num_rows, num_cols, num_symbols)
    % Generate an "arena" by repeatedly calling generate_arena_try
    % until it succeeds.
    arena = 0;
    number_of_tries = 0;
    while ~(arena)
        arena = generate_arena_try(num_rows, num_cols, num_symbols);
        number_of_tries = number_of_tries + 1;
    end
    sprintf('Generating this matrix took %i tries.', number_of_tries)
end

function arena = generate_arena_try(num_rows, num_cols, num_symbols)
    % Attempts to generate a num_rows by num_cols matrix of random integers
    % from the range 1:num_symbols, with no symbols repeated in each row or
    % column.
    %
    % returns 0 on failure, or the random matrix if it succeeds.

    arena = zeros(num_rows, num_cols);
    symbols = 1:num_symbols;
    for n = 1:num_rows
        for m = 1:num_cols
           current_row = arena(n,:);
           current_col = arena(:,m);
           % find elements in $symbols that are not in the current row or col
           choices = setdiff ( symbols, [current_row current_col'] );
           if isempty(choices)
               arena = 0;
               return;
           end
           % Pick one of the valid choices at random.
           arena(n,m) = choices(randi(length(choices)));
        end
    end
    return;
end

调用和输出类似于:

>> generate_arena(5,6,6)

ans =

Generating this matrix took 5 tries.


ans =

     2     3     6     4     5     1
     6     1     5     3     4     2
     1     5     4     2     6     3
     4     6     2     1     3     5
     3     4     1     5     2     6

别说我什么都没给你. ;)

Don't say I never gave you nothing. ;)

这篇关于在MATLAB中生成随机的'su doku'类型矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆