中级农民
- 积分
- 120
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2016-12-8
- 最后登录
- 1970-1-1
|
恩,是的。下面是我的代码大家参考参考:- import java.util.LinkedList;
- import java.util.Queue;
- public class PrintShortestPath {
- public Node findPath(char[][] board, Node start, Node end) throws Exception {
- if (board == null || board.length == 0 || board[0].length == 0) {
- throw new Exception("invalid input");
- }
- int[] x = { 0, 0, 1, -1 };
- int[] y = { 1, -1, 0, 0 };
- int n = board.length;
- int m = board[0].length;
- boolean[][] visited = new boolean[n][m];
- Queue<Node> queue = new LinkedList<>();
- queue.offer(start);
- visited[start.curi][start.curj] = true;
- while (!queue.isEmpty()) {
- Node cur = queue.poll();
- for (int k = 0; k < 4; k++) {
- int nexti = cur.curi + x[k];
- int nextj = cur.curj + y[k];
- if (inBound(nexti, nextj, board) && !visited[nexti][nextj] && board[nexti][nextj] != '1') {
- Node newNode = new Node(nexti, nextj, cur);
- if (newNode.equals(end)) {
- return newNode;
- }
- queue.offer(newNode);
- visited[nexti][nextj] = true;
- }
- }
- }
- return null;
- }
- private boolean inBound(int nexti, int nextj, char[][] board) {
- if (nexti < 0 || nextj < 0 || nexti >= board.length || nextj >= board[0].length) {
- return false;
- }
- return true;
- }
- public void printPath(Node node) {
- StringBuilder sb = new StringBuilder();
- Node last = node;
- while (last != null) {
- sb.insert(0, "(" + last.curi + "," + last.curj + ")");
- last = last.prevNode;
- }
- System.out.println(sb.toString());
- }
- public static void main(String[] args) {
- PrintShortestPath sol = new PrintShortestPath();
- char[][] board = { { '0', '0', '1', '0', '0' }, { '0', '0', '0', '0', '0' }, { '0', '0', '1', '0', '0' },
- { '0', '0', '1', '1', '0' } };
- try {
- Node start = new Node(1, 1, null);
- Node end = new Node(0, 4, null);
- end = sol.findPath(board, start, end);
- sol.printPath(end);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- class Node {
- Integer curi;
- Integer curj;
- Node prevNode;
- public Node(Integer ci, Integer cj, Node prev) {
- curi = ci;
- curj = cj;
- prevNode = prev;
- }
- public int hashCode() {
- return curi;
- }
- public boolean equals(Object obj) {
- boolean flag = false;
- Node n = (Node) obj;
- if (n.curi == curi && n.curj == curj) {
- flag = true;
- }
- return flag;
- }
- }
复制代码 |
|