算法:走迷宫

package com.bxh.algorithms.OtherAlgorithms;

import java.util.Queue;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * Created by bxh on 7/27/17.
 */

public class BFS {
    int[][] maze = {
            {0, 1, 0, 0, 0},
            {0, 1, 0, 1, 0},
            {0, 0, 0, 0, 0},
            {0, 1, 1, 1, 0},
            {0, 0, 0, 1, 0}
    };
    Node[][] nodes = new Node[maze.length][maze[0].length];

    int[][] dir = {
            {0, 1},//右
            {1, 0},//下
            {0, -1},//左
            {-1, 0}//上
    };

    public void test() {
        //init nodes arrays
        initNodes();
        boolean x = getPath(nodes[0][0], nodes[4][4]);
    }

    public void initNodes() {
        //init nodes
        for (int i = 0; i < maze.length; i++) {
            for (int j = 0; j < maze[0].length; j++) {
                nodes[i][j] = new Node(i, j, maze[i][j]);
            }
        }
    }

    public boolean getPath(Node start, Node end) {
        //real calculation
        Queue<Node> queue = new LinkedBlockingDeque();
        start.visted = true;
        queue.add(start);
        while (queue.size() > 0) {
            Node cur = queue.poll();
            if (cur.equals(end)) {
                System.out.println("---new cur ,end !");
                print(cur);
                return true;
            }
            //四个方向
            for (int i = 0; i < 4; i++) {
                //相邻节点
                int lx = cur.x + dir[i][0];
                int ly = cur.y + dir[i][1];
                System.out.println("---lx=" + lx + "---ly=" + ly + "--i=" + i);
                System.out.println("---cur.x=" + cur.x + "---cur.y=" + cur.y + "--i=" + i);
                if (lx < 0 || ly < 0 || lx >= 5 || ly >= 5) {
                    continue;
                }
                Node lNode = nodes[lx][ly];
                //已经访问过,不走回头路
                if (lNode.visted) {
                    System.out.println("---already visited");
                    print(lNode);
                    continue;
                }
                lNode.visted = true;
                if (lNode.v == 1) {
                    continue;
                }
                if (lNode.equals(cur)) {
                    System.out.println("---this is end node");
                    print(lNode);
                    return true;
                } else {
                    queue.add(lNode);
                    System.out.println("----add to queue");
                    print(lNode);
                }
            }


        }
        return false;
    }

    public void print(Node n) {
        System.out.println("pos is (" + n.x + "," + n.y + ")");
    }

    static class Node {
        int x;
        int y;
        int v;
        boolean visted;

        Node(int[] a) {
            x = a[0];
            y = a[1];
        }

        Node(int a, int b) {
            x = a;
            y = b;
        }

        Node(int a, int b, int c) {
            x = a;
            y = b;
            v = c;
        }

        @Override
        public boolean equals(Object obj) {
            Node n = (Node) obj;
            if (n.x == this.x && n.y == this.y && n.v == this.v) {
                return true;
            }
            return super.equals(obj);
        }
    }

}

0:可以通过

1:禁止通行

上面的代码画个图的话大致是走了下图的路线,能到达终点的路线不止一条。





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值