Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions dana/programs/N_Queens.dana
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
def main

def printBoard: board as byte[][], n as int
var i j is int
i := 0
loop:
if i >= n :
break
j := 0
loop:
if j >= n :
break
if board[i][j] = 1 :
writeString: "Q "
else :
writeString: ". "
j := j + 1
writeString: "\n"
i := i + 1
writeString: "\n"

def isSafe is byte: board as byte[][], row col n as int
var i j is int

# check columns
i := 0
loop:
if i >= row :
break
if board[i][col] = 1 :
return : 0
i := i + 1

# check left diagonal
i := row - 1
j := col - 1
loop:
if i < 0 or j < 0 :
break
if board[i][j] = 1 :
return : 0
i := i - 1
j := j - 1

# check right diagonal
i := row - 1
j := col + 1
loop:
if i < 0 or j >= n :
break
if board[i][j] = 1 :
return : 0
i := i - 1
j := j + 1

return : 1

def solveNQueensUtil is byte: board as byte[][], row n as int
if row = n :
printBoard: board, n
return : 1

var col is int
col := 0
loop:
if col >= n :
break
if isSafe: board, row, col, n :
board[row][col] := 1
if solveNQueensUtil: board, row + 1, n :
return : 1
board[row][col] := 0
col := col + 1

return : 0

def solveNQueens: n as int
var board is byte[n][n]
var i j is int

i := 0
loop:
if i >= n :
break
j := 0
loop:
if j >= n :
break
board[i][j] := 0
j := j + 1
i := i + 1

if solveNQueensUtil: board, 0, n :
writeString: "Solution found.\n"
else :
writeString: "No solution exists.\n"

var size is int
writeString: "Enter board size: "
size := readInteger()
solveNQueens: size
1 change: 1 addition & 0 deletions dana/programs/N_Queens.input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8
10 changes: 10 additions & 0 deletions dana/programs/N_Queens.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Q . . . . . . .
. . . . . . Q .
. . . . Q . . .
. . . Q . . . .
. . Q . . . . .
. Q . . . . . .
. . . . . Q . .
. . . . . . . Q

Solution found.