From e855043419a9c37ae0c13a9a7de386205416ac37 Mon Sep 17 00:00:00 2001 From: elioat Date: Tue, 25 Jun 2024 22:54:00 -0400 Subject: * --- awk/nqueens.awk | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 awk/nqueens.awk diff --git a/awk/nqueens.awk b/awk/nqueens.awk new file mode 100644 index 0000000..894cc4d --- /dev/null +++ b/awk/nqueens.awk @@ -0,0 +1,45 @@ +# Check if a position is safe for a queen +function is_safe(board, row, col, N) { + for (i = 1; i < row; i++) { + # Check column and diagonals + if (board[i] == col || board[i] - i == col - row || board[i] + i == col + row) { + return 0 + } + } + return 1 +} + +# Print the board configuration +function print_board(board, N) { + for (i = 1; i <= N; i++) { + for (j = 1; j <= N; j++) { + if (board[i] == j) { + printf "Q " + } else { + printf ". " + } + } + printf "\n" + } + printf "\n" +} + +function solve(board, row, N) { + if (row > N) { + # Print the board configuration when a solution is found + print_board(board, N) + return + } + + for (col = 1; col <= N; col++) { + if (is_safe(board, row, col, N)) { + board[row] = col + solve(board, row + 1, N) + } + } +} + +BEGIN { + N = 8 # Change this value for different board sizes + solve(board, 1, N) # FIXME: dawg, this don't be working +} -- cgit 1.4.1-2-gfad0