标签:print name tchar include define printf using can div
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 using namespace std; 6 #define N 9 7 int n,k,ans;//ans答案数 8 char m[N][N]; 9 int mark[N];//标记该列是否有棋子 10 11 void dfs(int f,int num)//f为行数,num为已放棋子数 12 { 13 int j; 14 if (num==k)//要先判断num==k再判断f>=n!!! 15 { 16 ans++; 17 return; 18 } 19 if (f>=n) 20 { 21 return; 22 } 23 for (j=0;j<n;j++) 24 { 25 if (mark[j]!=1&&m[f][j]==‘#‘) 26 { 27 mark[j]=1; 28 num++; 29 f++; 30 dfs(f,num); 31 mark[j]=0; 32 num--; 33 f--; 34 } 35 } 36 dfs(++f,num);//此列无法摆放,要继续向下一行DFS!!! 37 } 38 39 int main() 40 { 41 int num; 42 while (scanf("%d %d",&n,&k)&&n!=-1&&k!=-1) 43 { 44 int i,j; 45 for (i=0;i<n;i++) 46 { 47 scanf("%s",m[i]); 48 getchar(); 49 } 50 memset(mark,0,sizeof(mark)); 51 num=0; 52 ans=0; 53 for (i=0;i<n;i++) 54 { 55 for (j=0;j<n;j++) 56 { 57 if (m[i][j]==‘#‘) 58 { 59 mark[j]=1;//先放第一个棋子 60 i++; 61 num++; 62 dfs(i,num); 63 i--; 64 mark[j]=0; 65 num--; 66 } 67 } 68 } 69 printf("%d\n",ans); 70 } 71 72 return 0; 73 }
标签:print name tchar include define printf using can div
原文地址:https://www.cnblogs.com/hemeiwolong/p/9275573.html