n皇后問題(含位運算)

問題1:

題目描述

檢查一個如下的6 x 6的跳棋棋盤,有六個棋子被放置在棋盤上,使得每行、每列有且只有一個,每條對角線(包括兩條主對角線的所有平行線)上至多有一個棋子。

上面的佈局可以用序列2 4 6 1 3 5來描述,第i個數字表示在第i行的相應位置有一個棋子,如下:

行號 1 2 3 4 5 6

列號 2 4 6 1 3 5

這只是跳棋放置的一個解。請編一個程序找出所有跳棋放置的解。並把它們以上面的序列方法輸出。解按字典順序排列。請輸出前3個解。最後一行是解的總個數。

//以下的話來自usaco官方,不代表洛谷觀點

特別注意: 對於更大的N(棋盤大小N x N)你的程序應當改進得更有效。不要事先計算出所有解然後只輸出(或是找到一個關於它的公式),這是作弊。如果你堅持作弊,那麼你登陸USACO Training的帳號刪除並且不能參加USACO的任何競賽。我警告過你了!

輸入格式

一個數字N (6 <= N <= 13) 表示棋盤是N x N大小的。

輸出格式

前三行爲前三個解,每個解的兩個數字之間用一個空格隔開。第四行只有一個數字,表示解的總數。

輸入輸出樣例

輸入 #1複製

6

輸出 #1複製

2 4 6 1 3 5
3 6 2 5 1 4
4 1 5 2 6 3
4

思路:約束條件一共有3個:(1)不同行(直接在搜索時往下一行搜索就可以解決)。(2)不同列,用一個數組x,記錄第i行皇后所在的列數可以解決。  (3) 不能同一斜線,這個看斜率就可以,兩個點(i,x[i]) (j,x[j])  判斷斜率不爲1即可。(x[j]-x[i])/(i-j)==+-1,相當於abs(x-j)==abs(x[i]-x[j])。

#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <queue>
#include<map>
#include<set>
using namespace std;
const int inf = 0x3f3f3f3f;
typedef long long ll;
const int MAXN=30000010;
int n,sum=0;
int x[20]={0};
int Judje(int t){
    for(int i=1;i<t;i++){
        if(t-i==abs(x[i]-x[t])||x[t]==x[i])
            return 0;
    }
    return 1;
}
void DFS(int s)
{
    if(s>n){
        sum++;
        if(sum<=3){
            for(int i=1;i<=n;i++){
                if(i<=n-1)
                   printf("%d ",x[i]);
                else
                   printf("%d\n",x[i]);
            }
        }
        return ;
    }
    for(int i=1;i<=n;i++)
    {
        x[s]=i;
        if(Judje(s)==1)
            DFS(s+1);
    }
    return ;
}
int main()
{
    scanf("%d",&n);
    DFS(1);
    printf("%d\n",sum);
    return 0;
}

問題2:

Description

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

Input

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

Output

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

Sample Input
1
8
5
0

Sample Output
1
92
10

法1:

#include <stdio.h> 
#include <cmath> 
#include <algorithm> 
#include <iostream> 
#include <cstdlib> 
#include <cstring> 
#include <queue> 
#include<map> 
#include<set> 
using namespace std; 
const int inf = 0x3f3f3f3f; 
typedef long long ll; 
const int MAXN=30000010; 
int n,sum=0; 
int x[20]={0}; 
int Judje(int t){ 
    for(int i=1;i<t;i++){ 
        if(t-i==abs(x[i]-x[t])||x[t]==x[i]) 
            return 0; 
    } 
    return 1; 
} 
void DFS(int s) 
{ 
    if(s>n){ 
        sum++; 
        return ; 
    } 
    for(int i=1;i<=n;i++) 
    { 
        x[s]=i; 
        if(Judje(s)==1) 
            DFS(s+1); 
    } 
    return ; 
} 
int main() 
{ 
    while(~scanf("%d",&n)&&n){ 
        memset(x,0,sizeof(x)); 
        sum=0; 
        DFS(1); 
        printf("%d\n",sum); 
    } 
    return 0; 
} 

法2:(位運算):

#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <queue>
#include<map>
#include<set>
using namespace std;
const int inf = 0x3f3f3f3f;
typedef long long ll;
const int MAXN=30000010;
int n;
int sum=0;
int fast_pow[13]={0,1,3,7,15,31,63,127,255,511,1023,2047};
void DFS(int row,int rd,int ld,int m){
    if(m>n){
        sum++;
        return ;
    }
    int p=row|rd|ld;
    int np=fast_pow[n]^p;//取反
    int t=np;
    while(t){
        int pos=t&(fast_pow[n]-t+1);//取最後一位1(p&(~p+1))
        t-=pos;
        DFS(row|pos,(rd+pos)>>1,((ld+pos)<<1)&(fast_pow[n]),m+1);
        //((ld+pos)<<1)&(fast_pow[n])是將左移最高位置0
    }
    return ;
}
int main()
{
    while(~scanf("%d",&n)&&n){
        sum=0;
        DFS(0,0,0,1);
        cout<<sum<<endl;
    }
    return 0;
}

 

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