Public · Protected · Private
A horse wants to travel across the chessboard
Type: Public  |  Created: 2008-07-29  |  Frozen: Yes
« Previous Public Blog Next Public Blog »
Comments
  • Problem Statement: A horse starts at any place in chessboard. and completes travelling all 64 positions. Each position is visited only once.
    2008-07-29 11:29
  • using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; namespace Chess { public partial class Form1 : Form { Int64 totalhits; static int[] xSteps = { -2, 2, -1, 1, -2, 2, -1, 1 }; static int[] ySteps = { 1, 1, 2, 2, -1, -1, -2, -2 }; public static bool[,] FilledBools = new bool[8, 8]; public static bool[,] succesAtdepth = new bool[8, 8]; public static ChessBox[,] CompleteBoard = new ChessBox[8, 8]; StringBuilder final; private static int dummy = 0;
    2010-09-30 23:45
  • public static List %lt; ChessBox %gt; [,] XYPossibilities = new List %lt; ChessBox %gt; [8,8]; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { button1.Enabled = false; totalhits = 0; final = new StringBuilder(); for (int i = 0; i %lt; 8; i++) { for (int j = 0; j %lt; 8; j++) { CompleteBoard[i, j] = new ChessBox() { x = i, y = j }; } } for (int i = 0; i %lt; 8; i++) { for (int j = 0; j %lt; 8; j++) { XYPossibilities[i, j] = new List %lt; ChessBox %gt; (); XYPossibilities[i, j] = FindPossibilities(i,j); } } bool success = recursedata(0, 0, 0 ); if (success) MessageBox.Show(final.ToString()); } private bool recursedata (int x,int y,int depthNow) { totalhits++; if (depthNow %gt; = 63) return true; else { succesAtdepth[x, y] = false; FilledBools[x, y] = true; List %lt; ChessBox %gt; possibilities = XYPossibilities[x, y]; foreach (ChessBox c in possibilities) { if ((!Form1.FilledBools[c.x, c.y])) { succesAtdepth[x, y] = recursedata(c.x, c.y, depthNow + 1); if (succesAtdepth[x, y]) { final.Append("(" + c.x.ToString() + "," + c.y.ToString() + ")-"); break; } } } if (!succesAtdepth[x, y]) { FilledBools[x, y] = false; } return succesAtdepth[x, y]; } } private System.Collections.Generic.List %lt; ChessBox %gt; FindPossibilities(int i, int j) { if (XYPossibilities[i, j].Count == 0) { for (dummy = 0; dummy %lt; 8; dummy++) { if ( ((i + xSteps[dummy]) %gt; -1) && ((i + xSteps[dummy]) %lt; 8) && ((j + ySteps[dummy]) %gt; -1) && ((j + ySteps[dummy]) %lt; 8) ) XYPossibilities[i, j].Add(CompleteBoard[i + xSteps[dummy], j + ySteps[dummy]]); } } return XYPossibilities[i, j]; } } public struct ChessBox { public int x; public int y; } }
    2010-10-01 00:21
  • you may need to replace %lt; with LESS-THAN and %gt; with GREATER-THAN symbols
    2010-10-01 00:22
  • The answer I got is (1,0)-(3,1)-(1,2)-(2,0)-(0,1)-(2,2)-(1,4)-(3,3)- (4,1)-(6,0)-(7,2)-(5,1)-(7,0)-(6,2)-(4,3)-(6,4)- (7,6)-(5,7)-(4,5)-(2,4)-(0,3)-(1,1)-(3,0)-(4,2)- (5,0)-(7,1)-(5,2)-(4,0)-(6,1)-(7,3)-(5,4)-(7,5)- (6,3)-(4,4)-(6,5)-(7,7)-(5,6)-(3,7)-(1,6)-(3,5)- (4,7)-(6,6)-(7,4)-(5,3)-(3,2)-(1,3)-(3,4)-(1,5)- (0,7)-(2,6)-(0,5)-(1,7)-(3,6)-(5,5)-(6,7)-(4,6)- (2,7)-(0,6)-(2,5)-(0,4)-(2,3)-(0,2)-(2,1)- Positions in reverse order.(what is reverse? what is forward? :) )
    2010-10-01 00:24
This blog is frozen. No new comments or edits allowed.