Matlab N-Queen问题

时间:2010-06-10 21:04:29

标签: matlab n-queens

main.m

counter = 1;
n = 8;
board = zeros(1,n);
back(0, board);
disp(counter);

sol.m

function value = sol(board)

for ( i = 1:(length(board)))
   for ( j = (i+1): (length(board)-1))
       if (board(i) == board(j))
          value = 0;
          return;
       end
       if ((board(i) - board(j)) == (i-j))
          value = 0;
          return;
       end
       if ((board(i) - board(j)) == (j-i))
          value = 0;
          return;
       end
   end
end
value = 1;
return;

back.m

function back(depth, board)

disp(board);

if ( (depth == length(board)) && (sol2(board) == 1))
    counter = counter + 1;
end

if ( depth < length(board))
   for ( i = 0:length(board))
       board(1,depth+1) = i;
       depth = depth + 1;
       solv2(depth, board);
   end
end

我试图找到n-queen可以放在n-by-n板上的最大数量,这样那些皇后不会互相攻击。我无法弄清楚上面的matlab代码的问题,我怀疑这是我的逻辑问题,因为我已经在java中测试了这个逻辑,它似乎在那里工作得很好。代码编译但问题是它产生的结果是错误的。

有效的Java代码:

               public static int counter=0;

                public static boolean isSolution(final int[] board){
                    for (int i = 0; i < board.length; i++) {
                        for (int j = i + 1; j < board.length; j++) {
                            if (board[i] == board[j]) return false;    
                            if (board[i]-board[j] == i-j) return false; 
                            if (board[i]-board[j] == j-i) return false; 
                        }
                    }
                    return true;
                }

                public static void solve(int depth, int[] board){

                    if (depth == board.length && isSolution(board)) {
                        counter++;
                 }
               if (depth < board.length) {  // try all positions of the next row

                for (int i = 0; i < board.length; i++) {
                            board[depth] = i;
                            solve(depth + 1, board);
                        }
                    }
                }


                    public static void main(String[] args){
                        int n = 8;
                        solve(0, new int[n]);
                    System.out.println(counter);
                    }

2 个答案:

答案 0 :(得分:2)

您的代码存在许多问题。

以下是一些:

  • 您将board初始化为1 x 8阵列
  • 您调用未定义的函数sol2solv2
  • 没有为solv2back捕获输出(请记住,Matlab按值传递变量,而不是通过引用传递变量)
  • 代码中没有任何评论可以解释您认为自己在做什么以及为什么要这样做。

另外:虽然Matlab有一个JIT编译器,除其他外,有助于加速循环,Matlab代码不能说是“编译”(除非你做了一些非常错误的事情)。

答案 1 :(得分:2)

工作代码:

function queen
clc;
counter = 0;
n = 8;
board = zeros(1,n);
[board,counter] = back(1, board,counter);
fprintf('Solution count: %d\n',counter);
%%
function value =  isSolution(board)

for  i = 1:length(board)
   for  j = (i+1): length(board)
       if abs(board(i) - board(j)) == abs(i-j)
          value = false;
          return;
       end      
   end
end
value = true;
%%
function [board,counter] =  back(depth, board,counter)

if  (depth == length(board)+1) && isSolution(board)
    counter = counter + 1;
    disp(board);
end

if ( depth <= length(board))
   for  i = 1:length(board)
       if ~any(board==i)
           board(1,depth) = i;           
           [~,counter] = back(depth+1, board,counter);
       end       
   end
end

我添加行如果〜任何(board == i),没有这个检查,我认为你的java解决方案比这个Matlab代码慢。对于最快的解决方案谷歌“跳舞链接”。

相关问题