Java 自动迷宫解算

Java 自动迷宫解算,java,android,arrays,xml,Java,Android,Arrays,Xml,所以我正在创建一个迷宫应用程序(我从迷宫的字符串数组中读取,然后用触摸事件引导一个球穿过它)。到目前为止,该应用程序成功地创建了所有内容,运行良好。但我想包括一个选项来自动解决它 我使用的递归算法如下: 基本上,我得到了一个布尔多维数组中的路径(有迷宫大小) 尝试实现类似MWC的设计,因此我有以下课程: 迷宫视图-处理与绘制迷宫和绘制球相关的所有内容 迷宫模型-初始化迷宫,处理球的运动,我在这里检查迷宫的结束,并在这里实现递归求解 迷宫式活动-这是我将所有东西聚集在一起的地方 就像我说的,到目前

所以我正在创建一个迷宫应用程序(我从迷宫的字符串数组中读取,然后用触摸事件引导一个球穿过它)。到目前为止,该应用程序成功地创建了所有内容,运行良好。但我想包括一个选项来自动解决它

我使用的递归算法如下:

基本上,我得到了一个布尔多维数组中的路径(有迷宫大小)

尝试实现类似MWC的设计,因此我有以下课程:

迷宫视图-处理与绘制迷宫和绘制球相关的所有内容 迷宫模型-初始化迷宫,处理球的运动,我在这里检查迷宫的结束,并在这里实现递归求解 迷宫式活动-这是我将所有东西聚集在一起的地方

就像我说的,到目前为止,手动解决迷宫就像一种魅力。 不知道如何设置自动解决方案的动画。 让我给你举个迷宫的例子:

<string-array name="labyrinthEasy">
    <item>0000000001</item>
    <item>0111110110</item>
    <item>0100000110</item>
    <item>0101111000</item>
    <item>0101000010</item>
    <item>0101011010</item>
    <item>0101011110</item>
    <item>0101000010</item>
    <item>0101111010</item>
    <item>0100000010</item>
    <item>0111111110</item>
    <item>1000000000</item>
</string-array>
基本上,我在人机对话中进行选择。如果选择了human,则处理对话框和nothing(应用程序继续并等待手动解决方案)。如果选择机器,我得到路径,如果可以解决,我想我应该在这里启动一个新线程,在那里我可以设置自动解决方案的动画,否则如果无法解决,我将返回应用程序的主菜单。现在开始我的问题,因为我不知道如何在AsyncTask类中实现它

我使用doInBackgroung方法进行逻辑分析,但无法找出如何遵循正确路径的逻辑。因为如果我逐行遍历数组,球将从一行跳到另一行,并且不会跟随路径的流动性。 此外,我认为应该在每次迭代后使用onProgressUpdate方法重新绘制进度

这就是我的手动逻辑工作原理(移动和绘图):

这就是我到目前为止在助理任务中所做的:

private class MazeSolver extends AsyncTask<Void,Void,Void>{
    @Override
    protected Void doInBackground(Void... params) {
        for ( int row = 0; row < labyrinthModel.correctPath.length; row ++)
            for ( int col = 0; col < labyrinthModel.correctPath[0].length; col++ ){
                if(labyrinthModel.correctPath[row][col + 1]){
                    labyrinthModel.moveRight();
                }
//here to do the implementaion of the path following logic???

            }
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
// here to redraw the progress ????
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
    }
}
私有类MazeSolver扩展异步任务{
@凌驾
受保护的Void doInBackground(Void…参数){
对于(int row=0;row

如果您能为我指出正确的方向,我将不胜感激。

鉴于您可以覆盖二维数组的路径信号,您只需从头到尾跟随根:

travel(correctPath, 0, 0, 11, 8);

void travel(bool a[][], int row, int col, int finalRow, int finalCol) {
   // if we are already there stop
   if(finalRow == row && col == finalCol) return;

   // avoid comming back
   a[row][col]=false;

   // if the input is correct only on of this moves will be valid
   // try each of them and see which move we can make
   if(col - 1 >= 0 && a[row][col-1]) { left(); travel(a, row, col-1, finalRow, finalCol)};
   if(col + 1 < 8 && a[row][col+1]) { right(); travel(a, row,col+1, finalRow, finalCol)};
   if(row - 1 >= 0 && a[row-1][col]) { up(); travel(a, row-1, col, finalRow, finalCol)};
   if(row + 1 < 11 && a[row+1][col]) { down(); travel(a, row+1,col, finalRow, finalCol)};
}
行程(正确路径,0,0,11,8);
无效行程(布尔a[][],整数行,整数列,整数最终行,整数最终行){
//如果我们已经到了,停下来
如果(finalRow==行和列==finalCol)返回;
//避免回击
a[行][列]=假;
//如果输入正确,则此移动中只有一个有效
//试试每一个,看看我们能采取什么行动
如果(列-1>=0&&a[行][col-1]){left();行程(a,行,列-1,finalRow,finalCol)};
if(col+1<8&&a[行][col+1]){right();travel(a,row,col+1,finalRow,finalCol)};
如果(第1行>=0&&a[第1行][col]){up();行程(第1行,col,finalRow,finalCol)};
如果(行+1<11&&a[行+1][col]){down();行程(行+1,col,finalRow,finalCol)};
}
private class MazeSolver extends AsyncTask<Void,Void,Void>{
    @Override
    protected Void doInBackground(Void... params) {
        for ( int row = 0; row < labyrinthModel.correctPath.length; row ++)
            for ( int col = 0; col < labyrinthModel.correctPath[0].length; col++ ){
                if(labyrinthModel.correctPath[row][col + 1]){
                    labyrinthModel.moveRight();
                }
//here to do the implementaion of the path following logic???

            }
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
// here to redraw the progress ????
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
    }
}
travel(correctPath, 0, 0, 11, 8);

void travel(bool a[][], int row, int col, int finalRow, int finalCol) {
   // if we are already there stop
   if(finalRow == row && col == finalCol) return;

   // avoid comming back
   a[row][col]=false;

   // if the input is correct only on of this moves will be valid
   // try each of them and see which move we can make
   if(col - 1 >= 0 && a[row][col-1]) { left(); travel(a, row, col-1, finalRow, finalCol)};
   if(col + 1 < 8 && a[row][col+1]) { right(); travel(a, row,col+1, finalRow, finalCol)};
   if(row - 1 >= 0 && a[row-1][col]) { up(); travel(a, row-1, col, finalRow, finalCol)};
   if(row + 1 < 11 && a[row+1][col]) { down(); travel(a, row+1,col, finalRow, finalCol)};
}