//链表栈

#include "stdafx.h"
#include<iostream>
using namespace std;
 
typedef struct{
    int line;
    int count;
    int dir;
}Elemtype;
 
typedef struct s{
    Elemtype  elem ;
     s* next;      
}s;//链表栈的结点类型为结构体;

void initstack(s*&base)
{    base=(s*) malloc (sizeof(s));
    base->next=NULL;
}

void push(s*&base ,Elemtype &e)
{   s* t=(s*) malloc (sizeof(s));      
     t->elem=e;      
    t->next =base->next ;
    base->next=t;
}
bool stackempty(s* base)
{
    if(base->next==NULL)
        return true;
    else return false;
}
bool pop(s*&base,Elemtype & e)
{       s* t=new s;
     if( stackempty(base))return false;
    else
        {  t=base->next;
           e=t->elem;         
           base->next=t->next;
           free(t);       
        }
    return true;
}
bool getstack(s*&base,Elemtype &e)
{  
    s* p=base  ;Elemtype  t;
    while(p->next )
    {    p=p->next ;t=p->elem;
        if(t.line ==e.line && t.count==e.count )
            return true;}
    return false;
}
        
#include"stdafx.h"
#include<iostream>
using namespace std;
#include"链表栈.h"
typedef int R[100][100];

int h ,l ,i=0,j=0,x=0,y=0;int dir;
R rs;
Elemtype e;
bool m=false,f=false,n=false;
s* base=new s;  
int sum=0;
void enter(bool &f)
{
    cout<<"请输入路口在矩阵中的位置i和j:"<<endl;cin>>i;cin>>j;
    
    cout<<"请输入出口在矩阵中的位置x和y:"<<endl; cin>>x;cin>>y;  
    
    if(rs[i][j]==1||rs[x][y]==1){cout<<"迷宫走不通"<<endl;f=true; }
    else f=false;
     
};//确定进出口位置;
bool getelem(int i,int j,int dir)
{      
  if(dir>3)return false;  
    e.line=i;
    e.count=j;
    e.dir =dir;   
    
    return true;
 
}//得到栈元素;
void change(Elemtype e)
{      
    if(e.dir==0) {j=e.count+1;i=e.line;}
    else if(e.dir==1) {i=e.line+1;j=e.count;}
    else if(e.dir==2) {j=e.count-1;i=e.line;}
    else if(e.dir==3) {i=e.line-1;j=e.count;}
    

}//得到下一步方向;


void mglk()
{cout<<"请输入行的长度:"<<endl;cin>>h;
cout<<"请输入列的长度:"<<endl;cin>>l ;

char choice;
cout<<"请选择是否自动输入,若是请输入a或A;否则为手动输入"<<endl;
cin>>choice;

if(choice!='a'||choice!='A')
cout<<"请用0和1表示迷宫中的通路和障碍并输入:"<<endl;

for(i=1;i<h+1;i++)
   for(j=1;j<l+1;j++)
   {if(choice=='a'||choice=='A')
    rs[i][j]=rand()%2;
   else cin>>rs[i][j];}
     
for(i=0;i<h+2;i++)
{rs[i][0]=1;rs[i][l+1]=1;}
for(i=0;i<l+2;i++)
{rs[0][i]=1;rs[h+1][i]=1;}

        
}//确定迷宫路况;


void  mgsf()
{  
    enter(f);
   initstack(base);    
    if(!f)
    {
    rs[x][y]=-3;
    while(1)
    {
        if(rs[i][j]==-3){cout<<"!!!!!"<<endl;cout<<"迷宫走通!!!!!!!!"<<endl;break;}
        else
              {
               if(rs[i][j]==0)
                 {getelem(i,j,0);    push(base,e); change(e);}

                 else
                 {
                    if(!pop(base,e)) {cout<<"~~~~~~~~~迷宫走不通"<<endl; break;}
                    
                    do{
                        while( !getelem( e.line,e.count,++e.dir))
                          if(!pop(base,e))
                            { cout<<"迷宫走不通"<<endl;n=true; break;}                
                          if(n)break;    
                    }while(getstack(base,e));

                    if(n)break;       
                   
                   push(base,e);change(e);    
               
                }//if-else;
              }//else
    }//while
    }//if
}



void show()
{    for(i=1;i<h+1;i++)
    {  for(j=1;j<l+1;j++)
           cout<<rs[i][j]<<",";
       cout<<endl;
    }
}//输出迷宫路径;
void showstack()
{   
    while(pop(base,e))
        cout<<"i="<<e.line <<","<<"j="<<e.count<<endl;
}//输出走得路径;


// 迷宫问题.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
using namespace std;
void mglk();
void mgsf();
void show();
void showstack();
int _tmain(int argc, _TCHAR* argv[])
{  
    int x;
    
    mglk();
    show();
    mgsf();
    showstack();
    cout<<"停下来:"<<endl;
    cin>>x;

    return 0;
}

 

 

posted on 2013-02-25 19:37  叶城宇  阅读(241)  评论(0编辑  收藏  举报