模板类 java 面试题_某东Java工程师笔试题解(含BFS通用通俗模板)

该博客介绍了如何运用BFS(广度优先搜索)算法解决一个二维地图上的路径寻找问题。地图由“#”表示的障碍物和“.”表示的可通行区域组成,起点A和终点B分别位于地图的不同位置。BFS模板框架在Java中实现,通过创建状态类记录坐标和步数,并使用队列进行搜索。在满足边界条件、未越界、未被访问且可以通行的情况下,更新状态并继续搜索。最终,如果找到目标状态则输出“yes”,否则输出“no”。
摘要由CSDN通过智能技术生成

题目

大致就是n行m列二维地图 A在(x,y),B在(a,b)。地图中有障碍物“#”表示不能通行,“.” 表示可以通行,A每次可以往上、下、左、右行走,不允许走出地图也不允许穿越障碍物。

判断A是否能成功走到B的位置

题解

思路就是BFS (套用BFS的模板框架) 判断条件为当前坐标是否与B的一致

BFS框架模板(Java)//BFS算法框架

boolean[][] mark = new boolean[n][m]; //访问标记

static int[][] go = {{0, -1}, {1, 0}, {-1, 0}, {0, 1}}; //方向向量

static class State

{

int x,y; //坐标位置

int step; //搜索步数记录

}

boolean Check(State s)

{

//边界判断

return s.x>=0&&s.x0&&s.y<=m;

}

void BFS(State st)

{

queue q;

State now,next;

st.step = 0; //步数清零;

q.add(st); //入队;

while(!q.isEmpty())

{

now = q.poll(); //队首元素出队;

if(now == 目标状态) //出现目标状态,此时的step为最小值,做做相关处理后退出即可;

{

……;

return ....;

}

//如果没有到目标状态

for(int i=0;i<4;i++)

{

next.x = now.x + go[i][0];//按照规则生成下一个状态

next.y = now.y + go[i][1];

if(CheckState(next)&&(根据题目修改条件)&&!mark) //未越界、未被访问、满足题目条件

{

next.step = now.step + 1;

mark[next.x][next.y]=true;

q.add(next);

}

}

}

}

public static void main(String[] args)

{

//输入

BFS();

//输出

}

题解代码public class AandB {

//初始化节点

static class Node {

int x;

int y;

public Node(int x, int y) {

this.x = x;

this.y = y;

}

public Node() {

}

}

//初始化全局变量

static int n;

static int m;

//行走的方向

static int[][] dir = {{0, -1}, {1, 0}, {-1, 0}, {0, 1}};

//a、b代表各自的坐标

static int ax = -1;

static int ay = -1;

static int bx = -1;

static int by = -1;

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int total = scanner.nextint();

while (total-- > 0) {

n = scanner.nextint();

m = scanner.nextint();

Boolean[][] visit = new Boolean[n][m];

//访问标记

String[] map = new String[n];

for (int i = 0; i < n; i++) {

map[i] = scanner.next();

}

//初始化坐标

for (int i = 0; i < n; i++) {

for (int j = 0; j < m; j++) {

if (map[i].charAt(j) == 'B') {

bx = i;

by = j;

}

if (map[i].charAt(j) == 'A') {

ax = i;

ay = j;

}

}

}

if (bfs(ax, ay, map, visit)) {

System.out.println("yes");

} else {

System.out.println("no");

}

}

}

//越界

static Boolean check(Node node) {

return node.x >= 0 && node.x < n && node.y >= 0 && node.y < m;

}

static Boolean bfs(int x, int y, String[] map, Boolean[][] visit) {

Queue queue = new LinkedList<>();

queue.add(new Node(x, y));

while (!queue.isEmpty()) {

Node node = queue.poll();

if (node.x == bx && node.y == by) {

return true;

}

for (int i = 0; i < 4; i++) {

Node next = new Node();

next.x = node.x + dir[i][0];

next.y = node.y + dir[i][1];

//判断条件

if (check(next) && (map[next.x].charAt(next.y) == '.' || map[next.x].charAt(next.y) == 'B') && !visit[next.x][next.y]) {

visit[next.x][next.y] = true;

queue.add(next);

}

}

}

return false;

}

}

输入输出数据2 //两组数据

2 2 //n行m列

.B

A.

2 2

#B

A#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值