ブレークボード女王N

説明

A N * Nは、ボードが古すぎるので、いくつかの壊れたグリッドは女王を置くことができない、チェス盤。
壊れたボードの統計上の女王のしてくださいソリューションN番号。

エントリー

2つの正の整数N、K、サブボードサイズの数を表すと例外の最初の行
例外サブ行を表す2つの整数の次のKライン、列番号

輸出

ボード上の壊れたNクイーン・ソリューションの数を表す整数;無ソリューション、出力であれば「いいえソリューション!」

サンプル入力

#1:
8 4
1 1
2 2
3 3
7 5 

#2:
8 0

サンプル出力

#1:
64

#2:
92

プロンプト

データの100%に、N <= 15、K <= N ^ 2

 

#include <bits/stdc++.h>

using namespace std;

//int i,j,k;

const int maxn=INT_MAX;

const int idata=50;
bool maps[20][20];
int judge[idata][3];
int n,m;
int cnt;

void dfs(int x)
{
    for(int i=1;i<=n;i++)
    {
        if(!judge[i][0]&&!judge[i+x][1]
           &&!judge[i-x+15][2]&&!maps[x][i])
        {
            judge[i][0]=1;
            judge[i+x][1]=1;
            judge[i-x+15][2]=1;

            if(x==n) cnt++;
            else dfs(x+1);

            judge[i][0]=0;
            judge[i+x][1]=0;
            judge[i-x+15][2]=0;

        }
    }
}

int main()
{
    int x,y;
    cin>>n>>m;
    while(m--)
    {
        cin>>x>>y;
        maps[x][y]=1;
    }
    dfs(1);
    if(cnt)
        cout<<cnt<<endl;
    else
        cout<<"No Solution!"<<endl;
    return 0;
}

 

公開された177元の記事 ウォンの賞賛8 ビュー6341

おすすめ

転載: blog.csdn.net/C_Dreamy/article/details/104247509