首页 > 代码库 > 【dfs】POJ1321 棋盘问题

【dfs】POJ1321 棋盘问题

题目链接: http://poj.org/problem?id=1321

题解: http://www.tuicool.com/articles/nE7BNj

 

 1 #include<cstdio> 2 #include<cstring> 3 char mat[15][15]; 4 bool col[15]; 5 int n, k, ans; 6  7 void dfs(int line, int cnt){ 8     if(cnt == k){ 9         ans++;10         return;11     }12     while(line < n){13         for(int i = 0; i < n; i++){14             if(mat[line][i] == # && col[i] == false){15                 col[i] = true;16                 dfs(line+1, cnt+1);17                 col[i] = false;18             }19         }20         line++;21     }22 }23 24 int main(){25     while(~scanf("%d%d", &n, &k)){26         if(n == -1 && k == -1) break;27         memset(mat, 0, sizeof(mat));28         memset(col, 0, sizeof(col));29         for(int i = 0; i < n; i++) scanf("%s", mat[i]);30         ans = 0;31         dfs(0, 0);32         printf("%d\n", ans);33     }34 35     return 0;36 }

 

【dfs】POJ1321 棋盘问题