chessboard problem

This problem is solved by dfs. The dfs function has only one parameter, which is the number of rows. Because the title says that two chess pieces cannot be placed in the same row or column of a chessboard, only one position can be selected for a row. Let's play chess. First define an array, visited is used to record whether there are chess pieces in the i-th column, note that it is the i-th column.

Then define a global variable way to record how many chess pieces have been put into the chessboard. The recursive boundary condition is that way is equal to k;

code show as below:

#include<bits/stdc++.h>
using namespace std;
int n,k;
char a[10][10];
int visited[10]; //visited array record has been placed in the first h-1 row and column chess pieces
int cont=0; //cont represents the number of kinds
int way=0;
void dfs(int h) //h represents the number of rows way represents the number of pieces selected to put in
{
    if(way==k) //when When the number of pieces placed is equal to the number of pieces to be placed, add 1 to the number of plans
    {
        cont++;
        return ;
    }
    if(h>=n) //When the number of lines searched is greater than the chessboard,
        return directly ;
    for(int i =0;i<n;i++) //List from the first position in the h line
    {
        if(!visited[i]&&a[h][i]=='#') //Current h-1 The chess piece has been placed in the i-th column of the row, then the h-th row does not need to be placed because the chess piece cannot be placed in the same row and the same column
        {
            visited[i]=1;
            way++;
            dfs(h+1);
            visited[i]=0; //Restore if there are two in row 0# When going down from the first # in row 0, suppose there are and only in row 2 A #, and not in the same column as any one in the first row, then go down
            way--; //When walking, this # mark will be marked when the recursion returns to the first # in the first row, i will ++ To the second # of the first line, the function will continue to go down to the # of the second line, because in the path of the first # of the first line
            // has passed the one of the second line # (Because there is only one # in the second row and the number of columns where this # is located does not coincide with the two #s in the first row), if the # mark on the second row is not removed for the first time, then the path will not be repeated this time. After // this # in the second line,
            then the optional path is missing, it must be wrong

        }
    }
    dfs(h+1);
}
int main()
{
    while (cin>>n>>k)
    {
        if(n==- 1&&k==-1)
            break ;
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
            cin>>a[i][j];
        dfs(0);
        cout<<cont<<endl;
        memset(visited,0,sizeof(visited));
        cont=0;
        way=0;
    }

}

Analyze how the dfs function works. First, in the main function, enter h=0 into the dfs function (meaning to search for the first line), and then use a for loop to access each character in this line in turn. If you can go down, put visited[i] Mark it as 1 (that is, the i-th column has been walked), let the way increase by 1, so that dfs enters the 2nd row, and then in the above process, when the way is equal to the given k, the number of seeds is increased by 1. If way is less than k, but h is already greater than the number of lines given by the question, return to the previous function directly, no need to visit again, because this line does not exist. The key of dfs is that when all the characters in this layer are accessed, it should be dfs(h+1) again, if not, then when all the characters in one line do not satisfy if(!visited[i]&&a[h ][i]=='#') This condition is to return to the previous line directly, and then enter this line from the next character of the previous line, but this line does not meet the conditions, so it is impossible for the dfs function to enter this line. The next line of a line, so there is no way to explore the characters of the line after this line, so it must not be right. But when dfs(h+1) is added, because all characters in this line will not meet the conditions, then after the for loop ends, it will enter the next line because of the dfs(h+1) statement, and continue to access the next line of this line. one line

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325725282&siteId=291194637