Skip to content

Instantly share code, notes, and snippets.

@Rustem
Created December 27, 2021 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rustem/27a2daf483da522b3fd99a4a568bf301 to your computer and use it in GitHub Desktop.
Save Rustem/27a2daf483da522b3fd99a4a568bf301 to your computer and use it in GitHub Desktop.
Super-Queens verify safe board layout
boolean canMove(int row, int col) {
// check we don't have any super0-queen on same row
for(int i = 0; i<col ;i++) {
if(board[row][i] == true) {
return false;
}
}
// check there is no already a queen not already on the left diag to the bottom of the (row, col)
for(int i = row, j = col; i>=0 && j >= 0; i --, j--) {
if(board[i][j] == true) {
return false;
}
}
// check there is no already a queen not already on the left diag to the topo of the (row, col)
for(int i = row, j = col; i < board.length && j >= 0; i ++, j--) {
if(board[i][j] == true) {
return false;
}
}
// check knight moves
for(int x = -2; x<=2; x++) {
for(int y = - 2; y<=2; y++) {
if(Math.abs(x - y) == 1) {
dx = row + x;
dy = col + y;
if(dx < 0 || dy < 0 || dx >= board.length || dy >= board.length) {
continue;
}
if(board[dx][dy] == true) {
return false
}
}
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment