我是靠谱客的博主 开放吐司,这篇文章主要介绍地下迷宫(bfs),现在分享给大家,希望可以做个参考。

1.地下迷宫

滴滴出行2017秋招工程岗笔试题(0918)编程题

滴滴出行2017秋招工程岗笔试题(0918)编程题

滴滴出行2017秋招工程岗笔试题(0918)编程题

思路:用广度优先搜索算法,即bfs,因为输入数据行数和列数为[3,10],所在在搜索路径时,可以用x*10+y来表示其对应的坐标,并且将其加入对应的hash结点中。另一方面要求输出消耗最小的路径,所以队列使用优先级队列,保证每次从队列中取出的总是消耗最小的状态结点。而搜索时,防止重复搜索,用vis数组来表示是否已经访问过,并且访问时,如果出现越界或者能量值小于0时,可以不考虑这些状态结点

具体代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import java.util.Scanner; import java.io.PrintWriter; import java.io.FileInputStream; import java.io.BufferedInputStream; import java.io.OutputStreamWriter; import java.io.IOException; import java.util.Map; import java.util.HashMap; import java.util.Queue; import java.util.PriorityQueue; import java.util.Comparator; import java.util.List; import java.util.ArrayList; public class Main implements Runnable { private Scanner cin; private PrintWriter cout; private boolean DEBUG = true; private int n, m, p; private Map<Integer, StateNode> hm; private Queue<StateNode> queue; private int[][] matrix; private boolean[][] vis; private int[] dirx = {-1, 0, 1, 0}; private int[] diry = {0, 1, 0, -1}; private int[] dirp = {-3, -1, 0, -1}; class StateNode { int x, y, step, p, pre; StateNode() { x = 0; y = 0; step = 0; p = 0; pre = -1; } StateNode(int x, int y, int step, int p, int pre) { this.x = x; this.y = y; this.step = step; this.p = p; this.pre = pre; } } private void init() { try { if (DEBUG) { cin = new Scanner(new BufferedInputStream(new FileInputStream("f:\OJ\uva_in.txt"))); } else { cin = new Scanner(new BufferedInputStream(System.in)); } cout = new PrintWriter(new OutputStreamWriter(System.out)); } catch (IOException e) { e.printStackTrace();; } } private int hashVal(StateNode node) { return node.x * 10 + node.y; } private boolean isTarget(StateNode node) { return node.x == 0 && node.y == m - 1 && node.p >= 0; } private boolean is_impossible(int x, int y, int curp) { if (x < 0 || x >= n || y < 0 || y >= m || curp < 0 || matrix[x][y] == 0 || vis[x][y]) return true; return false; } private boolean input() { if (!cin.hasNextInt()) return false; n = cin.nextInt(); m = cin.nextInt(); p = cin.nextInt(); matrix = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { matrix[i][j] = cin.nextInt(); } } return true; } private void solve() { StateNode node = new StateNode(0, 0, 0, p, -1); hm = new HashMap<>(); Comparator<StateNode> cmp = new Comparator<StateNode>() { @Override public int compare(StateNode o1, StateNode o2) { return o2.p - o1.p; } }; queue = new PriorityQueue<StateNode>(cmp); queue.add(node); hm.put(hashVal(node), node); vis = new boolean[n][m]; int ans = -1; while (!queue.isEmpty()) { StateNode cur_node = queue.poll(); if (isTarget(cur_node)) { ans = hashVal(cur_node); break; } vis[cur_node.x][cur_node.y] = true; for (int i = 0; i < 4; i++) { int new_x = cur_node.x + dirx[i]; int new_y = cur_node.y + diry[i]; int new_p = cur_node.p + dirp[i]; int new_step = cur_node.step + 1; if (is_impossible(new_x, new_y, new_p)) continue; StateNode new_node = new StateNode(new_x, new_y, new_step, new_p, hashVal(cur_node)); queue.add(new_node); hm.put(hashVal(new_node), new_node); } } if (ans == -1) { cout.println("Can not escape!"); } else { List<Integer> path = new ArrayList<>(); while (ans != -1) { path.add(ans); ans = hm.get(ans).pre; } for (int i = path.size() - 1; i >= 0; i--) { cout.print("[" + Integer.toString(path.get(i) / 10) + "," + Integer.toString(path.get(i) % 10) + "]"); if (i != 0) cout.print(','); } } cout.flush(); } public void run() { init(); while (input()) { solve(); } } public static void main(String[] args) { new Thread(new Main()).start(); } }


最后

以上就是开放吐司最近收集整理的关于地下迷宫(bfs)的全部内容,更多相关地下迷宫(bfs)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部