diff options
author | elioat <elioat@tilde.institute> | 2024-06-25 22:54:00 -0400 |
---|---|---|
committer | elioat <elioat@tilde.institute> | 2024-06-25 22:54:00 -0400 |
commit | e855043419a9c37ae0c13a9a7de386205416ac37 (patch) | |
tree | 5481bf1bacd93580a3899a8ab12a185fa8bb5f71 /awk/nqueens.awk | |
parent | 565796ff19a25fef7111d2be5ab3fbb86c84213b (diff) | |
download | tour-e855043419a9c37ae0c13a9a7de386205416ac37.tar.gz |
*
Diffstat (limited to 'awk/nqueens.awk')
-rw-r--r-- | awk/nqueens.awk | 45 |
1 files changed, 45 insertions, 0 deletions
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 +} |