- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有一个二维数组long[50][50]
,其中填充了从 0 到 100 的随机数。我需要找到从最大(或第一高)到最小的最长路径.您可以上下左右移动。
我找到了如何找到单一的方法:找到最近的最大数字(但没有更大,它是)并移动到那里。
public static int measure = 50;
public long[][] map = new long[measure][measure];
我的移动方法:
private long moveUp(int x, int y) {
if (x >= measure || x == 0 || y == 0 || y >= measure) {
return -1;
}
return map[x - 1][y];
}
private long moveRight(int x, int y) {
if (x >= measure || x == 0 || y == 0 || y >= measure) {
return -1;
}
return map[x][y + 1];
}
private long moveDown(int x, int y) {
if (x >= measure || x == 0 || y == 0 || y >= measure) {
return -1;
}
return map[x + 1][y];
}
private long moveLeft(int x, int y) {
if (x >= measure || x == 0 || y == 0 || y >= measure) {
return -1;
}
return map[x][y - 1];
}
找到最近的最大的:
private long rightWay(int x, int y) {
List<Long> pickList = new ArrayList<>();
long up = moveUp(x, y);
long right = moveRight(x, y);
long down = moveDown(x, y);
long left = moveLeft(x, y);
if (up != -1 && up < map[x][y]) {
pickList.add(moveUp(x, y));
}
if (right != -1 && right < map[x][y]) {
pickList.add(moveRight(x, y));
}
if (down != -1 && down < map[x][y]) {
pickList.add(moveDown(x, y));
}
if (left != -1 && left < map[x][y]) {
pickList.add(moveLeft(x, y));
}
if (pickList.size() == 0) {
return -1;
} else {
Collections.sort(pickList);
for (int i = 0; i < pickList.size(); i++) {
System.out.println("right way " + i + " -> " + pickList.get(i));
}
return pickList.get(pickList.size() - 1);
}
}
然后只使用最近的最大值找到最长的路:
private void findRoute(long[][] route, long current, int width, int height) {
System.out.println("width = " + width + " height = " + height);
long nextSpetHeight = rightWay(width, height);
System.out.println("max = " + nextSpetHeight);
if (nextSpetHeight == -1) {
return;
} else {
if (nextSpetHeight == moveUp(width, height)) {
findRoute(route, nextSpetHeight, width - 1, height);
way.add(nextSpetHeight);
}
if (nextSpetHeight == moveRight(width, height)) {
findRoute(route, nextSpetHeight, width, height + 1);
way.add(nextSpetHeight);
}
if (nextSpetHeight == moveDown(width, height)) {
findRoute(route, nextSpetHeight, width + 1, height);
way.add(nextSpetHeight);
}
if (nextSpetHeight == moveLeft(width, height)) {
findRoute(route, nextSpetHeight, width, height - 1);
way.add(nextSpetHeight);
}
}
}
way
的大小就是这条路线的长度。但是现在我不知道如何从一些坐标中找到所有可能的路线以找到其中最长的路线。我的意思是我不知道返回“岔路口”并继续另一条路线的最佳方式是什么。
我希望解释清楚。提前致谢。
最佳答案
如果您将此问题视为有向图问题,则可以应用已知的图算法。
这是 Johnson's algorithm 的一个实现
首先,它在点矩阵上搜索局部最大值。他们将成为第一批候选人,然后算法对候选人进行迭代,它遵循约翰逊算法。并且它计算到任何点的所有长度。
public class Solver {
private static class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
private static class State {
static State best;
State parent;
List<State> children = new ArrayList<>();
int length;
Point p;
public State(State parent, Point p) {
this.parent = parent;
this.p = p;
this.length = parent.length + 1;
this.parent.children.add(this);
if (best.length < length) {
best = this;
}
}
public State(Point p) {
this.parent = null;
this.p = p;
this.length = 1;
if (best == null) {
best = this;
}
}
public void checkParent(State st) {
if (st.length + 1 > length) {
parent.children.remove(this);
this.parent = st;
updateLength();
}
}
private void updateLength() {
this.length = parent.length + 1;
if (best.length < length) {
best = this;
}
for (State state : children) {
state.updateLength();
}
}
}
public static boolean checkRange(int min, int max, int x) {
return min <= x && x < max;
}
public static boolean maxLocal(int x, int y, int[][] points) {
int value = points[x][y];
if (x > 0 && points[x - 1][y] > value) {
return false;
}
if (y > 0 && points[x][y - 1] > value) {
return false;
}
if (x < points.length - 1 && points[x + 1][y] > value) {
return false;
}
return !(y < points[0].length - 1 && points[x][y + 1] > value);
}
private static List<Point> getNeigbours(int x, int y, int[][] points) {
int value = points[x][y];
List<Point> result = new ArrayList<>(4);
if (x > 0 && points[x - 1][y] < value) {
result.add(new Point(x - 1, y));
}
if (y > 0 && points[x][y - 1] < value) {
result.add(new Point(x, y - 1));
}
if (x < points.length - 1 && points[x + 1][y] < value) {
result.add(new Point(x + 1, y));
}
if (y < points[0].length - 1 && points[x][y + 1] < value) {
result.add(new Point(x, y + 1));
}
return result;
}
private static int[][] generateRandomPoint(int width, int height, int max) {
int[][] result = new int[width][height];
Random rand = new Random(0L);
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[i].length; j++) {
result[i][j] = rand.nextInt(max);
}
}
return result;
}
public static void main(String[] args) {
int[][] points = generateRandomPoint(50, 50, 100);
State[][] states = new State[points.length][points[0].length];
List<State> candidates = new ArrayList<>(points.length*points[0].length);
for (int x = 0; x < points.length; x++) {
for (int y = 0; y < points[0].length; y++) {
if (maxLocal(x, y, points)) {
states[x][y] = new State(new Point(x, y));
candidates.add(states[x][y]);
}
}
}
while (!candidates.isEmpty()) {
State candidate = candidates.remove(candidates.size() - 1);
for (Point p : getNeigbours(candidate.p.x, candidate.p.y, points)) {
if (states[p.x][p.y] == null) {
states[p.x][p.y] = new State(candidate, p);
candidates.add(states[p.x][p.y]);
} else {
states[p.x][p.y].checkParent(candidate);
}
}
}
State temp = State.best;
List<Point> pointList = new ArrayList<>(temp.length);
while (temp != null) {
pointList.add(temp.p);
temp = temp.parent;
}
for (int x = 0; x < points.length; x++) {
for (int y = 0; y < points[0].length; y++) {
if (points[x][y] < 10) {
System.out.print(" ");
} else if (points[x][y] < 100) {
System.out.print(" ");
}
System.out.print(points[x][y] + " ");
}
System.out.println();
}
System.out.println("-------");
for (Point point : pointList) {
System.out.println(point.x + ", " + point.y + " -> " + points[point.x][point.y]);
}
System.out.println();
System.out.println("lengths:");
for (int x = 0; x < points.length; x++) {
for (int y = 0; y < points[0].length; y++) {
if (states[x][y].length < 10) {
System.out.print(" ");
}
System.out.print(states[x][y].length + " ");
}
System.out.println();
}
}
}
它打印(10 x 10 矩阵)
第一个 block :二维矩阵 (10x10)
第二 block :从最小值到最大值的最长解的坐标。
最后一个 block :坐标的长度。
输出:
60 48 29 47 15 53 91 61 19 54
77 77 73 62 95 44 84 75 41 20
43 88 24 47 52 60 3 82 92 23
45 45 37 87 2 62 25 53 38 35
60 75 55 30 98 91 74 36 12 62
19 77 16 46 7 16 8 37 43 47
87 88 5 58 8 17 51 18 58 18
38 72 57 51 26 80 97 62 35 20
67 73 17 69 5 52 89 43 1 41
23 80 68 14 16 23 57 22 5 71
-------
5, 4 -> 7
6, 4 -> 8
7, 4 -> 26
7, 3 -> 51
7, 2 -> 57
7, 1 -> 72
8, 1 -> 73
9, 1 -> 80
lengths:
2 3 6 5 6 2 1 4 5 1
1 2 3 4 1 5 2 3 4 5
6 1 7 6 5 4 5 2 1 4
5 4 5 1 6 3 4 3 4 3
4 3 4 5 1 2 3 5 5 1
5 2 5 2 8 4 5 4 3 2
2 1 5 1 7 3 2 4 1 5
4 3 4 5 6 2 1 2 3 4
3 2 5 1 7 3 2 3 4 2
4 1 2 6 5 4 3 4 5 1
关于java - 二维数组中从最大值到最小值的最长路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39128399/
我在使用 Android 时遇到了一点问题。 我有我的 GPS 位置,明确的经纬度,以及以米为单位的搜索射线(例如 100 米),可以吗? 想象一下我在射线形成的圆心的位置,我会知道如何在 Andro
深夜的编程之旅 这是一个深夜,街头灯光昏暗,大部分人都已陷入梦乡。但对于我来说,这却是一个灵感迸发的时刻。窗外的星空仿佛在诉说着某种宇宙的密码,而键盘下的代码则是我解密这个宇宙的工具。 一个突如其来的
我将数据集结构定义为 struct Dataset: Hashable { var x: Double var y: Double } 然后是数组 var dataset: [Data
我在 Excel 文件中有一个摘要选项卡,需要查看应计选项卡才能找到 Max和 Min .我遇到的问题是有许多不同的位置/商品组合,我需要找到 Max和 Min基于位置/商品组合。位置和商品位于两个单
我有一个 Excel 表,其中包含两列感兴趣的年份和捐款。年份值为 2008,2009,2010 等... 我想获得 2009 年所有捐款中的最低金额。我试过了 MIN(IF(Year="2009",
到现在为止,我刚刚找到了为列表中多个数据帧中的列获取最大值的解决方案。 我已经将数据帧 df1, df2, df3, ..., dfn 存储在列表 dfList 中,我想获取列 df_ 的最大值$a
假设我有一个列名列表作为向量: vec=c("C1" , "C2" ,"C3"). 我知道这些列名来自数据框 df: df: C1 C2 C3 C4 C5 1 2 3 4 5 1 4
我需要计算大数组的最小值/最大值。我知道Math.max.apply() ,但在大型数组上,它会因堆栈溢出异常而失败。有什么简单的解决方案吗? 最佳答案 使用 sort() 对数组进行排序方法它使用快
例如,我有一个像这样的模型: class Record(models.Model): name = CharField(...) price = IntegerField(...)
我正在编写一个用于测试听力的简单应用,并且正在使用Audiotrack生成纯音。因为它是用于测试听力的应用程序,所以我使用非常低的音量来播放这些音调。 要设置音量,我使用音轨的 setVolume(f
Example data set 对,上面是我的数据集子段图像的链接。它以 3 列为一组,第一个是浓度,第二个是限定值,最后一个是 MDL - 并持续最多 95 个 sample (因此总共 285
我想计算 df 的每 n 行的最小值/最大值,比如 10,但是使用 df.rolling(10).max() 给出第 0-9、1-10、2-11 行的值等。我想要 0-9、10-19、20-29 等
我被问到了关于 c# 的同样问题 here我发现通过使用 linq 你可以轻松地做到这一点。 但是既然 java 中的 linq 没有其他选择,我该如何简单地做到这一点呢? 最佳答案 如果您想要类似于
我曾经使用过数组,并且知道如何对使用数值(double 和 int)的数组进行排序,但我必须使用字符串数组制作相同的应用程序。我的教授不允许我发挥“创造力”,也不允许我与其他可能有助于完成这项工作的静
我想知道通过这样的回溯获得某些事实的最大值(最年长的人)是否是个好主意: data(MaxID, MaxName, MaxAge), \+ (data(ID, Name, Age), ID \= Ma
我想计算 df 的每 n 行的最小值/最大值,比如 10,但是使用 df.rolling(10).max() 给出第 0-9、1-10、2-11 行的值等。我想要 0-9、10-19、20-29 等
我的数据如下所示: df <- tribble( ~A, ~B, 0.2, 0.1, 0.2, 0.3, 0.5, 0.1, 0.7, 0.9,
我有以下数据集 Date Category 2014-01-01 A 2014-01-02 A 2014-01-03 A 2014-01-04
我是使用 Python 进行数据分析的初学者,并且坚持以下几点: 我想使用广播/矢量化方法从各个列 (pandas.dataframe) 中找到最大值(value)。 我的数据框的快照如下: 最佳答案
C99 中是否有一个标准函数来使用给定的比较函数获取给定数组中的最小/最大元素。 类似: void* get_min(void* start,size_t size,size_t elementSiz
我是一名优秀的程序员,十分优秀!