mirror of
https://github.com/luau-lang/luau.git
synced 2024-11-15 22:35:43 +08:00
d01addc625
Co-authored-by: Rodactor <rodactor@roblox.com>
77 lines
2.2 KiB
Lua
77 lines
2.2 KiB
Lua
--[[
|
|
MIT License
|
|
|
|
Copyright (c) 2017 Gabriel de Quadros Ligneul
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|
|
]]
|
|
local bench = script and require(script.Parent.bench_support) or require("bench_support")
|
|
|
|
function test()
|
|
|
|
local N = tonumber((arg and arg[1]) or 8) -- board size
|
|
|
|
|
|
-- check whether position (n,c) is free from attacks
|
|
local function isplaceok (a, n, c)
|
|
for i = 1, n - 1 do -- for each queen already placed
|
|
if (a[i] == c) or -- same column?
|
|
(a[i] - i == c - n) or -- same diagonal?
|
|
(a[i] + i == c + n) then -- same diagonal?
|
|
return false -- place can be attacked
|
|
end
|
|
end
|
|
return true -- no attacks; place is OK
|
|
end
|
|
|
|
|
|
-- print a board
|
|
local function printsolution (a)
|
|
for i = 1, N do
|
|
for j = 1, N do
|
|
--print(a[i] == j and "X" or "-", " ")
|
|
end
|
|
--print("\n")
|
|
end
|
|
--print("\n")
|
|
end
|
|
|
|
|
|
-- add to board 'a' all queens from 'n' to 'N'
|
|
local function addqueen (a, n)
|
|
if n > N then -- all queens have been placed?
|
|
printsolution(a)
|
|
else -- try to place n-th queen
|
|
for c = 1, N do
|
|
if isplaceok(a, n, c) then
|
|
a[n] = c -- place n-th queen at column 'c'
|
|
addqueen(a, n + 1)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
|
|
-- run the program
|
|
addqueen({}, 1)
|
|
|
|
end
|
|
|
|
bench.runCode(test, "queen")
|