走棋盘问题(BFS问题)

题目内容为一个棋盘,长宽自己输入,棋盘状态自己输入(.表示无棋子,o表示有棋子),可以全部进行上移、下移、左移、右移操作,遇到边界棋子丢失,计算出最短步数使棋盘上仅剩下k个棋子。

import java.util.*;
public class BFS {

    public static int n = 0;
    public static int m = 0;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String[] strs = s.split(" ");
        n = Integer.parseInt(strs[0]);
        m = Integer.parseInt(strs[1]);
        char[][] ch = new char[n][m];
        for(int i = 0; i < n; i++){
            String str = sc.nextLine();
            ch[i] = str.toCharArray();
        }

        int k = sc.nextInt();
        System.out.print(bfs(ch,k));
    }

    public static int bfs(char[][] ch, int k){
        int currentK = count(ch);
        if(currentK < k)
            return -1;
        if(currentK == k)
            return 0;

        Queue<char[][]> queue = new ArrayDeque<>();
        int step = 0;
        queue.offer(ch);
        Queue<In
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值