นท กับ Search Engine
posted on 02 May 2011 11:43 by bi89 in Comedit @ 2 May 2011 12:16:06 by Bi89
edit @ 2 May 2011 12:17:02 by Bi89
edit @ 2 May 2011 12:16:06 by Bi89
edit @ 2 May 2011 12:17:02 by Bi89
1 package bishop;
2
3 public class Main {
4
5 public static boolean check(int[] b, int NumBoard) { //check the bishop do it cross to together
6 for (int i = 0; i < NumBoard; i++) {
7 if ((b[i] - b[NumBoard]) == (NumBoard - i)) return false; // same major diagonal
8 if ((b[NumBoard] - b[i]) == (NumBoard - i)) return false; // same minor diagonal
9 }
10 return true;
11 }
12
13 public static void print(int[] b) throws InterruptedException { // print the output
14 int N = b.length;
15 for (int i = 0; i < N; i++) {
16 for (int j = 0; j < N; j++) {
17 if (b[i] == j) System.out.print("B ");// print the Bishop
18 else System.out.print(": ");// print not Bishop
19 }
20 System.out.println("||");
21 }
22 System.out.println("....++++^^++++....\n");
23 Thread.sleep(1500);//time dalay
24 }
25
26 public static void explain(int N) throws InterruptedException { // do backtracking
27 int[] a = new int[N];
28 explain(a, 0);
29 }
30 public static void explain(int[] b, int NumBoard) throws InterruptedException { // do backtracking
31 int N = b.length;
32 if (NumBoard == N) print(b);
33 else {
34 for (int i = 0; i < N; i++) {
35 b[NumBoard] = i;
36 if (check(b, NumBoard)) explain(b, NumBoard+1);
37 }
38 }
39 }
40
41 public static void main(String[] args) throws InterruptedException { // the main
42 int N = 8;
43 System.out.println("Bishop in "+N+"x"+N+" chessboard.");
44 if (args.length > 0) {
45 N = Integer.parseInt(args[0]);
46 }
47 explain(N);
48 }
49 }
50 // อ่าน Blog การทำโค้ดนี้กันได้ที่ http://bi89.exteen.com
51
ส่วนผลลัพธ์ขอโปรแกรม ยังไม่ทราบ แต่คาดว่าเป็นพันๆวิธี เนื่อจากว่าเท่าที่ลอง มันใช้ว่าอย่างน้อย 113 นาที ในการคำนวน
edit @ 19 Apr 2011 22:40:58 by Bi89
edit @ 22 Feb 2011 04:02:44 by Bi89