hdu2553 N皇后問題

Problem Description

在N*N的方格棋盤放置了N個皇后,使得它們不相互攻擊(即任意2個皇后不允許處在同一排,同一列,也不允許處在與棋盤邊框成45角的斜線上。
你的任務是,對於給定的N,求出有多少種合法的放置方法。
 

 

 

Input

共有若干行,每行一個正整數N≤10,表示棋盤和皇后的數量;如果N=0,表示結束。

 

 

Output

共有若干行,每行一個正整數,表示對應輸入行的皇后的不同放置數量。

 

 

Sample Input


 

1 8 5 0

 

 

Sample Output


 

1 92 10

 

 

Author

cgf

 

 

Source

2008 HZNU Programming Contest

代碼:

     打表,不打表超時

      關鍵:x[t]=i  表示第t行,第i列

     一顆大樹,除了葉節點每個結點都有8個子節點

     其實就是行是從1->n,看看對於每個行列有哪些選擇

import java.util.Scanner;

public class main {
	  static int n,cnt;
//	  static int x[]=new int[15];
//	  public static boolean check(int t){
//		   for(int i=1;i<t;i++)
//			   if(Math.abs(i-t)==Math.abs(x[i]-x[t]) || x[i]==x[t])
//				   return false;
//		   return true;
//	  }
//	  public static void dfs(int t,int m){
//		   if(t>m){
//			    cnt++;
//			    return;
//		   }
//		   for(int i=1;i<=m;i++){
//			     x[t]=i;
//			     if(check(t)) 
//			    	 dfs(t+1,m);
//		   }
//	  }
//	  static int arr[]=new int[15];
       public static void main(String[] args) {
		   Scanner scan=new Scanner(System.in);
//		   for(int i=1;i<=10;i++){
//			   cnt=0;
//			   dfs(1,i);
//			   arr[i]=cnt;
//		   }
//		   for(int i=1;i<=10;i++)
//			   System.out.print(arr[i]+" ");
//		   System.out.println();
		   int arr[]={1 ,0 ,0 ,2 ,10 ,4 ,40 ,92 ,352 ,724 };
		   while(scan.hasNext()){
			   n=scan.nextInt();
			   if(n==0) break;
			    System.out.println(arr[n-1]);
		   }
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章