/************************************************ Grundlegende Algorithmen mit Java, http://algorithmen-und-problemloesungen.de/ Copyright @2007-2009 by Doina Logofatu in C#: Michael Gärtner ************************************************/ using System; using System.IO; namespace Logofatu { class P08Springer { private static String FileOutputName = "springer.out"; private int nSol; private int[,] table; private StreamWriter sw; private int n; public P08Springer(int n, StreamWriter sw) { this.n = n; this.table = new int[n, n]; this.sw = sw; } private void writeSolution() { sw.Write(" Loesung: "); sw.WriteLine(++nSol); for ( int i = 0; i < n; i++ ) { for ( int j = 0; j < n; j++ ) sw.Write("{0,3:d} ", table[i, j]); sw.WriteLine(); } } private void run(int l, int c) { this.nSol = 0; this.table[l - 1, c - 1] = 1; this.back(l - 1, c - 1, 1); } private void back(int l, int c, int step) { if ( n * n == step ) { writeSolution(); return; } int dx, dy, lnew, cnew; for ( dx = -2; dx < 3; dx++ ) for ( dy = -2; dy < 3; dy++ ) if ( Math.Abs(dx * dy) == 2 ) { lnew = l + dx; cnew = c + dy; if ( 0 <= lnew && lnew < n && 0 <= cnew && cnew < n && table[lnew,cnew] == 0 ) { table[lnew, cnew] = step + 1; back(l + dx, c + dy, step + 1); table[lnew, cnew] = 0; } } } static void Main(string[] args) { StreamWriter sw = new StreamWriter(FileOutputName); try { string[] sArray = Console.ReadLine().Split(' '); int l = int.Parse(sArray[0]); int c = int.Parse(sArray[1]); new P08Springer(5, sw).run(l, c); } finally { sw.Close(); } } } }