zoukankan      html  css  js  c++  java
  • C语言实现全排列和回溯法总结

    一、递归实现全排列

     1 #include"cstdio"
     2 int A[50];
     3 void print_permutation(int n,int *A,int cur){
     4         if(cur==n){
     5         for(int i=0;i<n;i++)
     6             printf("%d",A[i]);
     7         printf("
    ");
     8         }
     9         else for(int j=1;j<n+1;j++){
    10         int ok=1;
    11         for(int k=0;k<cur;k++)
    12             if(A[k]==j)
    13                 ok=0;
    14             if(ok){
    15             A[cur]=j;
    16             print_permutation(n,A,cur+1);
    17             }
    18         }
    19     }
    20 int main(){
    21     int n;
    22     scanf("%d",&n);
    23     print_permutation(n,A,0);
    24     return 0;
    25 }
    View Code

    二、解答树

     1     #include <string.h>
     2     #include <iostream>
     3      
     4     using namespace std;
     5     const int N = 99999999;     //输入排序的个数的最大值
     6     int record[N];              //记录每次排序的序列
     7     int visited[N];             //标记节点是否被访问过
     8     int n;                      //输入节点的数目
     9     int totalSize = 0;
    10     void DFS(int start){
    11         if(start>=n){           //递归出口
    12             for(int i=0;i<n;i++){
    13                 cout<<record[i]<<" ";
    14             }
    15             totalSize++;
    16             cout<<endl;
    17             return;
    18         }
    19         for(int i=1;i<=n;i++){      //深度遍历节点,并标记已经访问过的节点
    20             if(visited[i]==0){
    21                 visited[i] = 1;
    22                 record[start] = i;
    23                 DFS(start+1);       //递归遍历
    24                 visited[i] = 0;     //回退时标记回退的节点为未被访问节点
    25             }
    26         }
    27     }
    28      
    29     int main()
    30     {
    31         cin>>n;
    32         memset(visited,0,n);
    33         DFS(0);
    34         cout<<"totalSize = "<<totalSize<<endl;
    35         return 0;
    36     }
    View Code

    三、

    调用next_permutation()方法

    四、回溯法总结

    1、八皇后问题代码

     1 #include<iostream>
     2 #include<math.h>
     3 using namespace std;
     4 int n=8; 5 int rows[8];//存储n行的第几列
     6 int j=0;
     7 bool Is(int row){
     8     for(int i=1;i<row+1;i++){
     9         if(rows[row-i]==rows[row]-i||rows[row-i]==rows[row]+i||rows[row]==rows[row-i])
    10             return false;
    11     }
    12     return true;
    13 }
    14 void start(int row){
    15     if(row==n)
    16         j++;
    17         else {
    18         for(int col=0;col<n;col++){
    19             rows[row]=col;
    20             if(Is(row)){
    21                 printf("%d  %d
    ",row,rows[row]);
    22                 start(row+1);
    23             }
    24         }
    25     }
    26 }
    27 int main(){
    28     start(0);
    29     printf("%d
    ",j);
    30     return 0;    
    31 }

    总结:在全排列和八皇后问题中,均使用了递归回溯。其格式大致为

    void f(){

    If(){//符合要求的一组解求出后

      count++

    }else{

          For(int ....){

           f();//递归调用

        }

      }

    }

  • 相关阅读:
    stream流
    python笔记:str.contains小坑之 UserWarning To actually get the groups, use str.extract.
    获取excel的行索引值
    Pandas学习(4、数据载入、存储及文件格式
    python中如何压缩和解压缩文件
    保持服务器屏幕可控制状态
    python判断文件和文件夹是否存在、创建文件夹
    PDF处理
    selenium+python配置chrome浏览器的选项
    python实现复制粘贴
  • 原文地址:https://www.cnblogs.com/tao7/p/9549184.html
Copyright © 2011-2022 走看看