关注
第三题核心代码:
// 从终点开始BFS
// 记录每个步数的频率
Map<Integer, Integer> t = new HashMap<>();
int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
boolean[][] visit = new boolean[n][m];
Queue<int[]> q = new LinkedList<>();
q.offer(new int[]{ex, ey, 0});
visit[ex][ey] = true;
while (!q.isEmpty()) {
int[] cur = q.poll();
int x = cur[0], y = cur[1], dis = cur[2];
// 访问当前节点并更新频率
t.put(dis, t.getOrDefault(dis, 0) + 1);
// 四个方向探查
for (int[] dir : dirs) {
int nx = x + dir[0], ny = y + dir[1];
if (0 <= nx && nx < n && 0 <= ny && ny < m && !visit[nx][ny] && map[nx][ny] != '1') { // 可以前进
q.offer(new int[]{nx, ny, dis + 1});
visit[nx][ny] = true;
}
}
}
查看原帖
1 1
相关推荐
牛客热帖
正在热议
# 25届秋招公司红黑榜 #
24240次浏览 192人参与
# 在找工作求抱抱 #
819883次浏览 9143人参与
# 字节求职进展汇总 #
279069次浏览 3213人参与
# 一人分享一句让你在秋招振作起来的话 #
11850次浏览 259人参与
# 海康威视求职进展汇总 #
297907次浏览 2843人参与
# 你最希望上岸的公司是? #
16044次浏览 176人参与
# 你的简历改到第几版了 #
650602次浏览 9524人参与
# 找实习多的是你不知道的事 #
1243148次浏览 17633人参与
# 海康威视求职进展 #
22141次浏览 154人参与
# 最后再改一次简历 #
1721672次浏览 27414人参与
# 如何写一份好简历 #
557886次浏览 8096人参与
# 我想象的实习vs现实的实习 #
216040次浏览 1920人参与
# 写简历别走弯路 #
586289次浏览 7266人参与
# 24届秋招同行攻略分享 #
809063次浏览 12317人参与
# 我的简历长这样 #
1656238次浏览 25937人参与
# 公司情报交流地 #
22469次浏览 148人参与
# 金融银行求职进展汇总 #
6540次浏览 51人参与
# 你已经投递多少份简历了 #
577225次浏览 7105人参与
# 产品人求职现状 #
100786次浏览 1306人参与
# 双非有机会进大厂吗 #
30136次浏览 277人参与