Board problem dfs

Chessboard problem

Place the chess pieces on a chessboard of a given shape (the shape may be irregular), and the chess pieces make no difference. It is required that any two chess pieces cannot be placed in the same row or the same column on the chessboard. Please program to solve all the feasible placement schemes C of k chess pieces for a chessboard of a given shape and size.
Input
contains multiple sets of test data.
The first row of each group of data is two positive integers, nk, separated by a space, which means that the chessboard will be described in an n*n matrix and the number of chess pieces placed. n <= 8, k <= n,
when it is -1 -1, the input is over.
The next n lines describe the shape of the chessboard: each line has n characters, where # represents the chessboard area, and. Represents the blank area (the data guarantees that there are no extra blank rows or blank columns).
Output
For each group of data, one line of output is given, and the number of output solutions is C (data guarantee C<2^31).
Sample Input
2 1
#.
.#
4 4
…#
…#.
.#…
#…
-1 -1
Sample Output
2
1

#include<bits/stdc++.h>
using namespace std;
bool vis[50]; 
int n,k;
typedef long long ll;//定义long long 需要用 typedef 
ll cnt;
char a[10][10];

void dfs(int x,int way){
    
    
	if(way==k){
    
    //达到规定数量需要回溯 
		cnt++;
		return;
	}
	if(x>=n){
    
    //超过行数需要回溯 
		return;
	}
	for(int i=0;i<n;i++){
    
    
		if(a[x][i]=='#'&&!vis[i]){
    
    
			vis[i]=1; //防止重复设为1 
			dfs(x+1,way+1);//行数和棋子数个加一 
			vis[i]=0;//回溯时需要把此节点变为0     ///注释1 
		}
	}
	dfs(x+1,way);//这一行没有地方时需要去找下一行   ///注释2 
}


int main(){
    
    
	while(~scanf("%d%d",&n,&k)){
    
    //输入 行数 和 棋子的数量 
		if(n==-1&&k==-1){
    
    
		 break;
		}
		memset(a,0,sizeof(a));//让棋盘初始化为0 且 必需放在输入的前面 
		for(int i=0;i<n;i++){
    
    
			cin>>a[i];
		}
		cnt=0;
		dfs(0,0);//重0行 和 0个棋子开始暴力找 
		cout<<cnt<<endl;
	}
	return 0;	
} 

Explanation for Note 1: Because we require only one pawn in each row and each column according to the title, if the pawn is placed in the current row, we can enter the next line search, then after the search is completed vis[i]= 0, what is the meaning of this? This means that when we have searched all the situations under this root node, we will backtrack to this node, that is to say, if we have traversed, if we put this chess piece on this position in this row and this column, below In all situations, then we have to search for the next state: that is, when the chess piece is placed in the next column of this row, then the chess piece we placed at this time is definitely not in this column, and we make this column unmarked.
Explanation for Note 2: This has two effects:
① If there is no chess piece in this line, we can skip to the next line and continue searching.
②Even if there are pieces to be placed in this row, we can traverse the situation that there are pieces to be placed in this row, but there is another case where no pieces are placed in this row, which is also possible, because it is enough to put enough k pieces at the end.

Guess you like

Origin blog.csdn.net/qq_47874905/article/details/109008877