データ構造(C言語)課は7を設定します.迷宮は解決します.

37048 ワード

データ構造(C言語)課は7を設定します.迷宮は解決します.
テーマの説明:
m*nの長方陣は迷宮を表し、0と1はそれぞれ迷宮中の通路と障害を表します.プログラムを設計して、任意に設定した迷路に対して、入り口から出口までの通路を求めたり、通路がないという結論を出したりします.
#include 
#include 
using namespace std;
#define MAXSIZE 100
typedef struct
{
    int x;
    int y;
}Coordinate;//  
typedef struct
{
    Coordinate position;
    int direction;
}CoordinateDir;
typedef struct
{
    CoordinateDir *top;
    CoordinateDir *base;
    int stacksize;
}SqStack;
void InitStack(SqStack &S)
{//   
    S.base = new CoordinateDir[MAXSIZE];
    if(!S.base){
        cout<<"      "<<endl;
    }
    S.top = S.base;
    S.stacksize = MAXSIZE;
}
bool isEmpty(SqStack &S)
{
    if(S.base == S.top)
    {
        return true;
    }
    return false;
}
void Push(SqStack &S, CoordinateDir &e)
{//      
    if(S.top-S.base == S.stacksize){
        cout<<"  "<<endl;
    }
    *S.top++ = e;
}
void Pop(SqStack &S, CoordinateDir &e)
{//  
    if(S.base == S.top){
        cout<<"  "<<endl;
    }
    e = *--S.top;
}
Coordinate R_position;
Coordinate C_position;
bool isPass(int **a, Coordinate &Coo)//      
{
    if(a[Coo.x][Coo.y] == 0)
    {
        return false;
    }
    return true;
}
void signRoad(int **a, Coordinate &Coo)//       2
{
    a[Coo.x][Coo.y] = 2;
}
void SetWall(int **a, Coordinate &Coo)//    
{
    a[Coo.x][Coo.y] = 1;
}
CoordinateDir isGo(int **a, CoordinateDir &Dir)//           
{
    switch(Dir.direction)
    {
        case 1: Dir.position.x++;//   
                if((a[Dir.position.x][Dir.position.y] != 2) && (a[Dir.position.x][Dir.position.y] != 1))
                    break;//    
                Dir.position.x--;
        case 2: Dir.direction = 2;
                Dir.position.y++;//   
                if((a[Dir.position.x][Dir.position.y] != 2) && (a[Dir.position.x][Dir.position.y] != 1))
                    break;//    
                Dir.position.y--;
        case 3: Dir.direction = 3;
                Dir.position.x--;//   
                if((a[Dir.position.x][Dir.position.y] != 2) && (a[Dir.position.x][Dir.position.y] != 1))
                    break;//    
                Dir.position.x++;
        case 4: Dir.direction = 4;
                Dir.position.y--;//             2         ,        
                if((a[Dir.position.x][Dir.position.y] == 2) && (a[Dir.position.x][Dir.position.y] == 1))
                {
                    SetWall(a, Dir.position);//    
                    Dir.position.y++;
                }
                break;
    }
    return Dir;
}

void create_Maze(int **&a)//         ***a
{
    int n, m, load;
    cout<<"         :";cin>>n>>m;
    cout<<"     (   0  ,   1  ):"<<endl;
     a= new int *[50];

    //a = (int **)malloc(sizeof(int *) *n);
    for(int i = 1; i <= n; i++)
    {a[i] = new int[50];

        //a[i] = (int *)malloc(sizeof(int) *m);
        for(int j = 1; j <= m; j++)
        {
            cin>>load;
            a[i][j] = load;
        }
    }
    cout<<"       : "; cin>>R_position.x>>R_position.y;
    cout<<"       : "; cin>>C_position.x>>C_position.y;
    cout<<"      !"<<endl;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            cout<<a[i][j]<<" ";
        }
        cout<<endl;
    }
}
void go_Maze(SqStack &S, int **a)
{
    CoordinateDir Dir;
    Dir.position.x = R_position.x;
    Dir.position.y = R_position.y;

    do
    {
        if(isPass(a, Dir.position) == false)//     
        {
            Dir.direction = 1;//     
            Push(S, Dir);//       
            signRoad(a, Dir.position);//       
            if(Dir.position.x == C_position.x && Dir.position.y == C_position.y)//         
            {
                break;
            }
            Dir = isGo(a, Dir);//       
        }
        else//    
        {
            if(!isEmpty(S))
            {
                Pop(S, Dir);//      
                while(Dir.direction == 4 && !isEmpty(S))
                {
                    SetWall(a, Dir.position);
                    Pop(S, Dir);
                }
                if(Dir.direction < 4)
                {
                    Dir.direction++;//             
                    Push(S, Dir);//     
                    Dir = isGo(a, Dir);
                }
            }
        }
        if(Dir.direction == 4 && isEmpty(S))
        {
            cout<<"      !"<<endl;
            break;
        }
    }while(true);
}
void showPath(int **&a, SqStack &S)
{
    SqStack s;
    InitStack(s);
    CoordinateDir Dir;
    while(!isEmpty(S))
    {
        Pop(S, Dir);
        Push(s, Dir);
    }
    while(!isEmpty(s))
    {
        Pop(s, Dir);
        cout<<"(" <<Dir.position.x<<", "<<Dir.position.y<<")";
    }
}
int main()
{
    SqStack S;
    InitStack(S);
    int **a = NULL;
    create_Maze(a);
    go_Maze(S, a);
    showPath(a, S);
    return 0;
}
/*
1 1 1 0 1
1 1 0 0 1
1 1 0 1 1
1 0 0 1 1
1 0 1 1 1

1 1 1 0 1
1 1 0 0 1
1 1 1 1 1
1 1 1 1 0
1 1 1 1 1

1 1 1 1 1 1 1
1 1 1 0 0 1 1
1 1 0 0 1 1 1
1 1 0 1 1 1 1
1 0 0 1 1 1 1
1 1 0 0 1 1 1
1 1 1 1 1 1 1


1 1 1 1 1 1 1 1 1 1
1 0 0 1 0 0 0 1 0 1
1 0 0 1 0 0 0 1 0 1
1 0 0 0 0 1 1 0 0 1
1 0 1 1 1 0 0 0 0 1
1 0 0 0 1 0 0 0 0 1
1 0 1 0 0 0 1 0 0 1
1 0 1 1 1 0 1 1 0 1
1 1 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1
*/

このブログは勉強の過程を記録するためだけに使われます.