POJ1321棋盘问题 java

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;
         
                 
                }
            }
        }
    }
}

你可能感兴趣的:(dfs,bfs)