棋盘问题

 1 #include<iostream>

 2 #include<cstdio>

 3 #define max 10

 4 using namespace std;

 5 char s[max][max];

 6 int n,k,t=0;

 7 bool deal(int r,int c)

 8 {

 9     for(int i=0;i<n;i++)

10     {

11         if(s[i][c]=='o') return false;

12     }

13     for(int i=0;i<n;i++)

14     {

15         if(s[r][i]=='o') return false;

16     }

17     return true;

18 }

19 void dfs(int c,int m)

20 {

21     int x,y;

22     if(m==k)

23     {

24         t++;

25         return ;

26     }

27     if(c==n*n)

28         return;

29     x=c/n;

30     y=c%n;

31     if(s[x][y]=='#'&&deal(x,y))

32     {

33 

34         s[x][y]='o';

35         dfs(c+1,m+1);

36         s[x][y]='#';

37     }

38     dfs(c+1,m);

39     return ;

40 }

41 int main()

42 {

43     while(scanf("%d%d",&n,&k)&&n!=-1&&k!=-1){

44             t=0;

45         for(int i=0;i<n;i++)

46             for(int j=0;j<n;j++)

47                 cin>>s[i][j];

48         dfs(0,0);

49         printf("%d\n",t);

50     }

51     return 0;

52 }
View Code

 

你可能感兴趣的:(问题)