简体   繁体   English

用右手原理解决迷宫

[英]solve maze by right hand principle

hello there 你好
i am solving a text book exercise to solve a maze by right hand principle 我正在解决一个教科书练习,以右手原则解决迷宫

i use switch case to deal it 我用开关盒来处理

switch ( face )
{
    case face_EAST:
    {
        if( map[x][y + 1] == '.' && map[x + 1][y] == '#' )
        {
            y = y + 1;
            gotoxy(x, y);
            face = face_EAST;
        }
        else if ( map[x - 1][y] == '.' && map[x][y + 1] == '#' )
        {
            x = x - 1;
            gotoxy( x, y );
            face = face_NORTH;
        }
        else if ( map[x + 1][y] == '.' && map[x + 1][y - 1] == '#' )
        {
            x = x + 1;
            gotoxy( x, y );
            face = face_SOUTH;
        }
        else
            face = face_SOUTH;

        break;
    }
    case face_SOUTH :
    {
        if( map[x + 1][y] == '.' && map[x][y - 1] == '#' )
        {
            x = x + 1;
            gotoxy( x, y );
            face = face_SOUTH;
        }
        else if( map[x][y - 1] == '.' && map[x - 1][y - 1] == '#' )
        {
            y = y - 1;
            gotoxy( x, y );
            face = face_WEST;
        }
        else if( map[x][y + 1] == '.' && map[x + 1][y] == '#' )
        {
            y = y + 1;
            gotoxy( x, y );
            face = face_EAST;
        }
        else
            face = face_WEST;
        break;
    }
    case face_WEST:
    {
        if( map[x][y - 1] == '.' && map[x - 1][y] == '#' )
        {
            y = y - 1;
            gotoxy( x, y );
            face = face_WEST;
        }
        else if( map[x - 1][y] == '.' && map[x - 1][y + 1] == '#' )
        {
            x = x - 1;
            gotoxy( x, y );
            face = face_NORTH;
        }
        else if( map[x + 1][y] == '.' && map[x][y - 1] == '#' )
        {
            x = x + 1;
            gotoxy( x, y );
            face = face_SOUTH;
        }
        else if( map[x][y + 1] == '.' && map[x + 1][y] == '#' )
        {
            y = y + 1;
            gotoxy( x, y );
            face = face_EAST;
        }
        else if( map[x - 1][y] == '.' && map[x - 1][y + 1] == '#' )
        {
            x = x - 1;
            gotoxy( x, y );
            face = face_NORTH;
        }
        else
            face = face_NORTH;

        break;
    }
    case face_NORTH:
    {
        if( map[x - 1][y] == '.' && map[x][y + 1] == '#' )
        {
            x = x - 1;
            gotoxy( x, y );
            face = face_NORTH;
        }
        else if( map[x][y - 1] == '.' && map[x - 1][y] == '#' )
        {
            y = y - 1;
            gotoxy( x, y );
            face = face_WEST;
        }
        else if( map[x][y + 1] == '.' && map[x + 1][y + 1] == '#' )
        {
            y = y + 1;
            gotoxy( x, y );
            face = face_EAST;
        }
        else
            face = face_EAST;

        break;
    }
}

i want to know if there any more easy way to make right hand principle work? 我想知道是否还有更简单的方法可以使右手原理起作用?
i have though to change the coordinate system, but can't implement it 我必须更改坐标系,但无法实现

As far as "any more easy way", this problem is a prime candidate for recursion. 就“更简单的方法”而言,此问题是递归的主要候选对象。 Folks often find recursion a little hard to grasp, at first, but it ends up being considerably easy to follow than what you have. 刚开始时,人们通常觉得递归有点难,但是最终变得比您所拥有的要容易得多。 There's a bunch of links in the "related" section to this question which may be helpful, and I wrote a program to do it a while ago, the look() function right at the bottom is the one that does the solving. 这个问题的“相关”部分中有很多链接可能会有所帮助,而我前一段时间编写了一个程序来执行此操作 ,最下面的look()函数就是解决问题的程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM