棋盘问题_poj_1321

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
int n,k,rec[10];
double count;
char map[10][10];
void dfs(int step,int h){
    int i;
    if(h>n){
        return;
    }
    if(step==k){
        count++;
        return;
    }
    for(i=0;i<n;i++){
        if(map[h][i]=='#'&&rec[i]==0){
            rec[i]=1;
            dfs(step+1, h+1);
            rec[i]=0;
        }
    }
    dfs(step, h+1);
    return;
}
int main() {
    while(1){
        int i,j;
        scanf("%d%d",&n,&k);
        if(n==k&&n==-1){
            break;
        }
        getchar();
        for(i=0;i<n;i++){
            for(j=0;j<n;j++){
                scanf("%c",&map[i][j]);
            }
            getchar();
        }
        dfs(0, 0);
        printf("%.0fn",count);
        count=0;
        memset(map,0,sizeof(map));
        memset(rec,0,sizeof(rec));
    }
    return 0;
}

这道题算是有点小花样,就是当每行遍历完后不应return,而是到下行继续遍历,这样才能避免跳行的情况的缺失.