棋盘问题 POJ - 1321

棋盘问题 POJ - 1321
在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。
Input
输入含有多组测试数据。
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n
当为-1 -1时表示输入结束。
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。
Output
对于每一组数据,给出一行输出,输出摆放的方案数目C (数据保证C<2^31)。
Sample Input
2 1
#.
.#
4 4
…#
…#.
.#…
#…
-1 -1
Sample Output
2
1

思路:一个简单搜索,dfs水题,
类似于八皇后问题,https://blog.csdn.net/h_666666/article/details/86748415
以前写过的,(dfs应该要回溯的,但是这里的写和不写确不一样),然后题上有一句话:数据保证不出现多余的空白行或者空白列,这样就能省去判断现在放了多少个棋子而不再去传参,而且vis数组也好写了,直接标记列就好了,因为每次低轨道下一行,所以行之间不会有冲突。

还有一个很蠢的事情,没有看懂题意,到底是#能放,还是。能放???

下面AC的代码属于dfs的正规操作,板子的话还是选择下面那个把

开始的wa掉的代码,写了回溯,测试样例能过,但是wa掉了额???哪位大佬看到后帮我指一下错误呀,哭了,,

import java.util.Scanner;

class Main{
    static char[][] graph;
    static char[][] flag;
    static int n;
    static  int k;
    static int count;
    static int num;
    public static void main(String[] args) {
        Scanner sc  = new Scanner(System.in);
        while (true){
            n= sc.nextInt();
            k = sc.nextInt();
            if(n==-1 && k ==-1){
                break;
            }
            else {
                graph = new char[n][n];
                flag = new char[n][n];
                for(int i =0;i<n;i++){
                    graph[i] = sc.next().toCharArray();
                }
                count  = 0;
                num = 0;
                dfs(0,0);
                System.out.println(num);

            }
        }
    }
    static void dfs(int row,int count){
        if(count==k){
            num++;
            return;
        }
        for(int col = 0;col<n;col++){
            boolean cheack = true;
            for(int i =0;i<row;i++){
                if(flag[i][col]==1 || graph[row][col]=='.'){
                    cheack = false;
                    break;
                }
            }
            if(cheack){
                flag[row][col] = 1;
                dfs(row+1,count+1);
                flag[row][col] = 0;
            }
        }
    }


}

ac代码:

import java.util.Scanner;

class Main{
    static int vis[] = new int[20];
    static char[][] graph = new char[20][20];
    static int n;
    static int k;
    static int num = 0;
    public static void main(String[] args) {
        Scanner sc  = new Scanner(System.in);
        while (true){
            n= sc.nextInt();
            k = sc.nextInt();
            if(n==-1 && k ==-1){
                break;
            }
            else {

                for(int i =0;i<n;i++){
                    graph[i] = sc.next().toCharArray();
                }
                num = 0;
                dfs(0,0);
                System.out.println(num);

            }
        }
    }
    static void dfs(int x,int y){
        if(y>=k){ //不得不说自己这里真是太蠢了,下面写了j的循环,这里y就多了一个,所以就直接等于啊
            num++;
            return;
        }
        for(int i = x;i<n;i++){
            for(int j =0;j<n;j++){
                if(vis[j]==0 && graph[i][j]=='#'){
                    vis[j] = 1;
                    dfs(i+1,y+1);
                    vis[j] = 0;
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值