簡體   English   中英

指針二進制樹迷宮解算器在C

[英]Pointers Binary Tree Maze Solver in C

我需要創建一個用C語言編寫的機器人模擬器。機器人必須使用遞歸回溯算法找到2d labirinth的出口,我知道這個算法是如何工作的,但我不知道如何實現它。 我想我可以使用指針使用二叉樹,但我不知道如何做到這一點,你能嘗試向我解釋一下嗎?

這是我創建的程序,現在機器人正在進入一個循環,因為改變方向的方法

#ifdef __unix__
#include <unistd.h>
#elif defined _WIN32
#include <windows.h>
#define sleep(x) Sleep(1000 * x)
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

void goUp();
void goDown();
void goLeft();
void goRight();

typedef struct robot {
    int direction;
    bool is_moving;
}robot;

typedef struct room {
    robot robot;
    bool is_robot;
    int obstacle;

}room;

room Room[20][20];
int r = 12;
int c = 10;

void generation(room matrix[20][20])
{
    srand(time(NULL));
    int x,i,j;
    x=0;
    for(i=0;i<20;i++)
    {
        for(j=0;j<20;j++)
        {
            matrix[i][j].is_robot=false;
            x=rand()%100+1;
            if(x==1||x==50||x==100)
            {
                matrix[i][j].obstacle=1;
            }
            else
            {
                matrix[i][j].obstacle=0;
            }
        }
    }
}

void print_matrix(room matrix[20][20])
{
    int i,j;

    for(i=0;i<20;i++)
    {
        for(j=0;j<20;j++)
        {
            if(matrix[i][j].obstacle==0)
            {
                if(matrix[i][j].is_robot==true)
                {
                    printf("I");
                }
                else
                {
                    printf(" ");
                }
            }
            else
            {
                if(matrix[i][j].is_robot==true)
                {
                    printf("I");
                }
                else
                {
                    printf("o");
                }
            }
        }
        printf("\n");
    }
}

bool changeDirection(room Room[20][20],int i,int j)
{
    if(Room[i][j].robot.direction == 1)
    {
        if(Room[i-1][j].obstacle == 1 || i-1 ==  0)
        {
            if(Room[i+1][j].obstacle == 1 || i+1 == 19)
            {
                Room[i][j].robot.direction = 2;
                return true;
            }
            else
            {
                Room[i][j].robot.direction = 4;
                return true;
            }

        }
        else
        {
            Room[i][j].robot.direction = 3;
            return true;
        }
    }



    if(Room[i][j].robot.direction == 2)
    {
        if(Room[i-1][j].obstacle == 1 || i-1 ==  0)
        {
            if(Room[i+1][j].obstacle == 1 || i+1 ==  19)
            {
                Room[i][j].robot.direction = 1;
                return true;
            }
            else
            {
                Room[i][j].robot.direction = 4;
                return true;
            }

        }
        else
        {
            Room[i][j].robot.direction = 3;
            return true;
        }
    }


    if(Room[i][j].robot.direction == 3)
    {
        if(Room[i][j+1].obstacle == 1 || j+1 ==  19)
        {
            if(Room[i][j-1].obstacle == 1 || j-1 ==  0)
            {
                Room[i][j].robot.direction = 4;
                return true;
            }
            else
            {
                Room[i][j].robot.direction = 2;
                return true;
            }

        }
        else
        {
            Room[i][j].robot.direction = 1;
            return true;
        }
    }

    if(Room[i][j].robot.direction == 4)
    {
        if(Room[i][j+1].obstacle == 1 || j+1 ==  19)
        {
            if(Room[i][j-1].obstacle == 1 || j-1 ==  0)
            {
                Room[i][j].robot.direction = 3;
                return true;
            }
            else
            {
                Room[i][j].robot.direction = 2;
                return true;
            }

        }
        else
        {
            Room[i][j].robot.direction = 1;
            return true;
        }
    }
}

void goRight()
{
    c=c+1;
    Room[r][c].robot.direction=1;
    Room[r][c].is_robot=true;
    Room[r][c-1].is_robot=false;
}

void goLeft()
{
    c=c-1;
    Room[r][c].robot.direction=2;
    Room[r][c].is_robot=true;
    Room[r][c+1].is_robot=false;
}

void goUp()
{
    r=r-1;
    Room[r][c].robot.direction=3;
    Room[r][c].is_robot=true;
    Room[r+1][c].is_robot=false;
}
void goDown()
{
    r=r+1;
    Room[r][c].robot.direction=4;
    Room[r][c].is_robot=true;
    Room[r-1][c].is_robot=false;
}

int main()
{

    generation(Room);
    Room[r][c].robot.direction = 1;
    Room[r][c].robot.is_moving = true;
    Room[r][c].is_robot = true;

    do
    {
        Room[r][c].robot.is_moving = true;
        if (Room[r][c].robot.direction == 1 && Room[r][c].robot.is_moving == true) // destra
        {
            if(Room[r][c    +1].obstacle == 1 || c+1 == 19)
            {

                changeDirection(Room,r,c);
            }
            else
            {
                goRight();
            }
        }

        if (Room[r][c].robot.direction == 2 && Room[r][c].robot.is_moving == true) // sinistra
        {
            if(Room[r][c   -1].obstacle == 1 || c-1 == 0)
            {
                changeDirection(Room,r,c);
            }
            else
            {
                goLeft();
            }
        }

        if (Room[r][c].robot.direction == 3 && Room[r][c].robot.is_moving == true) // su
        {
            if(Room[r-1][c].obstacle == 1 || r-1 == 0)
            {
                changeDirection(Room,r,c);
            }
            else
            {
                goUp();
            }
        }

        if (Room[r][c].robot.direction == 4 && Room[r][c].robot.is_moving == true) // giu
        {
            if(Room[r+1][c].obstacle == 1 || r+1 == 19)
            {
                changeDirection(Room,r,c);
            }
            else
            {
                goDown();
            }
        }

        print_matrix(Room);
        sleep(0.1);
        system("cls");

    }
    while(1);
    print_matrix(Room);
}

我很難理解二叉樹如何在迷宮中找到一條路徑(也許它用來代表迷宮?)但也許我是盲目的。 我只是制作一個2d int數組,讓0意味着位置被阻擋(那里有一堵牆或其他東西)和1意味着它是開放的(你可以移動那里)。 蠻力回溯程序,正常運動(左,右,上,下)將是:

f(x,y){
    // you found the place your want to go to
    if (x,y) is (destinationX,destinationY) 
        return true

    block the position (x,y) // i.e. mark current position as visited

    if there is an open spot at (x,y-1) AND f(x,y-1)
        return true
    if there is an open spot at (x,y+1) AND f(x,y+1)
        return true
    if there is an open spot at (x-1,y) AND f(x-1,y)
        return true
    if there is an open spot at (x+1,y) AND f(x+1,y)
        return true

    return false
}

假設你的迷宮看起來像:

"+" is where you start ([1][1])
"-" is your destination ([3][1])
"#" is a blocked region

===========
|#|#|#|#|#|
|#|+| |#|#|
|#|#| |#|#|
|#|-| | |#|
|#|#|#|#|#|
===========

使用上述想法我有:

#include <stdio.h>
#define width 5
#define height 5

// print maze
void print(char arr[][width]){
    for (int i = 0; i < 2*width+1; i++) printf("=");
    printf("\n");
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            printf("|%c",arr[i][j]);
        }
        printf("|\n");
    }
    for (int i = 0; i < 2*width+1; i++) printf("=");
}


// starting from (x,y) to (destX,destY)
int path(int arr[][width],int x,int y,int destX,int destY,char toDest[][width]){
    if (x==destX && y==destY) {
        toDest[y][x] = '*';
        print(toDest);
        return 1;
    }
    // mark current position as visited
    arr[y][x] = 0;
    toDest[y][x] = '*';

    // left
    if (arr[y][x-1] && path(arr,x-1,y,destX,destY,toDest))
        return 1;

    // right
    if (arr[y][x+1] && path(arr,x+1,y,destX,destY,toDest))
        return 1;

    // up
    if (arr[y-1][x] && path(arr,x,y-1,destX,destY,toDest))
        return 1;

    // down
    if (arr[y+1][x] && path(arr,x,y+1,destX,destY,toDest))
        return 1;

    return 0;
}


int main () {
    // use this to store path
    // and then print it out if found
    char toDest[height][width] = {
            {'#','#','#','#','#'},
            {'#',' ',' ','#','#'},
            {'#','#',' ','#','#'},
            {'#',' ',' ',' ','#'},
            {'#','#','#','#','#'}
    };

    // 0 -> position is blocked
    // 1 -> position is open
    int maze[height][width] = {
            {0,0,0,0,0},
            {0,1,1,0,0},
            {0,0,1,0,0},
            {0,1,1,1,0},
            {0,0,0,0,0}
    };

    path(maze,1,1,1,3,toDest);
}

輸出:

===========
|#|#|#|#|#|
|#|*|*|#|#|
|#|#|*|#|#|
|#|*|*| |#|
|#|#|#|#|#|
===========

在輸出中,路徑由* s指定

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM