我得到了几个坐标点:
来源(0,0)
目的地(m,n)
一组坐标点S = {(x,y)
,以便0 < x < m
和0 < y < n}
目标是找出(0,0)
和(m,n)
之间的最短路径数,以便在这些路径中永远不会遇到集合S
中的任何点。我怎么找到它?
答案 0 :(得分:3)
这里有一个C#解决方案,但可以轻松转换为Java。希望你会发现它很有用。
using System;
using System.Collections.Generic;
using System.Drawing;
namespace Game
{
class Program
{
/// <summary>
/// Find a matrix with all possible optimum paths from point A to point B
/// </summary>
/// <param name="rows">Rows of the matrix</param>
/// <param name="cols">Cols of the matrix</param>
/// <param name="points">Obstacles location</param>
/// <param name="moves">Allowed moves</param>
/// <param name="matrix">Resulting matrix</param>
/// <param name="A">Starting point</param>
/// <param name="B">Ending point</param>
private static void FindMatrix(int rows, int cols, List<Point> points, List<Point> moves, out List<List<int>> matrix, out Point A, out Point B)
{
matrix = new List<List<int>>();
A = new Point(-1, -1);
B = new Point(-1, -1);
//Init values of the matrix
for (int row = 0; row <= rows; row++)
{
matrix.Add(new List<int>());
for (int col = 0; col <= cols; col++)
matrix[matrix.Count - 1].Add(0);
}
var index = 0;
while ((index < points.Count) && (points[index].Y >= 0) && (points[index].Y <= rows) && (points[index].X >= 0) && (points[index].X <= cols))
{
matrix[points[index].Y][points[index].X] = -1;
index++;
}
if ((index == points.Count) && (matrix[0][0] == 0) && (matrix[rows][cols] == 0))
{
A.X = 0;
A.Y = 0;
B.X = cols;
B.Y = rows;
}
if ((A.X >= 0) && (A.Y >= 0) && (B.X >= 0) && (B.Y >= 0)) //To check if points A and B exist in the board
{
var pairs = new List<Point>[2] { new List<Point>(), new List<Point>() };
int level = 0;
index = 0;
pairs[index].Add(A);
while ((pairs[index].Count > 0) && (pairs[index][pairs[index].Count - 1] != B))
{
pairs[Math.Abs(1 - index)].Clear();
level++;
foreach (var pair in pairs[index])
foreach (var move in moves) //Test all possible moves
if ((pair.Y + move.Y >= 0) && (pair.Y + move.Y < matrix.Count) && (pair.X + move.X >= 0) && (pair.X + move.X < matrix[pair.Y + move.Y].Count) && (matrix[pair.Y + move.Y][pair.X + move.X] == 0)) //Inside the boundaries? Not visited before?
{
pairs[Math.Abs(1 - index)].Add(new Point(pair.X + move.X, pair.Y + move.Y));
matrix[pair.Y + move.Y][pair.X + move.X] = level;
}
index = Math.Abs(1 - index);
}
matrix[A.Y][A.X] = 0;
}
}
/// <summary>
/// Finds all possible optimum paths from point A to point B in a matix with obstacles
/// </summary>
/// <param name="matrix">Matrix with obstacles</param>
/// <param name="moves">Allowed moves</param>
/// <param name="A">Starting point</param>
/// <param name="B">Ending point</param>
/// <param name="result">Resulting optimum paths</param>
/// <param name="list">Temporary single optimum path</param>
private static void WalkMatrix(List<List<int>> matrix, List<Point> moves, Point A, Point B, ref List<List<Point>> result, ref List<Point> list)
{
if ((list.Count > 0) && (list[list.Count - 1] == B)) //Stop condition
{
result.Add(new List<Point>(list));
}
else
{
foreach (var move in moves)
if ((A.Y + move.Y >= 0) && (A.Y + move.Y < matrix.Count) && (A.X + move.X >= 0) && (A.X + move.X < matrix[A.Y + move.Y].Count) && (matrix[A.Y + move.Y][A.X + move.X] == matrix[A.Y][A.X] + 1)) //Inside the boundaries? Next step?
{
list.Add(new Point(A.X + move.X, A.Y + move.Y)); //Store temporary cell
WalkMatrix(matrix, moves, list[list.Count - 1], B, ref result, ref list);
list.RemoveAt(list.Count - 1); //Clean temporary cell
}
}
}
public static List<List<Point>> FindPaths(int rows, int cols, List<Point> points)
{
var result = new List<List<Point>>();
var moves = new List<Point> { new Point(1, 0), new Point(0, 1), new Point(-1, 0), new Point(0, -1) }; //Right, Down, Left, Up (clockwise)
List<List<int>> matrix; //Matrix temporary representation to store all possible optimum paths
Point A; //Starting point
Point B; //Ending point
FindMatrix(rows, cols, points, moves, out matrix, out A, out B);
if ((A.X >= 0) && (A.Y >= 0) && (B.X >= 0) && (B.Y >= 0)) //To check if points A and B exist
{
List<Point> list = new List<Point>();
list.Add(A);
WalkMatrix(matrix, moves, A, B, ref result, ref list);
}
return result;
}
static void Main(string[] args)
{
var points = new List<Point>
{
new Point(3, 2),
new Point(4, 2),
new Point(5, 2),
new Point(3, 3),
new Point(4, 3),
new Point(5, 3),
new Point(3, 4),
new Point(4, 4),
new Point(5, 4)
};
List<List<Point>> paths = FindPaths(5, 10, points); //path.Count store the quantity of optimum paths
}
}
}
答案 1 :(得分:1)
这可以通过动态编程来解决。
首先,使用广度优先搜索找到每个节点的原点距离。
然后,通过距离为F
的点(x,y)
的最短路径d
的数量只是所有{{1}的F(x1,y1)
的总和距离(x1, y1)
的邻近(x,y)
。
换句话说,d-1
答案 2 :(得分:1)
我认为这是一个完整的解决方案,但您可能需要对其进行测试并将其转换为java。
基本思想是首先搜索网格。在网格中的每个点,到达该点的方式的数量等于在距离较少的那一点上接近该点的方式的数量。
n, m = 10, 15 #10 by 15 grid say
s = set([(1, 1), (2, 2), (9, 14)])
grid = []
ways = []
for i in range(n + 1):
grid.append([None]*(m + 1))
ways.append([0]*(m + 1))
start = (0, 0)
end = (n, m)
grid[0][0] = 0
ways[0][0] = 1
fringe = [start]
distance = 0
new_fringe = []
while True:
for node in new_fringe:
i, j = node
deltas = [(-1, 0), (1, 0), (0, 1), (0, -1)] #directions to travel
for di, dj in deltas:
i2, j2 = i + di, j + dj
if 0 <= i2 <= n and 0 <= j2 <= m and grid[i2][j2] == distance - 1:
ways[i][j] += ways[i2][j2]
new_fringe = []
for node in fringe:
i, j = node
deltas = [(-1, 0), (1, 0), (0, 1), (0, -1)] #directions to travel
for di, dj in deltas:
i2, j2 = i + di, j + dj
if 0 <= i2 <= n and 0 <= j2 <= m and (i2, j2) not in s and grid[i2][j2] is None:
grid[i2][j2] = distance + 1
new_fringe.append((i2, j2))
if not new_fringe: #no new nodes
break
fringe = new_fringe
distance += 1
for row in grid:
print row
"""[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
[1, None, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
[2, 3, None, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17],
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18],
[4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
[6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21],
[7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22],
[8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
[9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, None, 24],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]]"""
for row in ways:
print row
"""[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
[1, 1, 0, 2, 5, 9, 14, 20, 27, 35, 44, 54, 65, 77, 90, 104]
[1, 2, 2, 4, 9, 18, 32, 52, 79, 114, 158, 212, 277, 354, 444, 548]
[1, 3, 5, 9, 18, 36, 68, 120, 199, 313, 471, 683, 960, 1314, 1758, 2306]
[1, 4, 9, 18, 36, 72, 140, 260, 459, 772, 1243, 1926, 2886, 4200, 5958, 8264]
[1, 5, 14, 32, 68, 140, 280, 540, 999, 1771, 3014, 4940, 7826, 12026, 17984, 26248]
[1, 6, 20, 52, 120, 260, 540, 1080, 2079, 3850, 6864, 11804, 19630, 31656, 49640, 75888]
[1, 7, 27, 79, 199, 459, 999, 2079, 4158, 8008, 14872, 26676, 46306, 77962, 127602, 203490]
[1, 8, 35, 114, 313, 772, 1771, 3850, 8008, 16016, 30888, 57564, 103870, 181832, 0, 203490]
[1, 9, 44, 158, 471, 1243, 3014, 6864, 14872, 30888, 61776, 119340, 223210, 405042, 405042, 608532]"""
答案 3 :(得分:0)
我假设您正在谈论(x,y)中定义的点网格。在你想要计算最短路径集合的两点之间,这是一个更障碍
所以我们把问题分解为两个 1-找到从A点到B点的一组最短路径 2-确保在这些路径的每条路径上,没有任何属于S的点位于路径上
要解决这个问题,那里有很多算法,我想到的第一个是强化学习 这是它的一个视觉示例in this link 您可以使用此库或自己实现它。它相当容易
两种算法都可以用于网格,在这种情况下,您可以将某些点标记为障碍物,即您的点S集,并且在检测路径时,将避免使用集合S的点。
希望这有帮助
答案 4 :(得分:0)
如果只允许整数坐标,它可以简单地简化为图形问题。
这可能是在每场比赛中完成的。图形看起来像一个矩形,一个角落的源头,另一个角落的目的地,以及通过垂直或水平边缘连接的节点。 (某些节点已被删除,集合S)。
现在,这里的最短路径是角落之间的Dyck路径的子集,因此加泰罗尼亚数字是上限。 http://mathworld.wolfram.com/DyckPath.html
答案 5 :(得分:-1)
尝试查看Dijkstra的算法:http://en.wikipedia.org/wiki/Dijkstra%27s%5Falgorithm