Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 867001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:54:12+00:00 2026-05-15T09:54:12+00:00

main.m counter = 1; n = 8; board = zeros(1,n); back(0, board); disp(counter); sol.m

  • 0

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

I’m attempting to find the maximum number of ways n-queen can be placed on an n-by-n board such that those queens aren’t attacking eachother. I cannot figure out the problem with the above matlab code, i doubt it’s a problem with my logic since i’ve tested out this logic in java and it seems to work perfectly well there. The code compiles but the issue is that the results it produces are erroneous.

Java Code which works:

               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);
                    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:54:12+00:00Added an answer on May 15, 2026 at 9:54 am

    Worked code:

    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
    

    I add line if ~any(board==i), without this check, I think your java solution slower than this Matlab code. For fastest solution google “Dancing links”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.