1698: Sea, Iceberg, battleship time limit: 1 Sec memory limit: MB
submitted: 5 Resolution:
Submitted State [Discussion Version] Title Description
Give an n * N of the ocean, #代表海域,. To place a k battleship in the sea, the battleship will attack other warships in the same row, so the same row cannot be placed on the other battleship, asking how many (data guaranteed c<2^31) The plan number C is.
Input
The input contains multiple sets of test data.
The first row of each set of data is two positive integers, n K, (n <= 8, K <= N)
The following n lines describe the shape of the ocean: each row has n characters, where # represents the sea area. Represents an iceberg
Output
For each set of data, give a line of output, the output is placed in the number of programs C (Data Assurance c<2^31).
Sample input
2 #.. #4 4...#. #.. #.. #...
Sample output
A similar to the N Queen question ~
Ac-code:
#include <cstdio> #include <cstring>int k,m,sum,vis[10];char s[10][10];void dfs (int row,int num) {if (num==k ) Sum++;else if (row==m) return, else{for (int i=0;i<m;i++) {if (!vis[i]&&s[row][i]== ' # ') {Vis[i]=1;dfs (row+1 , num+1); vis[i]=0;}} DFS (Row+1,num);}} int main () {int i;while (~scanf ("%d%d", &m,&k)) {for (i=0;i<m;i++) scanf ("%s", S[i]); Memset (Vis ); Sum=0;dfs (0,0);p rintf ("%d\n", sum);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hpoj 1698: Sea, Iceberg, battleship "DFS"