迷宫最近距离
2018-02-27 14:40
113 查看
广搜代码如下:import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main
{
public static final int[][] maze = new int[][] {
{0,1,0,0,0,1,1,1,0,1,0,1},
{0,0,0,1,0,0,0,0,1,0,0,1},
{0,1,0,1,0,1,1,1,0,1,0,0},
{0,1,0,0,0,0,0,1,0,0,1,1},
{0,0,0,0,1,0,0,0,0,0,0,0},
{0,0,1,0,0,0,1,0,0,0,1,0},
{0,0,1,0,0,0,0,0,1,0,0,0},
{1,0,0,1,0,1,0,0,0,1,0,1},
{0,0,1,0,1,0,1,0,1,0,0,0},
{0,0,0,0,0,1,0,0,0,1,1,0},
{0,0,0,0,0,1,0,0,0,0,0,0},
{0,1,0,1,0,0,0,1,0,1,0,0}
};
public static final int N = 12;
public static int sx, sy, gx, gy;
public static int[] dx = new int[] {1, 0, -1, 0};
public static int[] dy = new int[] {0, 1, 0, -1};
public static int[][] d = new int[150][150];//记录到该点最小步数
public static final int INF = 10000000;
public static class Point
{
public int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
public static void main(String[] args)
{
Scanner cin = new Scanner(System.in);
sx = cin.nextInt();
sy = cin.nextInt();
gx = cin.nextInt();
gy = cin.nextInt();
cin.close();
System.out.println(bfs());
}
public static int bfs()
{
Queue<Point> queue = new LinkedList<Point>();
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
{
d[i][j] = INF;//初始化
}
}
queue.add(new Point(sx, sy));
d[sx][sy] = 0;//到起点的步数为0
while (!queue.isEmpty())
{
Point p = queue.poll();
if (p.x == gx && p.y == gy) break;
for (int i = 0; i < 4; ++i)
{
int nx = p.x + dx[i];
int ny = p.y + dy[i];
if (nx >= 0 && nx < N && ny >= 0 && ny < N && maze[nx][ny] != 1 && d[nx][ny] == INF)
{
queue.add(new Point(nx, ny));
d[nx][ny] = d[p.x][p.y] + 1;
}
}
}
//到达不了的情况
if (d[gx][gy] == INF)
{
return 10000;
}
return d[gx][gy];
}
}
=============================我是一个反应迟钝的程序员==============================
相关文章推荐
- 圆周率,孪生素数,反转串,最近距离,基因牛,迷宫问题(2011年蓝桥杯预赛java本科组试题)
- 一个求迷宫入口到出口最近距离的程序 JAVA版本
- 一个求迷宫入口到出口最近距离的程序
- 空间两条直线段的最短距离及最近点计算
- 第二届2011年国信蓝点杯软件设计大赛预赛的试题最近距离
- 二叉排序树中,令f = (最大值+最小值) / 2,设计一个算法, 找出距离f值最近、大于f值的结点。复杂度不能为O(n2)。
- 单词最近距离
- hdoj1007(最近距离点对)Quoit Design
- 三维空间碰撞问题;空间中两直线的最短距离及最近点
- 分治求点集中的最近距离 version 0.1
- 14、一棵排序二叉树,令 f=(最大值+最小值)/2, 设计一个算法,找出距离f值最近、大于f值的结点。
- 空间想象能力挑战:立方体相邻面两对角线的最近距离?
- 分治,在平面上找距离最近的两个点
- 点到三维网格最近边的距离
- 找出平面上距离最近的两个点
- hdu 1007 Quoit Design 找最近的两个点的距离
- ESL作业笔记2.3:均匀分布情况下最近邻距离的中值
- 计算几何 平面最近点对 nlogn分治算法 求平面中距离最近的两点
- 如何计算点到线段的最近距离
- 【K-D树 在限制条件下求最近欧几里德距离】HDU - 5992 Finding Hotels