简体   繁体   中英

Matlab: N queens (checking 2 table's symmetry)

I am trying to make a simple script that can decide whether two chess boards are similar or identical or different. I have to test it if it was the same size first (3x3 for example) because if not I shouldn't run the test in the first place. I also have to do a vertical and horizontal flip to the first board and compare them to the second board and see if any of the two flips match the second board. This is the code that I have so far:

function similar = symmetric_queens(b1, b2) %b1 is table 1 and b2 is table 2
similar= true;
h1= flipud(b1); %flipping about horizontal axis
v1= fliplr(b1); %flipping about vertical axis
if
     b1==b2;
     imshow('The boards are identical')  %because they are exactly the same
end
    if 
        size(b1)==size(b2)  %if they are of equal size test if horizontal flip= second board
        h1=b2  
        imshow('true')
else
    v1=h2 %test if vertical flip = second board
    imshow('true')

else              %in these two codes I am trying to check make the function stop if the boards arent the same size
 size(b1)>size(b2)  
    similar = false;                 
    return;                        

else
    size(b1)<size(b2)
    similar = false;                   
    return;                         
end
end

I think there are lots of mistakes which are causing my code not to wrk, any help would be highly appreciated. I am sorry if I didn't post this right or if I did anything wrong as this is my first time posting in here.

Thanks in advance!

Little bit organized code I would think to do the same thing -

function similar = symmetric_queens(b1, b2) %b1 is table 1 and b2 is table 2

%%// Check for equal sizes
if ~isequal(size(b1),size(b2))
    similar = false;
    return;
end

if isequal(b2,b1) %%// Check for perfect match
    disp('The boards are identical');
    similar = true; %%// What to return for - similar value when they are identical? Assumed - true
elseif isequal(b2,fliplr(b1)) || isequal(b2,flipud(b1)) %%// Check horizontal and vertical flip
    disp('The boards are similar');
    similar = true;
else
    similar = false;
end

return;

Note: I wasn't sure why you have used imshow . Maybe you meant displaying the text? That's what the code would do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM