import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static char xy[][];
public static boolean h[];
public static int step;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
step=0;
int a,b;
a=sc.nextInt();b=sc.nextInt();
if (a==-1 && b==-1){
break;
}
xy=new char[a][a];
h=new boolean[a];
for (int i = 0; i <a ; i++) {
char z[]=sc.next().toCharArray();
for (int j = 0; j <a ; j++) {
xy[i][j]=z[j];
}
}
dfs(0,b);
System.out.println(step);
}
}
public static void dfs(int x,int to ){
if (to==0){
step++;
return;
}
int sb=to;
for (int i = x; i <xy.length ; i++) {
for (int j = 0; j < xy.length; j++) {
if (h[j]==false && xy[i][j]=='#'){
h[j]=true;
dfs(i+1,to-1);
h[j]=false;
}
}
}
}
}
POJ1321棋盘问题 java
最新推荐文章于 2022-11-21 10:13:01 发布
本文展示了一个使用Java实现的深度优先搜索(DFS)算法示例,该算法应用于解决特定类型的图遍历问题。通过输入矩阵和目标节点,程序能够找到从起点到目标的所有可能路径数量,提供了一个清晰的解决方案来理解DFS的工作原理。
1万+

被折叠的 条评论
为什么被折叠?



