C++迷宮

/*
2016年10月10日19:48:47
C++
迷宮算法
*/

#include <iostream>
#include <stack>
using namespace std;

void InitMaze(char **maze,int x,int y) {
    for (int i = 0; i < y+2; i++)
        maze[i] = new char[x+2];
    for (int i = 0; i < y + 2; i++) {
        for (int j = 0; j < x + 2; j++)
            maze[i][j] = cin.get();
        cin.get();
    }
}
void Display(char **maze,int x,int y) {
    cout << endl << endl;
    for (int i = 0; i < y; i++) {
        for (int j = 0; j < x; j++)
            cout << maze[i][j];
        cout << endl;
    }
}
int *GetStart(char **maze, int x, int y) {//沒有圍牆
    int *s = new int[2];
    for (int i=0;i<y + 2;i++)
        for (int j=0;j<x + 2;j++)
            if (maze[i][j] == 'S') {
                s[0] = i; s[1] = j;
                return s;
            }
    s[0] = y; s[1] = x;
    return s;
}
int *GetEnd(char **maze, int x, int y) {
    int *s = new int[2];
    for (int i = 0; i<y + 2; i++)
        for (int j = 0; j<x + 2; j++)
            if (maze[i][j] == 'E') {
                s[0] = i; s[1] = j;
                return s;
            }
    s[0] = y; s[1] = x;
    return s;
}
bool NextStep(char **maze, int **foot, int di, int cur_x, int cur_y, int x, int y) {
    switch (di) {
    case 1:
        if (cur_x < x - 1) {
            if (!foot[cur_y][cur_x + 1] && maze[cur_y + 1][cur_x + 2] != '*')//right
                return true;
        }
        return false;
    case 2:
        if (cur_y < y - 1) {
            if (!foot[cur_y + 1][cur_x] && maze[cur_y + 2][cur_x + 1] != '*')//down
                return true;
        }
        return false;
    case 3:
        if (cur_x > 0) {
            if (!foot[cur_y][cur_x - 1] && maze[cur_y + 1][cur_x] != '*')//left
                return true;
        }
        return false;
    case 4:
        if (cur_y > 0) {
            if (!foot[cur_y - 1][cur_x] && maze[cur_y][cur_x + 1] != '*')//up
                return true;
        }
        return false;
    }
    return false;
}
void SetFoot(int **foot,int di,int &cur_x,int &cur_y) {
    switch (di)
    {
    case 1:
        foot[cur_y][++ cur_x] = 1;
        break;
    case 2:
        foot[++ cur_y][cur_x] = 1;
        break;
    case 3:
        foot[cur_y][-- cur_x] = 1;
        break;
    case 4:
        foot[-- cur_y][cur_x] = 1;
        break;
    default:
        break;
    }
}
bool MazePath(char **maze,stack<int> &s,int x, int y) {//沒有圍牆
    int **foot = new int *[y];
    for (int i = 0; i < y; i++)
        foot[i] = new int[x];
    for (int i = 0; i < y; i++)
        for (int j = 0; j < x; j++)
            foot[i][j] = 0;
    int sx = GetStart(maze, x, y)[1];
    int sy = GetStart(maze, x, y)[0];
    foot[sy - 1][sx - 1] = 1;
    int di;
    int cur_x = sx - 1, cur_y = sy - 1;
    do {
        di = 1;
        while (NextStep(maze, foot, di, cur_x, cur_y, x, y) == false) {
            di++;
            if (di > 4)
                break;
        }
        if (di<=4) {
            s.push(di);
            SetFoot(foot, di, cur_x, cur_y);
        }
        else {
            int predi;
            if (!s.empty()) {
                predi = s.top();
                s.pop();
                if (predi == 3)
                    predi = 1;
                else if (predi == 1)
                    predi = 3;
                else if (predi == 2)
                    predi = 4;
                else
                    if (predi == 4)
                        predi = 2;
                SetFoot(foot, predi, cur_x, cur_y);
            }
        }
        if (cur_x + 1 == GetEnd(maze, x, y)[1] && cur_y + 1 == GetEnd(maze, x, y)[0])
            return true;
    } while (!s.empty());
    delete foot;
    return false;
}
void PrintPath(char **maze, stack<int> &s, int x, int y) {
    int ex = GetEnd(maze, x, y)[1];
    int ey = GetEnd(maze, x, y)[0];
    while (!s.empty()) {
        int di = s.top();
        switch (di)
        {
        case 1:
            if (ex>1)
                maze[ey][--ex] = '.';
            break;
        case 2:
            if (ey>1)
                maze[--ey][ex] = '.';
            break;
        case 3:
            maze[ey][++ex] = '.';
            break;
        case 4:
            maze[++ey][ex] = '.';
            break;
        default:
            break;
        }
        s.pop();
    }
}
void DeleteMaze(char **maze, int y) {
    for (int i = 0; i < y; i++)
        delete maze[i];
    delete maze;
}
int main() {
    int x, y;
    cin >> x >> y;//不包括圍牆
    char **maze=new char *[y+2];
    stack<int> s;
    cin.get();
    InitMaze(maze, x, y);
    if (MazePath(maze, s, x, y)) {
        PrintPath(maze, s, x, y);
        Display(maze, x + 2, y + 2);
        cout << "Got it!\n";
    }
    else
        cout << "There's no path.\n";
    DeleteMaze(maze, y + 2);
    system("pause");
    return 0;
}
/*



22 12
************************
*S***** **** *** *******
*  ** ******       *   *
** **     ** ***** * * *
**  * * ****    **   * *
***   * * ** ** ** *****
***** *  *** ***** * *E*
*   * ** *** **    *** *
* *   **  ** *   * *** *
* ** **  *** * *** *   *
* ** *  *  * *   **  ***
* ***** * ** ***    ****
*    *       *   **    *
************************




*/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章