Permalink
Browse files

Starting point for eightqueens solutions -- in c, go, perl, php, ruby…

…, and xslt.
  • Loading branch information...
1 parent 6c94ff9 commit d6db477b8d77c07a7e824aef401ea6bc29142bd0 @evaddnomaid committed Oct 3, 2012
Showing with 271 additions and 0 deletions.
  1. +2 −0 c/Makefile
  2. +71 −0 c/eightq.c
  3. +3 −0 go/Makefile
  4. +80 −0 go/eightq.go
  5. +55 −0 perl/eightq.pl
  6. +2 −0 php/eightq.php
  7. +55 −0 ruby/eightq.rb
  8. +3 −0 xslt/eightq.xslt
View
@@ -0,0 +1,2 @@
+eightq: eightq.c
+ gcc -o eightq eightq.c
View
@@ -0,0 +1,71 @@
+/* Eight queens solution in C, starting point file */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+void printboard(int *board) {
+ int i, x, y;
+
+ for (y = 8; y > 0; y--) {
+ for (x = 0; x < 8; x++) {
+ if (board[x] == y) {
+ printf("[Q]");
+ } else {
+ printf("[ ]");
+ }
+ }
+ printf("\n");
+ }
+ printf("\n");
+}
+
+int addcol(int *board) {
+ /* Boards passed to addcol are assumed good so far */
+ int i, solutions, newcol;
+
+ solutions = 0;
+ if (board[7] > 0) {
+ /* This board is full and must be a solution */
+ printboard(board);
+ solutions = 1;
+ } else {
+ for (i = 1; i < 9; i++) {
+ if (newcol = checkleft(i, board)) {
+ int *newboard = malloc(8 * sizeof(int));
+ memcpy(newboard, board, 8 * sizeof(int));
+ newboard[newcol - 1] = i;
+ solutions += addcol(newboard);
+ free(newboard);
+ }
+ }
+ }
+ return solutions;
+}
+
+int checkleft(int pos, int *board) {
+ int j, nextcol;
+
+ for (j = 8; j > 0 && board[j - 1] == 0; j--) {
+ nextcol = j;
+ }
+ for (j = nextcol - 2; j >= 0; j--) {
+ /* check here */
+ if (board[j] == pos) { return 0; }
+ if (board[j] == pos + (nextcol - 1 - j)) { return 0; }
+ if (board[j] == pos - (nextcol - 1 - j)) { return 0; }
+ }
+ return nextcol;
+}
+
+main() {
+ int i, solutions;
+ int board[8];
+
+ for (i = 0; i < 8; i++) {
+ board[i] = 0;
+ }
+ solutions = addcol(board);
+ printf("Solutions: %d\n", solutions);
+}
+
View
@@ -0,0 +1,3 @@
+eightq: eightq.go
+ go build eightq.go
+
View
@@ -0,0 +1,80 @@
+package main
+
+import fmt "fmt"
+
+type Board struct {
+ boardstate [8]int // State of the board
+}
+
+func (b *Board) addcol() int {
+/* It is assumed that anytime we call addcol the board is known good */
+ var nextcol int
+ var solutions int = 0
+
+ if (b.boardstate[7] > 0) { // All cols full, so this board is a solution
+ b.printstate()
+ return 1
+ }
+ for i := 1; i < 9 ; i++ {
+ if (b.checkleft(i)) {
+ // It passes the test, so it is a potential solution
+ var newbd *Board = new(Board)
+ newbd.init()
+ for j := 7; j >= 0; j-- {
+ newbd.boardstate[j] = b.boardstate[j]
+ if b.boardstate[j] == 0 {
+ nextcol = j
+ }
+ }
+ newbd.boardstate[nextcol] = i
+ solutions += newbd.addcol()
+ }
+ }
+ return solutions
+}
+
+func (b *Board) checkleft(pos int) bool {
+ var nextcol int
+
+ // Find the first open column
+ for nextcol = 0; nextcol < 8 && b.boardstate[nextcol] > 0 ; nextcol++ { }
+ for i := nextcol - 1 ; i >= 0 ; i-- {
+ if (
+ (b.boardstate[i] == pos - (nextcol - i)) ||
+ (b.boardstate[i] == pos + (nextcol - i)) ||
+ (b.boardstate[i] == pos)) {
+ return false
+ }
+ }
+ return true
+}
+
+func (b *Board) printstate() int {
+ // Start with the top row and work down
+ for i := 8; i > 0; i-- {
+ for j := 0; j < 8; j++ {
+ if (b.boardstate[j] == i) {
+ fmt.Printf("[Q]")
+ } else {
+ fmt.Printf("[ ]")
+ }
+ }
+ fmt.Printf("\n")
+ }
+ fmt.Printf("\n")
+ return 0
+}
+
+func (b *Board) init() int {
+
+ for i := 0; i < 8; i++ {
+ b.boardstate[i] = 0
+ }
+ return 0;
+}
+
+func main() {
+ var startboard *Board = new(Board)
+
+ fmt.Printf("Solutions: %d\n", startboard.addcol())
+}
View
@@ -0,0 +1,55 @@
+#!/usr/bin/perl -w
+use strict;
+my $solutions = &addcol([]);
+print "Solutions: " . $solutions . "\n";
+
+sub addcol {
+ # All boards passed to addcol are assumed to be good so far
+ my $boardref = shift;
+ my $newsol = 0;
+ my @newboard;
+
+ if (@{$boardref} == 8) {
+ # Board is full; it must be a solution
+ &printboard($boardref);
+ $newsol += 1;
+ } else {
+ for (my $i = 1; $i <= 8; $i++) {
+ if (&leftisgood($i, $boardref)) {
+ @newboard = ($i);
+ for (my $j = $#$boardref; $j >= 0; $j--) {
+ unshift(@newboard, $boardref->[$j]);
+ }
+ $newsol += &addcol(\@newboard);
+ }
+ }
+ }
+ return $newsol;
+}
+
+sub leftisgood {
+ my $pos = shift;
+ my $boardref = shift;
+
+ for (my $i = 0; $i <= $#$boardref; $i++) {
+ if ($boardref->[$#$boardref - $i] == $pos) { return 0; }
+ if ($boardref->[$#$boardref - $i] == $pos + $i + 1) { return 0; }
+ if ($boardref->[$#$boardref - $i] == $pos - $i - 1) { return 0; }
+ }
+ return 1;
+}
+
+sub printboard {
+ my $board = shift;
+ for (my $y = 8; $y > 0; $y--) {
+ for (my $x = 0; $x < 8; $x++) {
+ if (exists($board->[$x]) && $board->[$x] == $y) {
+ print "[Q]";
+ } else {
+ print "[ ]";
+ }
+ }
+ print "\n";
+ }
+ print "\n";
+}
View
@@ -0,0 +1,2 @@
+<? // Eight queens solution for PHP
+?>
View
@@ -0,0 +1,55 @@
+#!/usr/bin/ruby
+
+class Eightqueens
+ @@solutions = 0
+
+ def good_addition(board, pos)
+ col = 0
+ for row in board do
+ return false if row == pos
+ return false if row + (board.size - col) == pos
+ return false if row - (board.size - col) == pos
+ col += 1
+ end
+ return true
+ end
+
+ def pg(good_sofar)
+ if good_sofar.count == 8
+ @@solutions = @@solutions + 1
+ printf("\n");
+ print_board(good_sofar)
+ else
+ i = 0
+ while i < 8
+ if good_addition(good_sofar, i)
+ a = Array.new
+ for x in good_sofar do
+ a << x
+ end
+ a << i
+ pg(a);
+ end
+ i += 1
+ end
+ end
+ return @@solutions
+ end
+
+ def print_board(ba)
+ for row in [0,1,2,3,4,5,6,7] do
+ for col in 0..7 do
+ if (ba[col] == row)
+ printf("[Q]")
+ else
+ printf("[ ]")
+ end
+ end
+ printf("\n");
+ end
+ end
+end
+
+eightqueens = Eightqueens.new
+printf("\nSolutions: %s\n", eightqueens.pg(Array.new))
+
View
@@ -0,0 +1,3 @@
+<?xml version="1.0"?>
+<xsl:stylesheet >
+</xsl:stylesheet>

0 comments on commit d6db477

Please sign in to comment.