DFS and Permutation and Combination (Description in C Language) #2

Title description:
Output all non-repeated permutations of natural numbers from 1 to n, that is, the complete permutation of n. It is required that no repeated numbers appear in any number sequence generated.
Sample input:
3
Sample output:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
First upload the code:
C language description with backtracking DFS

#include<stdio.h>
int n=0;
int k=0,l=0,m=0;
int a[100];
int b[100];
void dfs(int k)
{
    
    int i=0;
    if(k==n){
    
    
        for(i=0;i<n;i++)
        printf("%5d",a[i]);
        printf("\n");
        return;
    }
        for(i=1;i<=n;i++)
        if(b[i]==0)//这个数没用过
        {
    
    
        a[k]=i;//纳入数组
         b[i]=1;//标记使用
        dfs(k+1);//往下填
        b[i]=0;//回溯
        }
}
int main()
{
    
    scanf("%d",&n);
dfs(0);
    return 0;
}

Pay attention to two points here
1.dfs(k+1) is not k++,++k;
2.i don't open the global, the value will be washed out in the recursion.
Principle:
Insert picture description here
As shown in the figure, when you encounter elements that have been used for pruning, take the beginning with 1 in [123] as an example to
loop to 1, enter the recursive search 123, keep 12 13 (exclude 11) Continue searching, keep 123 132 exclude ( 121, 122, 131, 133).
The above is the arrangement.
Supplement: Backtracking recursive DFS is used for knapsack problem.
The time limit determines whether DFS can be used for the knapsack problem. It is feasible from the pursuit of the answer, but it is difficult to AC from the perspective of time.
One-dimensional backpack:

#include<stdio.h>
int n=0,m=0,x=0;
int a[103][3],b[15000];
int i=0,j=0,k=0,sum=0,l=0,p=0,y=0;
int c[101];
int max(int a,int b)
{
    
    
    if(a>=b)
        return a;
    else
        return b;
}
void dfs(int sum,int k,int i)
{
    
    
int z=0;
        int j=0;
for(j=i;j<=n;++j){
    
    
    if(c[j]==0)
              if(a[j][1]<=k)
        {
    
    z=1;
        c[j]=1;
        dfs(sum+a[j][0],k-a[j][1],i+1);
        c[j]=0;
        }
}
if(z==0)
    {
    
    y=max(y,sum);
    return;}
}
int main()
{
    
    int max=0;
scanf("%d %d",&m,&n);//m为重量限制,n为可选择数目
for(i=1;i<=n;i++)
{
    
    
    scanf("%d %d",&a[i][1],&a[i][0]);
}
dfs(0,m,1);
printf("%d",y);
return 0;
}

Two-dimensional backpack:

#include<stdio.h>
int n=0,m=0,x=0;
int a[103][3],b[15000];
int i=0,j=0,k=0,sum=0,l=0,p=0,y=0;
int c[101];
int max(int a,int b)
{
    
    
    if(a>=b)
        return a;
    else
        return b;
}
void dfs(int sum,int k,int l,int i)
{
    
    
int z=0;
        int j=0;

for(j=i;j<=n;++j){
    
    
    if(c[j]==0)
              if(a[j][1]<=k&&a[j][2]<=l)
        {
    
    z=1;
        c[j]=1;
        dfs(sum+a[j][0],k-a[j][1],l-a[j][2],i+1);
        c[j]=0;
        }
}
if(z==0)
    {
    
    y=max(y,sum);
    return;}
}
int main()
{
    
    int max=0;
scanf("%d %d %d",&n,&m,&x);//n为可选择数目,m为数据限制1,x为数据限制2
for(i=1;i<=n;i++)
{
    
    
    scanf("%d %d %d",&a[i][0],&a[i][1],&a[i][2]);
}
dfs(0,m,x,1);
printf("%d",y);
return 0;
}

Both codes have been tested in a circle, and they will be TL when the data is large enough, but the results themselves are no problem.
The selection ornot tree in the previous article can also be used for skiing problems.
Similar to taking
dfs(i+1,j-1)
dfs(i+1,j+1)
recursively, but again, time is limited, so many can be DFS The problem can only be DP.
No way, attach a one-dimensional backpack. Answer:

#include<stdio.h>
int dp[101][1005];
int max(int a,int b)
{
    
    
    if(a>=b)
        return a;
    else
        return b;
}
int main()
{
    
    int n=0,k=0,i=0,j=0,m=0;
int a[103],b[103];
scanf("%d %d",&n,&m);
for(i=1;i<=m;i++)
    scanf("%d%d",&a[i],&b[i]);
for(i=1;i<=m;i++)
   for(j=n;j>=0;j--)
   {
    
    
       if(a[i]<=j)
        dp[i][j]=max(dp[i-1][j],dp[i-1][j-a[i]]+b[i]);
        else
        dp[i][j]=dp[i-1][j];
   }
   printf("%d",dp[m][n]);
    return 0;
}

Summary: The DP is DP, even if DFS can do it, DP looks better regardless of code or time complexity.
Although it was originally permutation and combination;

Atoms are unlimited in number and diverse in form.
Everything is arranged and combined.
————Democritus

Guess you like

Origin blog.csdn.net/weixin_43736127/article/details/105949271