#2019120500015-LG 全排列

P1706 全排列 搜索
(与原题不符 原题输出前三个 本代码输出全部+个数ans)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int a[25];
bool vis[25];
int n;
int ans=0; 
void f(int x){
    if(x==n+1){
        ans++;
        //if(ans<=3){
            for(int i=1;i<=n;i++){
                printf("%5d",a[i]);
            }
            printf("\n");
        //}
        return;
    }
    for(int i=1;i<=n;i++)
    {
        if(vis[i]==1) continue;
        bool q=1;
        //for(int j=1;j<x;j++)
        //{
        //  if(abs(x-j)==abs(i-a[j])){
        //      q=0;
        //      break;
        //  }
        //  
        //}
        if(q){
            vis[i]=1;
            a[x]=i;
            f(x+1);
            vis[i]=0;
        }
        
    }
    return ;
}
int main( ){
    scanf("%d",&n);
    f(1);
    printf("%d",ans); //...
    return 0;
} 

猜你喜欢

转载自www.cnblogs.com/liuziwen0224/p/11992426.html