迷宫程序的实现

来源:互联网 发布:华为分享网络wifi密码 编辑:程序博客网 时间:2024/05/18 00:40
#include<iostream>#include<stack>#define N 3#define M 3using namespace std;struct STEP   //保存坐标点{    int x;    int y;};struct Matrix{    int maxtrix[N+2][M+2];  //迷宫    struct STEP entrance;   //入口    struct STEP exit;    //出口}; struct Dir        //方向{   struct STEP right;   struct STEP down;   struct STEP left;   struct STEP up;};struct STEP dir[4]={{0,1},{1,0},{0,-1},{-1,0}};Matrix mm = {{{1,1,1,1,1},{1,0,1,0,1},{1,0,0,1,1},{1,0,0,0,1},{1,1,1,1,1}},{1,1},{3,3}};int main(){    stack<struct STEP> path;    struct STEP cur = mm.entrance;    path.push(cur);    int i = 0;    for(int i=0;i<=4;i++)    {        for(int j=0;j<=4;j++)            cout<<mm.maxtrix[i][j]<<" ";        cout<<endl;    }    while(1)    {        cur.x = cur.x + dir[i%4].x;        cur.y = cur.y + dir[i%4].y;        path.push(cur);        if(mm.maxtrix[cur.x][cur.y] == 1)        {            path.pop();            cur = path.top();            ++i;            continue;        }        else        {            i = 0;            if(cur.x == mm.exit.x && cur.y == mm.exit.y)            {                break;            }        }    }    while(!path.empty())   //逆向输出路径    {        struct STEP cur = path.top();        cout<<"("<< cur.x<<","<<cur.y<<")"<<endl;        path.pop();    }    return 0;}

0 0
原创粉丝点击