poj 1321(dfs,类似八皇后问题)

2021/12/1 23:08:36

本文主要是介绍poj 1321(dfs,类似八皇后问题),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

#include<iostream>
#include<cstring>
using namespace std;
int n,k,total,m;
char ch[10][10];
bool book[10];
void dfs(int cur){
    if(m==k){
        total++;
        return;
    }
    if(cur>=n){
        return;
    }
    for(int i=0;i<n;i++){
        if(!book[i]&&ch[cur][i]=='#'){
            m++;
            book[i] = true;
            dfs(cur+1);
            m--;
            book[i] = false;
        }
    }
    dfs(cur+1);
}
int main(){
    while(scanf("%d%d",&n,&k)==2&&n!=-1){
        for(int i=0;i<n;i++){
            scanf("%s",ch[i]);
        }
        memset(book,false,sizeof book);
        total = m = 0;
        dfs(0);
        printf("%d\n",total);
    }
    return 0;
}

 



这篇关于poj 1321(dfs,类似八皇后问题)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程