Source: http://poj.org/problem? Id = 1321
Board Problems
| Time limit:1000 ms |
|
Memory limit:10000 K |
| Total submissions:22605 |
|
Accepted:11223 |
Description
There is no difference in placing a piece on a given-shape chessboard (which may be irregular. It is required that any two pawns should not be placed in the same row or column of the board. Please program to solve all feasible placement schemes C for all K pawns with a given shape and size.
Input
The input contains multiple groups of test data.
The first row of each group of data is two positive integers, n k, separated by a space, indicating that the board will be described in an N * n matrix, and the number of pieces placed. N <= 8, k <= N
If it is-1-1, the input ends.
The next n lines describe the shape of the Board: each line has n characters, where # indicates the board area, and .. indicates the blank area (no extra blank rows or columns are required for data ).
Output
For each group of data, a single line of output is provided, and the number of solutions placed in the output is C (Data guarantee is C <2 ^ 31 ).
Sample Input
2 1#..#4 4...#..#..#..#...-1 -1
Sample output
21
Source
Cai Wei @ PKU
Question: omitted.
Problem: DFS ~ Each time you fill in #, you can use two numbers row column to mark whether this row (column) can be filled.
AC code:
#include<iostream>#include<string>#include<cstring>using namespace std;int n,t,count;bool row[10],column[10];string map[10];void dfs(int x,int y,int step){ if(step==t){ count++; return ; } // cout<<x<<" "<<y<<" "<<step<<endl; for(int i=x;i<n;i++) for(int j=0;j<n;j++) if(map[i][j]=='#'&&!row[i]&&!column[j]){ map[i][j]='.';row[i]=true;column[j]=true; dfs(i,j,step+1); map[i][j]='#';row[i]=false;column[j]=false; }}int main(){ while(cin>>n>>t&&(n!=-1||t!=-1)){ count=0; memset(row,0,sizeof(row)); memset(column,0,sizeof(column)); for(int i=0;i<n;i++) cin>>map[i]; dfs(0,0,0); cout<<count<<endl; } return 0;}