题目内容为一个棋盘,长宽自己输入,棋盘状态自己输入(.表示无棋子,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