% 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 = 4; A = zeros(N); % attackedPositions = zeros(N); positions = 1 : N^2; solutions = []; no_solutions = []; solutionsNoAttack = []; %for numQueens = 1 : N % i represents the number of queens on the board in the current step % uncomment the previous line and comment out the next line and the program % will find the solutions for all number of queens. numQueens = 3; 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; numAttacks = find_attacks_gen(A); if numAttacks == 0 solutionsNoAttack(:, :, end+1) = A; end else no_solutions(:, :, end+1) = A; end A = zeros(N); % if (numel(solutionsNoAttack)>0) % to find all solutions % break; % comment out these % end % three lines. end %end solutions(:,:,1) = []; no_solutions(:, :, 1) = []; solutionsNoAttack(:, :, 1) = [];