DFS special training 1

1_1: AcWing 93. Recursive implementation of combined enumeration (dfs)

Question link
Randomly select m from n integers from 1∼n and output all possible choices.

Input format:
Two integers n, m, separated by spaces on the same line.

Output format
Output all solutions in ascending order, 1 per line.

First, the numbers in the same row are arranged in ascending order, and two adjacent numbers are separated by a space.

Secondly, for two different lines, the numbers corresponding to the subscripts are compared one by one, and the smaller one in lexicographic order is ranked first (for example, 1 3 5 7 is ranked in front of 1 3 6 8).

Data range
n>0,
0≤m≤n,
n+(n−m)≤25

Input example:

5 3

Output sample:

1 2 3 
1 2 4 
1 2 5 
1 3 4 
1 3 5 
1 4 5 
2 3 4 
2 3 5 
2 4 5 
3 4 5 

Question to think about: What should you do if you are required to use a non-recursive method?

Analysis: In order to get a dictionary order, you only need to start from the smallest 1 as the root and search downwards.

#include <iostream>
#include <vector>

using namespace std;

const int N = 30;

int n, m;
bool st[N];
vector<int>ans;

void dfs(int u)
{
    
    
    if(ans.size() == m)
    {
    
    
        for (auto p : ans)
            cout << p << ' ';
            
        cout << endl;
        return ;
    }
    for (int i = u; i <= n; i ++ )
    {
    
    
        if(!st[i])
        {
    
    
            st[i] = true;
            ans.push_back(i);
            dfs(i);
            ans.pop_back();
            st[i] = false;
        }
    }
}

int main()
{
    
    
    cin >> n >> m;
    
    dfs(1);
    
    return 0;
}

1_2: AcWing 129. Train into stack (dfs + stack)

Question link
There are n trains that are about to enter and exit the station. However, each train has only one section, which is the front of the train.

These n trains turn left from the east and enter the station in order from 1 to n. This station is in the north-south direction. Although it is infinitely long, it is a dead end, and there is only one track on the platform. The trains can only go out from the west in reverse direction. , and each train must enter the station, first in, last out.

In other words, this train station is actually equivalent to a stack. Each time, the train on the right side can enter the stack, or the train on the top of the stack can leave the station.

The station diagram is as follows:

        出站<——    <——进站
                 |车|
                 |站|
                 |__|

Now please output the first 20 possible pop-up plans in "lexicographic order".

Input format
Enter an integer n, representing the number of trains.

Output format:
Output the first 20 answers in "lexicographic order", one type per line, without spaces.

Data range
1≤n≤20

Input example:

3

Output sample:

123
132
213
231
321


Analysis:
Insert image description here

#include <iostream>
#include <vector>
#include <stack>

using namespace std;

int n;
vector<int>ans;
stack<int>st;
int u = 1, cnt = 20;

void dfs()
{
    
    
    if(!cnt) return ; //  方案最多输出20种
    
    if(ans.size() == n)
    {
    
    
        cnt --;
        for (auto x : ans)
            cout << x;
            
        cout << endl;
        return ;
    }
    if(st.size()) // 栈里面不空
    {
    
    
        ans.push_back(st.top());
        st.pop();
        dfs();
        st.push(ans.back());
        ans.pop_back();
    }
    if(u <= n)
    {
    
    
        st.push(u);
        u ++;
        dfs();
        st.pop();
        u --;
    }
}
int main()
{
    
    
    cin >> n;
    
    dfs();
    
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_52331221/article/details/127657041