Add OS X/Swift and Python implementations of the Eight Queens problem

This commit is contained in:
Donald Burr 2015-05-28 20:31:49 -07:00
parent 6130affabf
commit e609b323a2
5 changed files with 452 additions and 0 deletions

View file

@ -0,0 +1,247 @@
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objects = {
/* Begin PBXBuildFile section */
EACAF1A71B16BD5000F01B99 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = EACAF1A61B16BD5000F01B99 /* main.swift */; };
EACAF1AE1B16BD6700F01B99 /* Queen.swift in Sources */ = {isa = PBXBuildFile; fileRef = EACAF1AD1B16BD6700F01B99 /* Queen.swift */; };
/* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */
EACAF1A11B16BD5000F01B99 /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = /usr/share/man/man1/;
dstSubfolderSpec = 0;
files = (
);
runOnlyForDeploymentPostprocessing = 1;
};
/* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */
EACAF1A31B16BD5000F01B99 /* EightQueens */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = EightQueens; sourceTree = BUILT_PRODUCTS_DIR; };
EACAF1A61B16BD5000F01B99 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
EACAF1AD1B16BD6700F01B99 /* Queen.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Queen.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
EACAF1A01B16BD5000F01B99 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
EACAF19A1B16BD5000F01B99 = {
isa = PBXGroup;
children = (
EACAF1A51B16BD5000F01B99 /* EightQueens */,
EACAF1A41B16BD5000F01B99 /* Products */,
);
sourceTree = "<group>";
};
EACAF1A41B16BD5000F01B99 /* Products */ = {
isa = PBXGroup;
children = (
EACAF1A31B16BD5000F01B99 /* EightQueens */,
);
name = Products;
sourceTree = "<group>";
};
EACAF1A51B16BD5000F01B99 /* EightQueens */ = {
isa = PBXGroup;
children = (
EACAF1A61B16BD5000F01B99 /* main.swift */,
EACAF1AD1B16BD6700F01B99 /* Queen.swift */,
);
path = EightQueens;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
EACAF1A21B16BD5000F01B99 /* EightQueens */ = {
isa = PBXNativeTarget;
buildConfigurationList = EACAF1AA1B16BD5000F01B99 /* Build configuration list for PBXNativeTarget "EightQueens" */;
buildPhases = (
EACAF19F1B16BD5000F01B99 /* Sources */,
EACAF1A01B16BD5000F01B99 /* Frameworks */,
EACAF1A11B16BD5000F01B99 /* CopyFiles */,
);
buildRules = (
);
dependencies = (
);
name = EightQueens;
productName = EightQueens;
productReference = EACAF1A31B16BD5000F01B99 /* EightQueens */;
productType = "com.apple.product-type.tool";
};
/* End PBXNativeTarget section */
/* Begin PBXProject section */
EACAF19B1B16BD5000F01B99 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0630;
ORGANIZATIONNAME = "Donald Burr";
TargetAttributes = {
EACAF1A21B16BD5000F01B99 = {
CreatedOnToolsVersion = 6.3.2;
};
};
};
buildConfigurationList = EACAF19E1B16BD5000F01B99 /* Build configuration list for PBXProject "EightQueens" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
en,
);
mainGroup = EACAF19A1B16BD5000F01B99;
productRefGroup = EACAF1A41B16BD5000F01B99 /* Products */;
projectDirPath = "";
projectRoot = "";
targets = (
EACAF1A21B16BD5000F01B99 /* EightQueens */,
);
};
/* End PBXProject section */
/* Begin PBXSourcesBuildPhase section */
EACAF19F1B16BD5000F01B99 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
EACAF1AE1B16BD6700F01B99 /* Queen.swift in Sources */,
EACAF1A71B16BD5000F01B99 /* main.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */
/* Begin XCBuildConfiguration section */
EACAF1A81B16BD5000F01B99 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.10;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = macosx;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
};
name = Debug;
};
EACAF1A91B16BD5000F01B99 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.10;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
};
name = Release;
};
EACAF1AB1B16BD5000F01B99 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Debug;
};
EACAF1AC1B16BD5000F01B99 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
};
/* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */
EACAF19E1B16BD5000F01B99 /* Build configuration list for PBXProject "EightQueens" */ = {
isa = XCConfigurationList;
buildConfigurations = (
EACAF1A81B16BD5000F01B99 /* Debug */,
EACAF1A91B16BD5000F01B99 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
EACAF1AA1B16BD5000F01B99 /* Build configuration list for PBXNativeTarget "EightQueens" */ = {
isa = XCConfigurationList;
buildConfigurations = (
EACAF1AB1B16BD5000F01B99 /* Debug */,
EACAF1AC1B16BD5000F01B99 /* Release */,
);
defaultConfigurationIsVisible = 0;
};
/* End XCConfigurationList section */
};
rootObject = EACAF19B1B16BD5000F01B99 /* Project object */;
}

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:EightQueens.xcodeproj">
</FileRef>
</Workspace>

View file

@ -0,0 +1,19 @@
//
// Queen.swift
// EightQueens
//
// Created by Donald Burr on 5/27/15.
// Copyright (c) 2015 Donald Burr. All rights reserved.
//
import Foundation
class Queen {
var row: Int
var column: Int
init(forRow: Int) {
row = forRow
column = 0
}
}

View file

@ -0,0 +1,80 @@
//
// main.swift
// EightQueens
//
// Created by Donald Burr on 5/27/15.
// Copyright (c) 2015 Donald Burr. All rights reserved.
//
import Foundation
var solutionsFound = 0
var positionsChecked = 0
// create 8 queen objects, place them one per row
let myQueens = map(0...7) { Queen(forRow: $0) }
func isSafe(currentRow: Int, currentColumn: Int) -> Bool {
positionsChecked++
// iterate over all queens in previous rows
for previousRow in 0..<currentRow {
// check vertical: are we trying to place in the same column as a previously placed queen?
if myQueens[previousRow].column == currentColumn {
return false
}
// check diagonal
let differenceInRows = currentRow - previousRow
let differenceInColumns = currentColumn - myQueens[previousRow].column
if (differenceInRows == differenceInColumns || differenceInRows == -differenceInColumns) {
return false
}
}
// compared all previous queens, no conflict
myQueens[currentRow].column = currentColumn
return true
}
func moveQueenAcrossRow(row: Int) {
for column in 0...7 {
// move queen column by column, checking if it's safe
if isSafe(row, column) {
// if we just placed the 8th queen, we have a result!
if row == 7 {
solutionsFound++
printAnswer()
} else {
// recursive call - now move the queen to the NEXT row
moveQueenAcrossRow(row+1)
}
}
}
}
func printAnswer() {
println("SOLUTION #\(solutionsFound)")
println()
// go backwards to print from top down
for currentRow in reverse(0...7) {
// print row number (using chess numbering)
print(currentRow+1)
for currentColumn in 0...7 {
if (myQueens[currentRow].column == currentColumn) {
print(" Q ")
} else {
print(" . ")
}
}
println()
}
println(" A B C D E F G H ")
println()
}
// start by trying to move queen across row 0
moveQueenAcrossRow(0)
// that's it
println("POSITIONS CHECKED: \(positionsChecked)")

View file

@ -0,0 +1,99 @@
# Eight Queens
# a solution to the Eight Queens problem written in Python
# by Donald Burr <dburr@borg-cube.com>
#
# more on the Eight Queens problem:
# http://en.wikipedia.org/wiki/Eight_queens_puzzle
import cProfile
# a simple class to represent a Queen
class Queen:
def __init__(self, row):
self.row = row
self.column = 0
# Array to hold our 8 queens
# we're starting off by placing each queen on a separate row
queens = []
for row in range(8):
queens.append(Queen(row))
# some variables to keep some interesting statistics
solutions_found = 0
positions_checked = 0
# test the queen at a given row to see if it is safe at a given column
def is_safe(current_row, current_column):
global positions_checked
global queens
positions_checked = positions_checked + 1
for previous_row in range(current_row):
# check vertically: are we trying to place in the same column as a previously placed queen?
if queens[previous_row].column == current_column:
# bzzt - not safe!
return False
# now check diagonally
difference_in_rows = current_row - previous_row
difference_in_columns = current_column - queens[previous_row].column
if difference_in_rows == difference_in_columns or difference_in_rows == -difference_in_columns:
# bzzt, not safe!
return False
# we've compared against all queens, so we must be good
# place the queen and we're outta here!
queens[current_row].column = current_column
return True
# recursively try moving a queen across all columns
def move_queen_across_row(row):
global solutions_found
for column in range(8):
# move queen column by column, checking if it's safe
if is_safe(row, column):
# if we just placed the last queen, we've got a solution
if row == 7:
solutions_found = solutions_found + 1
print_answer()
else:
# recursive call - now move the queen to the NEXT row
move_queen_across_row(row+1)
# print out a solution once we've found it
def print_answer():
global solutions_found
global queens
print "SOLUTION #%d\n" % solutions_found
# rows are in reverse order
for current_row in reversed(range(8)):
# print row number (using chess numbering)
print "%d" % (current_row+1),
for current_column in range(8):
if queens[current_row].column == current_column:
print " Q ",
else:
print " . ",
print
print " A B C D E F G H \n\n"
##### end of functions
##### main code (such as it is) below here
##### putting it in a function so we can profile just for the halibut
def main():
# kick things off by trying to move the first queen
move_queen_across_row(0)
# all solutions have been found
print "POSITIONS CHECKED: %d\n\n" % positions_checked
# the REAL start of the code
# just profile it
cProfile.run('main()')