1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| #include <iostream> #include <cstdio> #include <cstring> using namespace std;
int n, k, tot; int a[10][10], vis[10][10]; char c[10];
inline bool pd(int x, int y) { int i, j; for (j = 1; j <= n; ++ j) if (a[x][j] == 1 && vis[x][j]) return false; for (i = 1; i <= n; ++ i) if (a[i][y] == 1 && vis[i][y]) return false; return true; }
inline void dfs(int t, int x) { if (x > k) { ++ tot; return; } int i, j; for (i = t; i <= n; ++ i) { for (j = 1; j <= n; ++ j) { if (a[i][j] == 1 && ! vis[i][j] && pd(i, j)) { vis[i][j] = 1;
dfs(i + 1, x + 1); vis[i][j] = 0; } } } return; }
signed main() { while (1) { int i, j; scanf ("%d%d", &n, &k); if (n == -1 && k == -1) return 0; memset(a, 0, sizeof a), tot = 0; memset(vis, 0, sizeof vis); for (i = 1; i <= n; ++ i) { scanf ("%s", c + 1); for (j = 1; j <= n; ++ j) a[i][j] = c[j] == '#' ? 1 : -1; } dfs(1, 1); cout << tot << '\n'; } return 0; }
|