public class POJ1321 {
private static int count = 0;
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
while(true ){
int n = scanner.nextInt();
int k = scanner.nextInt();
if(n == -1 && k == -1){
System.out.println("end");
scanner.close();
return;
}
char[][] chars = new char[n][n];
for(int i = 0; i < n; i++) {
char[i] = scanner.next().toCharArray();
}
int[] rows = new int[n];//该行是否放置了棋子,0:未放 1:放置
int[] columns = new int[n];//该列是否放置了棋子,0:未放 1:放置
dfs(0, n, k, rows, columns, chars);
System.out.println("count:" + count);
count = 0;
}
}
private static void dfs(int row, int n, int k, int[] rows, int[] columns, char[][] chars) {
if(k == 0){
count++;
return ;
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++) {
if(chars[i][j] == '#' && rows[i] == 0 && columns[j] == 0) {
rows[i] = 1;
columns[j] = 1;
dfs(i+1, n, k-1, rows, columns, chars);
rows[i] = 0;
columns[j] = 0;
}
}
}
}
}
POJ-1321Java实现
于 2024-01-18 11:44:15 首次发布