% This program solves the dominant queens % problem for the 4x4 board using brute force method. % It starts by placing one queen (on all places) and checking the % number of attacked positions. Then it places two queens % (tries all possibilities) and checks the attacked positions, % and so on until the solution is found. % The solution is found when all positions on the board % are attacked. clear clc N = 2; A = zeros(N); % attackedPositions = zeros(N); positions = 1 : N^2; solutions = []; no_solutions = []; for numQueens = 1 : N % i represents the number of queens on the board in the current step Q = nchoosek(positions, numQueens); possibilities = numel(Q) / numQueens; for i = 1 : possibilities temp = Q(i, :); A(temp) = 1; B = attackedPositions(A, N); if sum(sum(B)) == N^2 solutions(:, :, end+1) = A; else no_solutions(:, :, end+1) = A; end A = zeros(N); end end solutions(:,:,1) = []; no_solutions(:, :, 1) = [];