- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在构建一个迷宫赛跑者机器人,其目标是能够从给定的起点到给定的终点穿越迷宫。我自己设计了算法 - 我坚持在查看以前的工作之前让我自己的实现工作。
部分算法涉及对方向进行排序(使用插入排序,因为数组很小)- forward, backward, up ,并且下降他们的前景如何。
我在排序时遇到了一些问题,它根据三个因素对方向进行排序
排序时,我比较因子1,如果相等,我比较因子2。如果因子2相等,我继续3。如果任何两个因子小于或< em>超过,我返回了。出于某种原因,有时状态较低的路径会被推到后面。我对路径的排序出了点问题。
enter code here
有什么想法吗?
/*------------( Getting the best direction to go in )---------------- */
/*
* 0: North
* 1: East
* 2: South
* 3: West
*/
int bestDirection(Point _curr_pos){
Vec4 statuses/*1 for unexplored(better), 2 for explored(worse)*/,
distances ,
lengths = collectDistancesToWalls(),
availablities = {POINT_REACHABLE, POINT_REACHABLE, POINT_REACHABLE, POINT_REACHABLE},
directions = {0,1,2,3};
//Give directions a length rating and distance rating and then sorts the directions, leaving the best direction in the front.
//If we discover that we have already been in a location, we should block off the passage behind us leading to that location because it must be a loop.
//Collecting the distance and length data.
for (int i=0; i < 4; i++) {
Point direction_translated = translateOnAxisInDirection(_curr_pos, i, 1);
//Converts the point to a reachable square - unnecessary to describe here. There is a domain specific problem which necessitates this function to be used.
Point heading_direction = pointToReachableSquare(direction_translated , i);
//Distance from end of path to "headinglocation", the goal
Point vec_to_end = subVec(heading_direction, headingLocation);
distances[i] = min(absv(vec_to_end.x), absv(vec_to_end.y));
statuses[i] = history[heading_direction.x][heading_direction.y].status;
//If path is unreachable because of wall or something, then mark it as unreachable.
if (cmpVec(heading_direction, _curr_pos) || history[heading_direction.x][heading_direction.y].classification == WALL || !pointInIndBounds(direction_translated)) {
availablities[i] = POINT_UNREACHABLE;
}
}
//Insertion sort the distances.
for (int i = 1; i < 4; i++) {
int j = i - 1;
while (
comparePathOptions(
statuses[i],
distances[i],
lengths[i],
statuses[j],
distances[j],
lengths[j]
) == LESS_THAN && (j >= 0)) {
int temp = directions[i];
directions[i] = directions[j];
directions[j] = temp;
j--;
}
}
//Return the first reachable direction.
int ind = 0;
int dir = directions[ind];
while (availablities[ directions[ind] ] == POINT_UNREACHABLE && (ind<4)) {
dir = directions[ind+1];
ind++;
}
return dir;
}
比较函数:
int relationship(int a, int b){
if (a < b) return LESS_THAN;
if (a > b) return MORE_THAN;
return EQUAL;
}
//Edit this function
//TODO: Edit comparePathOptions.
//NOTE: Something
int comparePathOptions(int n_explored, int n_closeness, int n_length,
int b_explored, int b_closeness, int b_length){
int objs[][3] = {
{n_explored, n_closeness, n_length},
{b_explored, b_closeness, b_length}
};
for (int i = 0; i < 3; i++){
int rel = relationship(objs[1][i],objs[0][i]);
if (rel!= EQUAL ) return rel;
}
return EQUAL;
}
多亏了@Kittsil,我的算法才起作用!您不是通过 j
和 i
访问 statuses
、lengths
和 distances
,而是通过 directions[i or j]
来完成,因为当 directions 数组改变时,i
和 j
停止引用当前方向。
编辑后的代码:
while ( (j >= 0) &&
comparePathOptions(
statuses[ directions[i] ],
distances[ directions[i] ],
lengths[ directions[i] ],
statuses[ directions[j] ],
distances[ directions[j] ],
lengths[ directions[j] ]
) == MORE_THAN ) {
int temp = directions[i];
directions[i] = directions[j];
directions[j] = temp;
j--;
}
以及解决的迷宫:
x: 0, y: 0
H: 5, W:5, Ss:1
4|#####|
3|#####|
2|#####|
1|#####|
0|*::::|
01234
4|#####|
3|#####|
2|#####|
1|#####|
0| *:::|
01234
4|#####|
3|#####|
2|#####|
1|#####|
0| *::|
01234
4|#####|
3|#####|
2|#####|
1|#####|
0| *:|
01234
4|#####|
3|#####|
2|####:|
1|####:|
0| *|
01234
4|#####|
3|#####|
2|####:|
1|####*|
0| |
01234
4|#####|
3|#####|
2|::::*|
1|#### |
0| |
01234
4|#####|
3|#####|
2|:::* |
1|#### |
0| |
01234
4|#####|
3|#####|
2|::* |
1|#### |
0| |
01234
4|#####|
3|#####|
2|:* |
1|#### |
0| |
01234
4|:####|
3|:####|
2|* |
1|#### |
0| |
01234
4|:####|
3|*####|
2| |
1|#### |
0| |
01234
4|*####|
3| ####|
2| |
1|#### |
0| |
01234
最佳答案
您正在对 directions
进行排序数组,但您无法对其他数组进行排序;一旦你进行第一次交换,statuses
, distances
, 和 lengths
不再与 directions
相关.
说明:问题出在您对比较函数的调用中。在这段代码中:
while (
comparePathOptions(statuses[i],
distances[i],
lengths[i],
statuses[j],
distances[j],
lengths[j])
== LESS_THAN &&
(j >= 0)
) {
int temp = directions[i];
directions[i] = directions[j];
directions[j] = temp;
j--;
}
您正在使用 i
和 j
访问包含排序信息的数组。一旦i
和 j
与 directions[i]
不同和 directions[j]
,这不会像您预期的那样运行。你有两个选择:一,将您的电话改为 comparePathOptions(.)
到
comparePathOptions(statuses[directions[i]],
distances[directions[i]],
lengths[directions[i]],
statuses[directions[j]],
distances[directions[j]],
lengths[directions[j]])
OR,按照惯例,将您关心的信息存储在(非常小的)对象中并对这些对象的向量进行排序。
此外,当 j=-1
时,您在循环中越界了。和你比较。你应该移动 (j>=0)
在 AND 的左侧。
说明:在几乎所有语言中,&& 和 ||是short-circuiting .如果 &&
的左侧是false
(或者 ||
的左侧是 true
),语言甚至不会评估右侧;它已经知道 bool 函数的结果。在您的实例中,您不需要 comparePathOptions(.)
在 j<0
时进行评估,因为这会让你出界。因此,你应该比较j
至 0
在你使用j
之前作为索引。
关于algorithm - 自主解迷宫机器人与故障路径排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30174963/
我的对话 fragment 有问题。我想使用 android:onClick 属性,因为在我看来代码更清晰。 在我的布局中,我有以下声明: 现在我的 DialogFragment import an
我正在制作一个使用谷歌地图的应用程序,我的主要 xml 代码 fragment 如下: 我的java代码是: import android.os.Bundle; import com.googl
是否可以在一个应用程序中有两个单独的首选项?我有一个在运行时连接到外部可执行文件(引擎)的应用程序。应用程序有自己的偏好,引擎也有。这两者不能混用,因为下次用户可能会决定加载具有不同偏好的完全不同的引
这是“Google Catalogs”应用程序的快照。我只是想知道我们如何进行这种图像重叠。![enter image description here][1] 最佳答案 您可以使用 FrameLay
我制作了一个 GridView ,其中添加了图像。但我的问题是它显示的 gridview 尺寸非常小,其中只有 3 张图像,就像图片中那样,我想在其中显示 9 张图像。 小号 但我希望 gridvie
我目前正在学习如何在 Android 上创建和使用服务。我查看了 android SDK 以获得进一步的帮助,我发现了 android:enable=[true |假]。 在SDK中是这样说的: Th
在完成我的 Android 游戏时,我希望用户将他/她的分数与高分进行比较。为此,我将当前的最高分存储在 SQLite 数据库中。但我认为我的方法(似乎可行)笨拙且丑陋: //in the final
出于某种原因,谷歌没有为模拟器提供任何通知声音,我找不到任何关于如何加载通知声音的引用。 我遇到过这个线程:How to play ringtone/alarm sound in Android但是除
我的应用有以下样式: true #ffffff true 它在我使用的大多数手机上运行良好。 (LG G5、Nexus 5、Moto G)但是当我在 LeEco Pro
我想确保我的网站阻止 Selenium 和 QTP 等自动化工具。有没有办法做到这一点 ?网站上的哪些设置会导致 Selenium 失败? 最佳答案 适当考虑对原始问题“你到底为什么要这样做?”的评论
我正在处理我的联系表,我希望它尽可能地防止垃圾邮件。我正在使用一些方法作为反击: 使用 JavaScript 正则表达式验证电子邮件的合法性 验证所有字段是否具有足够的值 制作一个不可见的字段来吸引机
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, over
我正在制作一个 android 应用程序,我需要在我的应用程序中显示远程图像我使用以下代码。但图像未显示: for(int i=0;i
我有几个与 Android 操作系统中的 AIML 集成相关的问题。我对 Java 了解不多,但我对 AIML 了解很多。我制作了 AIML 文件,现在我想做的是制作一个可以加载文件并在 GUI 中运
我有一个具有“登录”和“注销”操作的应用程序。用户登录后,在他们注销时堆栈中可能还剩下任意数量的 Activity 。 当用户注销时,我希望重置所有应用程序状态。我清除了我的磁盘缓存和首选项,但在运行
我是 Android 应用程序开发的新手,我正在尝试开发一个可用的应用程序。但是我创建的这个页面自创建以来就出现了问题,我真的希望有人能帮助我解决这个问题。每次我运行这个程序时,应用程序都会关闭。 这
我尝试将 android:imeOptions 设置为 actionSend、actionSearch。但是键盘上没有“发送”或“搜索”按钮,只有普通的“Enter”键。我还尝试设置不同的输入类型。
我想创建一种方法来查看相机坐标的中心是否在某个区域的边界内,如果是,则执行一些操作。 当我尝试这样做时,出现错误:Operator !不能应用于 LatLngBounds。 我也试过 if(temp
我正在尝试将视频录制时长限制为 5 秒。我正在使用默认相机和此代码: intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 5); 现在它在三星 S6 上工
我正在开发一款安卓游戏 https://code.google.com/p/something-soft/我的日志猫说它正试图激发游戏的 Intent ,但随后主线程似乎死了(出现 ActivityN
我是一名优秀的程序员,十分优秀!