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;
}

 

上一篇:Total Commander 用法介绍


下一篇:C语言题-2题:统计此字符串中字母、数字、空格和其它字符的个数