递归地解决RushHour,在递归函数中获得段错误

时间:2013-10-09 10:06:22

标签: function recursion segmentation-fault

我试图将这个代码用于类项目,但无法弄清楚递归求解这个算法的算法。网上有很多关于使用广度优先搜索来解决高峰时段的信息,但不幸的是,这不是递归的,这个项目的重点是递归地进行。所以,如果有人可以提供帮助,我们将不胜感激。

这是作业。

  

有一个商业上令人困惑的叫做“尖峰时刻”,你的目标是打破复杂的交通堵塞。拼图是在6x6网格上进行的。车辆(汽车和卡车)在整个位置分散在网格上,如下所示。两种类型的车辆都是1平方宽。汽车长2平方,卡车长3平方。车辆可以相对于网格水平或垂直定向。

     

输入是这样的,通过键盘输入。

1
2 H 2 1
9
2 H 1 3
2 H 0 0
2 H 1 0
2 H 2 0
2 H 3 0
2 H 4 0
2 H 4 0
2 V 2 2
3 V 0 5
9
2 H 2 3
2 H 0 0
2 H 1 0
2 V 3 3
2 H 3 4
2 H 4 0
2 H 5 2
2 V 2 2
3 V 0 5
9
2 H 2 3
2 H 0 0
2 H 1 0
2 V 3 3
2 H 3 4
2 V 4 1
2 H 5 2
2 V 2 2
3 V 0 5
0
  

这是多个场景,每个场景都以一个整数开头,表示该场景中有多少辆车。下一个喜欢的第一个数字是那辆车的长度。字母是如何定向的(水平为H,垂直为V)。下一个数字是它所在的行,下一个数字是它所在的列。

     

目标是将每个场景中的第一辆车送到董事会的最右边。

     

我们要输出所需的移动次数(移动是一辆车移动1个空间)来解决这个难题(如果有多个解决方案,那么必须输出移动最少的一个。我们不必输出移动本身,只是它们的数量。

这是我的代码......

#include <iostream>
using namespace std; 

int maxMoves = 10;  

class car 
    {
    public:
    void setLength(int); 
    void setRow(int);
    void setCol(int);
    void setOri(char);  
    int getLength()
        {
        return length;
        }
    int getRow()
        {
        return row;
        }
    int getCol()
        {
        return col;
        }
    char getOri()
        {
        return ori; 
        }

    private:
    int length; 
    int row; 
    int col; 
    char ori; 
    };

    void printBoard ( char arr[][6]);
    int readInCars ( char arr[][6], car carArr[]); 
    void solve (  char arr[][6], car carArr[], int numCars, car &carGiven, int numCounter, int index);
    bool isSolved ( car myCar );
    bool moveRight ( car &carGiven, char arr[][6] );
    bool moveLeft ( car &carGiven, char arr[][6] );
    bool moveDown ( car &carGiven, char arr[][6] );
    bool moveUp ( car &carGiven, char arr[][6] );



   int main ()
    {
    char arr[6][6];  
    int moves;
    int bound = 20; 
    int numberOfCars; 
    car carArr[50]; 
    car searchQueue; 

    numberOfCars = readInCars ( arr, carArr ); 
    printBoard ( arr ); 



    cout << "Number of Cars - " << numberOfCars << endl; 
    solve ( arr, carArr, numberOfCars, carArr[0], 0, 0  ); 

    cout << "maxMoves- " << maxMoves << endl; 
    return 0; 
    }

    int readInCars( char arr[][6], car carArr[])
    {
    int  length, row, col, numberOfCars; 
    char vehicleOrientation;


    // Fills entire array with -'s 
    for ( int rowIndex = 0; rowIndex < 6; rowIndex++ ) 
        {
        for ( int colIndex = 0; colIndex < 8; colIndex++ ) 
            {

            arr[rowIndex][colIndex] = '-';
            }
        }


    cin >> numberOfCars; 
    // Fills array with cars 
    cin >> length >> vehicleOrientation >> row >> col; 
    arr[row][col] = 'm'; 
    arr[row][col+1] = 'm';

    car temp; 

    temp.setRow(row);
    temp.setCol(col);
    temp.setLength(length);
    temp.setOri(vehicleOrientation); 

    carArr[0] = temp; 


for ( int i = 1; i < numberOfCars; i++ ) 
        {

        cin >> length >> vehicleOrientation >> row >> col;
        car temp; 

        temp.setRow(row);
        temp.setCol(col);
        temp.setLength(length);
        temp.setOri(vehicleOrientation); 

        carArr[i] = temp; 

            for (int j = 0; j < length; j++ )
                {
                if ( vehicleOrientation == 'H' )
                    {
                    arr[row][col+j] = 'c'; 
                    }
                else if ( vehicleOrientation == 'V' ) 
                    {
                    arr[row+j][col] = 'c'; 
                    }
                }


            }
        return numberOfCars; 

        }

        void printBoard ( char arr [][6] ) 
    {
cout << endl; 
    for ( int rowIndex = 0; rowIndex < 6; rowIndex++ ) 
        {
        for ( int colIndex = 0; colIndex < 6; colIndex++ ) 
            {
            cout << arr[rowIndex][colIndex];
            }
        cout << endl; 
        }

    }

    void solve ( char arr[][6], car carArr[], int numCars, car &carGiven, int counter, int index) 
    {
    bool worked; 
    if ( counter == maxMoves )
        {
        return; 
        }
    else if ( isSolved( carArr[0] ) ) 
        {
        cout << "Max Moves set to " << counter << endl; 
        maxMoves = counter; 
        return;
        }

    for ( int i = index; i < numCars; i++ ) 
        {
        if ( carGiven.getOri() == 'H' ) 
            {
            worked = moveRight ( carGiven, arr ); 
            if ( worked == true ) 
                {
                counter++; 
                for ( int j = 0; j < numCars; j++ )
                    {
                    solve ( arr, carArr, numCars, carArr[j], counter, i );     
                        moveLeft(carGiven, arr); 
                    counter--; 
                cout << counter << endl; 
                    }

                }
            worked = moveLeft ( carGiven, arr ); 
            if ( worked == true ) 
                {
                counter++; 
                for ( int j=i; j < numCars; j++ )
                    {
                    solve ( arr, carArr, numCars, carArr[j], counter, i );     
                        moveRight(carGiven, arr); 
                    counter--; 
                    cout << counter << endl; 
                    }
                    }

            }
        else
            {
            worked = moveDown ( carGiven, arr ); 
            if ( worked == true ) 
                {   
                counter++; 
                for ( int j = 0; j < numCars; j++ )
                    {
                    solve ( arr, carArr, numCars, carArr[j], counter, i );     
                    moveUp(carGiven, arr); 
                counter--; 
                    }

                }
            worked = moveUp ( carGiven, arr ); 
            if ( worked == true ) 
                {
                counter++; 
                for ( int j=0; j < numCars; j++ )
                    {
                    solve ( arr, carArr, numCars, carArr[j], counter, i );     
                    moveDown(carGiven, arr); 
                    counter--; 
                    }
                }

            }


        }


    }


   void car::setLength(int given)
    {   
    length = given; 
    }
    void car::setRow(int given)
    {
    row = given; 
    }
    void car::setCol(int given)
    {
    col = given;
    }
    void car::setOri(char given)
    {
    ori = given; 
    } 

    bool isSolved ( car myCar ) 
    {
    int col = myCar.getCol();
    if ( col+1 == 5 ) 
        {
        return true; 
        }
    return false; 

    }

    bool moveRight ( car &carGiven, char arr[][6] )
    {
    int length = carGiven.getLength(); 
    int row = carGiven.getRow();
    int col = carGiven.getCol(); 

    if ( length == 2 ) 
        {
        if ( col+2 != 6 and arr[row][col+2] == '-' ) 
            {
            arr[row][col+2] = arr[row][col+1]; 
            arr[row][col] = '-';
            carGiven.setCol(col+1);  
            return true; 
            }
        }
    else
        {
        if ( col + 3 != 6 and arr[row][col+3] == '-' ) 
            {
            arr[row][col+3] = arr[row][col+2];
            arr[row][col] = '-';
            carGiven.setCol(col+1);
            true; 
            }
        }
    return false; 

    }

    bool moveLeft ( car &carGiven, char arr[][6] )
    {
    int length = carGiven.getLength(); 
    int row = carGiven.getRow();
    int col = carGiven.getCol(); 

    if ( length == 2 ) 
        {
        if ( col-1 >= 0 and arr[row][col-1] == '-' ) 
            {
            arr[row][col-1] = arr[row][col]; 
            arr[row][col+1] = '-'; 
            carGiven.setCol(col-1); 
            return true; 
            }
        }
    else
        {
        if ( col - 1 >= 0 and arr[row][col-1] == '-' ) 
            {
            arr[row][col-1] = arr[row][col];
            arr[row][col+2] = '-';
            carGiven.setCol(col-1); 
            return true; 
            }
        }
    return false; 

    }

    bool moveDown ( car &carGiven, char arr[][6] )
    {
    int length = carGiven.getLength(); 
    int row = carGiven.getRow();
    int col = carGiven.getCol(); 

    if ( length == 2 ) 
        {
        if ( row+2 != 6 and arr[row+2][col] == '-' ) 
            {
            arr[row+2][col] = arr[row+2][col]; 
            arr[row][col] = '-'; 
            carGiven.setRow(row+1); 
            return true; 
            }
        }
    else
        {
        if ( row + 3 != 6 and arr[row+3][col] == '-' ) 
            {
            arr[row+3][col] = arr[row+2][col];
            arr[row][col] = '-';
            carGiven.setRow(row+1); 
            return true; 
            }
        }
    return false; 

    }


    bool moveUp ( car &carGiven, char arr[][6] )
    {
    int length = carGiven.getLength(); 
    int row = carGiven.getRow();
    int col = carGiven.getCol(); 

    if ( length == 2 ) 
        {
        if ( row-1 >= 0 and arr[row-1][col] == '-' ) 
            {
            arr[row-1][col] = arr[row][col]; 
            arr[row+1][col-1] = '-'; 
            carGiven.setRow(row-1); 
            return true; 
            }
        }
    else
        {
        if ( row - 1 >= 0 and arr[row-1][col] == '-' ) 
            {
            arr[row-1][col] = arr[row][col];
            arr[row+2][col] = '-';
            carGiven.setRow(row-1); 
            return true; 
            }
        }
    return false; 

    }

我的移动功能按预期工作,因为我已经彻底测试过它们。我正在尝试进行简单的测试...

  

2

     

2 H 2 1

     

3 V 1 3

     

0

首先,但是我在solve函数中遇到了段错误,但我无法弄清楚如何修复它。谢谢你的帮助,你们真棒。

0 个答案:

没有答案