递归_百炼 1321 棋盘问题
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <math.h> 4 #include <algorithm> 5 #include <stdlib.h> 6 #include <vector> 7 #include <map> 8 #include <queue> 9 #include <string> 10 #include <iostream> 11 #include <ctype.h> 12 #include <string.h> 13 #include <set> 14 #include <stack> 15 #include<functional> 16 using namespace std; 17 #define Size 10 18 #define maxn 1<<30 19 #define minn 1e-6 20 char a[Size][Size]; 21 int dis[Size];//标记第i列放没放元素 22 int len; 23 int ans = 0; 24 void solve(int row, int k){ 25 if (k == 0) { 26 ans++; 27 return; //达到结束条件时候一定要return,不要再往下执行了 28 } 29 if (row > len) return; 30 solve(row + 1, k);//有可能这一行不放棋子也能满足要求 31 for (int i = 1; i <= len; i++){//列 32 if (a[row][i] == '#'&&dis[i] == 0){ 33 dis[i] = 1; 34 solve(row+1, k - 1); 35 dis[i] = 0; 36 } 37 } 38 } 39 int main(){ 40 int k; 41 while (cin >> len >> k){ 42 ans = 0; 43 for (int i = 1; i <= len; i++) 44 dis[i] = 0; 45 if (len == -1 || k == -1) break; 46 for (int i = 1; i <= len; i++) 47 for (int j = 1; j <= len; j++) 48 cin >> a[i][j]; 49 solve(1, k); 50 cout << ans << endl; 51 } 52 }