POJ 1321 棋盤問題 DFS 回溯

2021-07-14 20:53:31 字數 1524 閱讀 9942

time limit:1000ms

memory limit:10000k

total submissions:34746

accepted:17137

description

在乙個給定形狀的棋盤(形狀可能是不規則的)上面擺放棋子,棋子沒有區別。要求擺放時任意的兩個棋子不能放在棋盤中的同一行或者同一列,請程式設計求解對於給定形狀和大小的棋盤,擺放k個棋子的所有可行的擺放方案c。

input

輸入含有多組測試資料。

每組資料的第一行是兩個正整數,n k,用乙個空格隔開,表示了將在乙個n*n的矩陣內描述棋盤,以及擺放棋子的數目。 n <= 8 , k <= n

當為-1 -1時表示輸入結束。

隨後的n行描述了棋盤的形狀:每行有n個字元,其中 # 表示棋盤區域, . 表示空白區域(資料保證不出現多餘的空白行或者空白列)。

output

對於每一組資料,給出一行輸出,輸出擺放的方案數目c (資料保證c<2^31)。

sample input

21#.

.#44...

#..#.

.#..

#...

-1 -1

sample output

2

1

題意:求在n*n的圖中放m個的種類,簡單dfs解決,注意條件任意的兩個棋子不能放在棋盤中的同一行或者同一列,』#』是棋盤可以擺,』.』是空的不可以擺。

#include 

#include

#include

#include

#include

#include

#include

#include

using

namespace

std;

#define eps 1e-6

#define pi 3.14159265359

typedef

long

long ll;

const

int maxn = 10;

int n, m, ma,cnt;

char a[maxn][maxn];

int vis[maxn];

/*在n*n的圖中放m個的種類*/

void dfs(int r,int curm)

if(r>n) return;

for(int j=1;j<=n;j++)

} dfs(r+1,curm);

return;

}int main()

}cnt = 0;

dfs(1,0);

cout

/*4 4

####

####

####

####

24*/

POJ 1321 棋盤問題(棋盤DFS)

棋盤問題 思路 分層查詢,逐行深搜。注意k n時的處理。1 include 2 include 3 include 4 include 5 include 6 include 7 include 8 include 9 include 10 define ll long long 11 define...

POJ 1321 棋盤問題 DFS

題意 在乙個給定形狀的棋盤 形狀可能是不規則的 上面擺放棋子,棋子沒有區別。要求擺放時任意的兩個棋子不能放在棋盤中的同一行或者同一列,請程式設計求解對於給定形狀和大小的棋盤,擺放k個棋子的所有可行的擺放方案c。題解 include using namespace std define n 10 bo...

poj 1321 棋盤問題(DFS)

大概題意就是給你乙個棋盤,讓你放棋子,求在棋盤n n上的 放上k個棋子的所有情況的數目。要求擺放時任意的兩個棋子不能放在棋盤中的同一行或者同一列。深搜可以搜尋出所有答案。直接看 include includebool chess 9 9 bool vis col 9 int n,k,ans void...