function N = bestBeamNeighbour(X)

%% return a matrix N that represent the next best move given the initial
%% configuration X for an n-queen problem and a beam search; Each row of X
%% encodes a state and the number of row are the number of states for the
%% beam search (e.g., k)

N = X; %%the next state
k=size(X,1);
n=size(X,2);
MT = zeros(k*n,n);
MTMapS = zeros(k*n,1); %%mapping to select state from which MT rows are generated
MTMapA = zeros(k*n,1); %%mapping to select state from which MT rows are generated
for r=1:k
    M = generateConflMatrix(X(r,:));
    c=1+n*(r-1);
    MT(c:c+n-1,:) = M;
    MTMapS(c:c+n-1) = r;
    MTMapA(c:c+n-1) = [1:n]';
end
MT
for r=1:k
    [v,h] = min(MT);
    [~,i] = min(v);
    [~,j] = min(MT(:,i));
    N(r,:) = X(MTMapS(h(i)),:);    
    N(r,i) = MTMapA(j);
    MT(h(i),:)=[];
    MTMapS(h(i),:)=[];
    MTMapA(h(i),:)=[];
end


