Расставить на шахматной доске 8 ферзей, чтобы они не били друг друга. Найти количество таких расстановок и вывести одну из них на экран.
Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Chess { class Program { public static void perest(int k, int[] a) { if (k == 0) { print(a); //count++; } else for (int i = 0; i <= k; i++) { int t = a[k]; a[k] = a[i]; // Перехватов Д. 09мос(у) a[i] = t; perest(k - 1, a); t = a[k]; a[k] = a[i]; a[i] = t; } } public static void print(int[] a) { bool f = false; for (int j = 7; j >= 0; j--) { if (f) break; for (int i = 0; i < 8; i++) { if (i!=j) if ((Math.Abs(a[j] - a[i]) == Math.Abs(j - i) )) { f = true; break; } } } if (f == false) { count++; for (int i = 0; i < 8; i++) { Console.Write(a[i]); } Console.WriteLine(" "); } } public static int count; static void Main(string[] args) { int k = 8, n = 1; int[] a = new int[k]; for (int i = 0; i < k; i++, n++) a[i] = n; perest(k - 1, a); Console.WriteLine(count); } } }
10 декабря 2009, 23:38