How to resolve the algorithm N-queens problem step by step in the Lua programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm N-queens problem step by step in the Lua programming language

Table of Contents

Problem Statement

Solve the eight queens puzzle.

You can extend the problem to solve the puzzle with a board of size   NxN. For the number of solutions for small values of   N,   see   OEIS: A000170.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm N-queens problem step by step in the Lua programming language

Source code in the lua programming language

N = 8

-- We'll use nil to indicate no queen is present.
grid = {}
for i = 0, N do
  grid[i] = {}
end

function can_find_solution(x0, y0)
  local x0, y0 = x0 or 0, y0 or 1  -- Set default vals (0, 1).
  for x = 1, x0 - 1 do
    if grid[x][y0] or grid[x][y0 - x0 + x] or grid[x][y0 + x0 - x] then
      return false
    end
  end
  grid[x0][y0] = true
  if x0 == N then return true end
  for y0 = 1, N do
    if can_find_solution(x0 + 1, y0) then return true end
  end
  grid[x0][y0] = nil
  return false
end

if can_find_solution() then
  for y = 1, N do
    for x = 1, N do
      -- Print "|Q" if grid[x][y] is true; "|_" otherwise.
      io.write(grid[x][y] and "|Q" or "|_")
    end
    print("|")
  end
else
  print(string.format("No solution for %d queens.\n", N))
end


  

You may also check:How to resolve the algorithm Leap year step by step in the Zig programming language
You may also check:How to resolve the algorithm Sort stability step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Digital root step by step in the J programming language
You may also check:How to resolve the algorithm String comparison step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Loops/While step by step in the Lingo programming language