DFS talk

DFS

The first contact should be to learn a piece of data traversing the tree and of a depth-first search and breadth-first search, but recently discovered this problem brush is used so often come to sum up. The old rules, to feel good on the blog.
DFS blog

Probably routine

Need to enumerate different circumstances when it is appropriate to use DFS, DFS is actually using a recursive enumeration, you can also back. Note that when using several places
1. boundary conditions, that is, when to exit the recursive
parameter 2.DFS in general what is used to make changes to put this variable parameter
condition 3. update, what state would further Search
this blog essay template is given in
Here Insert Picture Description
general, we need to set the mark to record whether or not searched, so there will be an array vis.

A simple example

Take the whole arrangement of an example, enter an integer, equal to the integer output is less than all of the arrangement
, for example, input 2, output: 12; 21

This is the first enumeration of a similar problem, so the direct use of DFS. According to the number of integer function parameters first thought, this question to an input of the above, it will be entered as a parameter. Then exit condition is that the recursive parameter exceeds the integer input and output all this time to exit the recursive arrangement. (If you do not understand the wait for the next look at the code to understand) and then update the condition is less than equal to the integer parameters and this number has not been searched, otherwise there will be repeated.

Code

//dfs
#include<iostream>
#include<cmath>
using namespace std;

int n;
bool vis[150]={false};
int res[150]={0};

void dfs(int x){
    if(x==n+1){  //到了最后一个
        for(int i=1;i<=n;i++)
            cout<<res[i]<<" ";
        cout<<endl;
        return ;
    }

    for(int i=1;i<=n;i++){
        if(vis[i]==false){
            res[x]=i;
            vis[i]=true;
            dfs(x+1);
            vis[i]=false;
        }
    }
}

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

Deep search from the beginning, every search will be deep x ++, so when x> to exit the recursive time n. Remember every time you have to change it back after traversing mark changes back guarantee.
Here Insert Picture Description

Slightly more complex

The problem today is just to brush, used to share
Here Insert Picture Description
first this into two parts, the two parts of the same value. I would like to see this beginning is greedy, to sort and then put great together, then slowly added to the small, see if you can have equal time. But look at the output, you need to include a minimum number of lattice partitions the upper left corner of the output, it can not be greedy, this time is similar to enumerate, so I used DFS.

Idea
since it is equal in two parts, as long as I like and equal to half the value of a portion of the total, when greater than the exit recursion. Counting the smallest lattice parameter of course, but because this is the top left corner of the grid need to include a two-dimensional array, so it needs coordinates x, y, and records the current values and parameters. But we are free to divide it also needs to be given direction of the search, this experience can remember when, after the encounter of this two-dimensional array can search directly out

Code

#include<iostream>
#include<string.h>
#include<cmath>
using namespace std;

int n,m;//格子的高,宽,map[n][m]
int map[11][11],vis[11][11],all_sum,res=404;
int dir[4][2]={0,1,0,-1,1,0,-1,0};//搜索的四个方向

void dfs(int x,int y,int sum,int cnt){    //x,y是当前坐标,sum是搜索位置值的和,cnt记录搜索过多少格子,也就是包含左上角的那部分格子数
    if(sum>all_sum/2)//当前和超过总数和的一半
        return;
    if(sum==all_sum/2){//刚好平分时更新cnt
        res=min(res,cnt);
    }   
    for(int i=0;i<4;i++){//向四个方向搜索
        int xx=x+dir[i][0];
        int yy=y+dir[i][1];
        if(xx>=0&&xx<n&&yy>=0&&y<m&&!vis[xx][yy]){//搜索的格子在范围内且没有搜索过
            vis[xx][yy]=1;
            dfs(xx,yy,sum+map[xx][yy],cnt+1);
            vis[xx][yy]=0;
        }
    }
}

int main(){
    cin>>m>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>map[i][j];
            all_sum+=map[i][j];
        }
    }

    memset(vis,0,sizeof(vis));
    vis[0][0]=1;//从左上角格子开始遍历搜索
    dfs(0,0,map[0][0],1);
    if(res==404)
        cout<<0<<endl;
    else
        cout<<res<<endl;
}

Here Insert Picture Description
Here Insert Picture Description

to sum up

Here Insert Picture Description
Actually, I think there is something to learn routines, or skill. Mastered the skills to learn quickly, to solve the problem most of the time with skill enough.

Published 85 original articles · won praise 55 · views 20000 +

Guess you like

Origin blog.csdn.net/shelgi/article/details/104189019
dfs