1 /* 2 DFS:因为一行或一列都只放一个,可以枚举从哪一行开始放,DFS放棋子,同一列只能有一个 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 using namespace std; 8 9 char maze[10][10]; 10 bool vis[10]; 11 int n, k, ans; 12 13 void DFS(int x, int num) { 14 for (int i=1; i<=n; ++i) { 15 if (maze[x][i] == ‘#‘ && !vis[i]) { 16 if (num == 1) ans++; 17 else { 18 vis[i] = true; 19 for (int j=x+1; j<=n-num+2; ++j) { 20 DFS (j, num-1); 21 } 22 vis[i] = false; 23 } 24 } 25 } 26 } 27 28 int main(void) { //POJ 1321 棋盘问题 29 while (scanf ("%d%d", &n, &k) == 2) { 30 if (n == -1 && k == -1) break; 31 for (int i=1; i<=n; ++i) scanf ("%s", maze[i] + 1); 32 memset (vis, false, sizeof (vis)); ans = 0; 33 for (int i=1; i<=n-k+1; ++i) { 34 DFS (i, k); 35 } 36 printf ("%d\n", ans); 37 } 38 39 return 0; 40 }
时间: 2024-12-29 11:49:46