[AcWing Brush Questions] Blue Bridge Cup Topic Breakthrough - Depth First Search - dfs (8)

Table of contents

Written in front:

Topic: 1114. Chessboard Problems - AcWing Question Bank

Title description:

Input format:

Output format:

data range:

Input sample:

Sample output:

Problem-solving ideas:

code:

AC !!!!!!!!!!

Write at the end:


Written in front:

How can I learn an algorithm well?

I personally think that systematic brushing of questions is particularly important,

Therefore, in order to learn depth-first search well, in order to use violent search to deal with the Blue Bridge Cup,

Without further ado, let's start quizzing right away!

Title: 1114. Chessboard Problems - AcWing Question Bank

Title description:

Input format:

The input contains multiple sets of test data.

The first line of each set of data is two positive integers n, k, separated by a space,

Indicates that the chessboard will be described in an n ∗ n matrix, as well as the number of pieces placed.

When -1 -1is indicates the end of the input.

The next n lines describe the shape of the checkerboard: each line has n characters, where  # the checkerboard regions are represented,

 . Indicates a blank area (the data is guaranteed to have no extra blank rows or columns).

Output format:

For each set of data, a line of output is given, and the number of schemes placed in the output is C (data guarantee C < 231).

data range:

n ≤ 8,k ≤ n 

Input sample:

2 1
#.
.#
4 4
...#
..#.
.#..
#...
-1 -1

Sample output:

2
1

Problem-solving ideas:

When we use depth-first search,

The first point to note is the order of the search,

Because we want to make sure,

We have written a recursive structure capable of traversing all cases.

(It is always good to be more familiar with the basic idea of ​​recursive search above)

 Next is the specific idea :

According to the meaning of the question, we can summarize two questions:

1. We can only play chess at the '#' position, not at the '.' position;

2. Only one chess is allowed in each row and column, so how to traverse all the situations on the board?

For the first question, we can add judgment,

The second question, we need to simulate it: (take the chessboard as an example with 3 rows and 3 columns)

Draw a recursive search tree:

We assume that each horizontal line is a row, and recursively search the next column each time:

root node:

Then, the first row can be placed in columns 1, 2, and 3,

We search recursively down accordingly:

 The missed columns cannot be placed after continuing the recursion:

 Continue to search recursively:

We found that this is actually a full permutation, and we can do it according to the idea of ​​a full permutation.

The following is the code implementation: 

code:

//包常用头文件
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

int n, k;

const int N = 10;

//计数
int res;

char g[N][N];

//记录这一列有没有下棋
bool used[N];

void dfs(int u, int cnt)
{
    //下的棋数符合题目要求,方案++
    if(cnt == k)
    {
        res++;
        return;
    }
    
    //已经到行底了
    if(u == n) return;
    
    for(int i = 0; i < n; i++)
    {
        if(!used[i] && g[u][i] == '#')
        {
            used[i] = true;
            dfs(u + 1, cnt + 1);
            used[i] = false;//恢复现场
        }
    }
    //如果前面(下的棋数符合题目要求,方案++),就会返回,导致到不了下一行,这里让他强行往下走一行
    dfs(u + 1, cnt);
}

int main()
{
    //多组输入
    while(cin >> n >> k && n > 0 && k > 0)
    {
        for(int i = 0; i < n; i++)
        {
            cin >> g[i];
        }
        //每组输入重新计数
        res = 0;
        dfs(0, 0);
        printf("%d\n", res);
    }
    return 0;
}

AC !!!!!!!!!!

Write at the end:

The above is the content of this article, thank you for reading.

If you like this article, please like and comment, and write down your opinions.

If you want to learn programming with me, you might as well follow me, we will learn and grow together.

I will output more high-quality content in the future, welcome to watch.

Guess you like

Origin blog.csdn.net/Locky136/article/details/129648782