递归寻径问题
本文关键字:问题 归寻 递归 | 更新日期: 2023-09-27 18:09:08
我正在编写一个寻径算法,我需要一些帮助来弄清楚如何通过避免递归来加速它,而创建了一个异常情况。
我有1个矩阵(spaces = walls;Hash = blocks;2 =实际位置)"2"需要收集所有的"#",每次他走到"#"上,它就消失了。
我自愿生成了一个不可能的来解释我的问题。
{ , , , , , , , };
{ , #, #, #, #, #, #, };
{ , #, , , , , #, };
{ , #, , #, #, , #, };
{ , #, , #, #, , #, };
{ , #, , #, #, , #, };
{ , #, , #, #, , #, };
{ , #, , , , , #, };
{ , #, #, #, 2, #, #, };
{ , , , , , , , };
正如你所看到的,在地图的中央有一个无法到达的岛屿。
我想知道你们是否知道如何检测这种情况。我想不出任何办法来……
下面是我的实际代码:
检查一些异常情况并返回true或false:
static bool BreakCaseFound() {
int EndCases = 0; // 3 blocs with 3 empty slots around
bool BreakCases = false; // 1 bloc with 4 empty slots around
int temp = 0;
for(int i = 1; i<17; i++) {
for(int j = 1; j<17; j++) {
if(matrice[j, i] == bloc) {
if (matrice[j+1, i] == empty) {
temp++;
}
if (matrice[j-1, i] == empty) {
temp++;
}
if (matrice[j, i+1] == empty) {
temp++;
}
if (matrice[j, i-1] == empty) {
temp++;
}
}
switch(temp) {
case 3:
EndCases++;
temp = 0;
break;
case 4:
temp = 0;
BreakCases = true;
break;
default:
temp = 0;
break;
}
if(BreakCases || EndCases >= 3) {
return true;
}
}
}
return false;
}
My Show function (MS-DOS windows)
static void show() {
Console.Clear();
for(int i =0; i<18; i++) {
for(int j = 0; j<18; j++) {
if(matrice[j,i] == empty) {
Console.Write(" ");
}
else {
if (matrice[j, i] == 2) { matrice[j, i] = bloc ; }
if (matrice[j, i] == 3) { matrice[j, i] = 5 ; }
Console.Write(matrice[j, i]);
}
}
Console.Write("'n");
}
}
My algorithm:
static dynamic move(int actualPosCol, int actualPosLigne, List<int[]> path, List<int[]> RealPath)
{
matrice[path[path.Count()-1][0], path[path.Count()-1][1]] = 5;
show();
if(nbBlocs > 0) {
show();
//Left move
if( (matrice[path[path.Count() - 1][0]-1, path[path.Count() - 1][1]] == bloc)
&& (!BreakCaseFound()) ) {
nbBlocs--;
matrice[actualPosCol, actualPosLigne] = empty;
int[] posNext = new int[2] {actualPosCol-1, actualPosLigne};
path.Add(posNext);
move(actualPosCol-1, actualPosLigne, path, RealPath);
}
//Right move
if( (matrice[path[path.Count() - 1][0]+1, path[path.Count() - 1][1]] == bloc)
&& (!BreakCaseFound()) ) {
nbBlocs--;
matrice[actualPosCol, actualPosLigne] = empty;
int[] posNext = new int[2] {actualPosCol+1, actualPosLigne};
path.Add(posNext);
move(actualPosCol+1, actualPosLigne, path, RealPath);
}
//Down move
if ( (matrice[path[path.Count() - 1][0], path[path.Count() - 1][1]+1] == bloc)
&& (!BreakCaseFound()) ) {
nbBlocs--;
matrice[actualPosCol, actualPosLigne] = empty;
int[] posNext = new int[2] {actualPosCol, actualPosLigne+1};
path.Add(posNext);
move(actualPosCol, actualPosLigne+1, path, RealPath);
}
//Up move
if ( (matrice[path[path.Count() - 1][0], path[path.Count() - 1][1]-1] == bloc)
&& (!BreakCaseFound()) ) {
nbBlocs--;
matrice[actualPosCol, actualPosLigne] = empty;
int[] posNext = new int[2] {actualPosCol, actualPosLigne-1};
path.Add(posNext);
move(actualPosCol, actualPosLigne-1, path, RealPath);
}
if(nbBlocs > 0) {
//Can't move right, left, up or down
matrice[path[path.Count() - 1][0], path[path.Count() - 1][1]] = 3;
show();
path.Remove(path.Last()); //remove last move from the List
nbBlocs++;
}
return path;
}
else { //No more blocs, path found.
foreach(int[] way in path) {
if(!RealPath.Contains(way)) {
RealPath.Add(way);
}
}
return path;
}
}
也许我疯了,但每次我看到一个涉及不连接的连续区域的问题时,我都会想到不连接的集合。不相交集是为高效合并而设计的集合,如果你想找出#的许多区域是否连通,你经常会用到合并。
把你地图上的每个位置放到它自己的不相交的集合中。任何包含位置的集合最终都将包含您可以从中移动到的所有位置。如何?我们在地图上走动,任何时候我们可以从一个地方移动到另一个地方,我们合并集合。
你应该以什么顺序采取这些步骤?从某处填满你的整个地图——从每个位置填满你不是来自的邻居。如果X和Y在同一个集合中,那么从X点到Y点的洪水填充应该什么都不做,否则,如果可以在X和Y之间移动,则应该合并X和Y的集合。如果以前从未访问过Y,则从Y递归。
对于地图上的n个位置,此解决方案的运行时间约为O(n逆ackermann(n))。