第8组软件2207邓子豪


私信TA

用户名:xx118

访问量:255

签 名:

这不是一个bug,这只是一个未列出来的特性。

等  级
排  名 1534
经  验 2710
参赛次数 4
文章发表 3
年  龄 0
在职情况 学生
学  校 武软
专  业 软件技术

  自我简介:

解题思路:使用宽度优先搜索解题

注意事项:题目测试数据不好,一定要使用{ { -1, 0 }, { 0, -1 }, { 1, 0 }, { 0, 1 } };这个顺序不然会报答案错误.
参考代码:

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main{
static int arrays[][] = new int[5][5];
static boolean bu[][] = new boolean[5][5];
static int direction[][] = { { -1, 0 }, { 0, -1 }, { 1, 0 }, { 0, 1 } };
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
arrays[i][j] = scanner.nextInt();
}
}
sout(bfs());
}
static Connect bfs() {
Queue<Connect> queue = new LinkedList<>();
Connect temp = new Connect(0, 0, null);
queue.add(temp);
while (!queue.isEmpty()) {
temp = queue.poll();
int x1 = temp.x;
int y1 = temp.y;
if (x1 == 4 && y1 == 4) {
break;
}
for (int i = 0; i < 4; i++) {
int x = x1 + direction[i][0];
int y = y1 + direction[i][1];
if (x < 5 && x >= 0 && y < 5 && y >= 0 && arrays[x][y] != 1 && !bu[x][y]) {
Connect temp1 = new Connect(x, y, temp);
queue.add(temp1);
bu[x][y] = true;
}
}
}
return temp;
}
static void sout(Connect temp) {
Connect tempConnect = temp.Rootnode;
if (tempConnect == null) {
System.out.println("(0, 0)");
return;
}
sout(tempConnect);
System.out.println("(" + temp.x + ", " + temp.y + ")");
}
}
class Connect {
int x;
int y;
Connect Rootnode;
public Connect(int x, int y, Connect rootnode) {
super();
this.x = x;
this.y = y;
Rootnode = rootnode;
}
}


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区