(3)走迷宫(vector 和 struct两种分别实现)

21 篇文章 1 订阅
// sf4.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
# include <iostream>
#include <vector>
using namespace std;

const int SIZE_X=10;
const int SIZE_Y=10;
 bool initArray[SIZE_X][SIZE_Y]={
	{false,false,false,false,false,false,false,false,false,false},  
    {false,true ,true ,false,true ,true ,true ,false,true ,false},
    {false,true ,true ,false,true ,true ,true ,false,true ,false},
    {false,true ,true ,true ,true ,false,false,true ,true ,false},
    {false,true ,false,false,false,true ,true ,true ,true ,false},
    {false,true ,true ,true ,false,true ,true ,true ,true ,false},
    {false,true ,false,true ,true ,true ,false,true ,true ,false},
    {false,true ,false,false,false,true ,false,false,true ,false},
    {false,false,true ,true ,true ,true ,true ,true ,true ,false},
    {false,false,false,false,false,false,false,false,false,false}
 };//迷宫矩阵
 bool initArray1[SIZE_X][SIZE_Y]={
	{false,false,false,false,false,false,false,false,false,false},  
    {false,true ,true ,false,true ,true ,true ,false,true ,false},
    {false,true ,true ,false,true ,true ,true ,false,true ,false},
    {false,true ,true ,true ,true ,false,false,true ,true ,false},
    {false,true ,false,false,false,true ,true ,true ,true ,false},
    {false,true ,true ,true ,false,true ,true ,true ,true ,false},
    {false,true ,false,true ,true ,true ,false,true ,true ,false},
    {false,true ,false,false,false,true ,false,false,true ,false},
    {false,false,true ,true ,true ,true ,true ,true ,true ,false},
    {false,false,false,false,false,false,false,false,false,false}
 };//迷宫矩阵
//data struct

 
 /算法一
 ///
struct mPoint
{
    int x;
    int y;
    bool can_move_to;
    mPoint(int rx=0,int ry=0,bool rcan_move_to=false)
    {
        x=rx;
        y=ry;
        can_move_to=rcan_move_to;
        next=NULL;
    }
    mPoint *next;
};
//function declaration
class mStack
{
public:
    mStack();//constructor
    int push(mPoint point);
    mPoint pop();
    int getLength();
    mPoint getTop();
    void printStack();
private:
    mPoint *base;//base pointer
    mPoint *top; //top pointer
    int length; //length of stack
};

mStack::mStack()
{
    length=0;
    base=NULL;
    top=NULL;
}

int mStack::push(mPoint point)
{
    mPoint *mpNode=new mPoint();
    *mpNode=point;
    if(length==0)
        top=base=mpNode;
    else
    {
        top->next=mpNode;
        top=mpNode;
    }
    return ++length;
}
mPoint mStack::getTop()
{
    return *top;
}

mPoint mStack::pop()
{
    if(length<=0)
        return NULL;
    mPoint retPoint=*top;
    top=base;
    while(top->next!=NULL)
    {
        if(top->next->next==NULL)
        {
            delete(top->next);
            top->next=NULL;
            break;
        }
        top=top->next;
    }
    if(length==1)
    {
        delete(base);
        base=top=NULL;
    }
    length--;
    return retPoint;
}

int mStack::getLength()
{
    return length;
}

void mStack::printStack()
{
    mPoint *p=base;
    while(p!=NULL)
    {
        cout<<"("<<p->x<<","<<p->y<<")"<<endl;
        p=p->next;
    }
   
}

/算法二
//
std::vector<int> path1;  //存储路径
int visit(int i,int j)//入口
{
	if(path1.empty())
	{
      if(initArray1[i+1][j])
	  {path1.push_back(0);visit(i+1,j);}
	  else if(initArray1[i][j+1])
	  {  path1.push_back(1);visit(i,j+1);}
	  else if(initArray1[i-1][j])
	  {  path1.push_back(2);visit(i-1,j);}
	  else if(initArray1[i][j-1])
	  {	  path1.push_back(3);visit(i,j-1);}
	}
	else
	{
	 if(i==8&&j==8)
	  {
		vector<int>::iterator iter;
		for(iter=path1.begin();iter!=path1.end();iter++)
		{
			cout<<*iter<<" "<<endl;
		}
	  cout<<"the end"<<endl;
	  return 1;
	  }
		else
	  {
	  if((initArray1[i+1][j])&&(path1.back()!=2))
	  { path1.push_back(0);visit(i+1,j);}
	  else if((initArray1[i][j+1])&&(path1.back()!=3))
	  { path1.push_back(1);visit(i,j+1);}
	  else if((initArray1[i-1][j])&&(path1.back()!=0))
	  {	path1.push_back(2);visit(i-1,j);}
	  else if((initArray1[i][j-1])&&(path1.back()!=1))
	  {	path1.push_back(3);visit(i,j-1);}
	  else
	  {
	  initArray1[i][j]=false;
	  if(path1.back()==0)
	  {path1.pop_back();visit(i-1,j);}
	  else if(path1.back()==1)
	  {path1.pop_back(); visit(i,j-1);}
	  else if(path1.back()==2)
	  {  path1.pop_back();visit(i+1,j);}
	  else if(path1.back()==3)
	  { path1.pop_back(); visit(i,j+1);}
	  }
	  }	  
	}

}


//


int main()
{


/
//
    mPoint mpArray[SIZE_X][SIZE_Y];
   
    for(int i=0;i<SIZE_X;i++)//init
        for(int j=0;j<SIZE_Y;j++)
        {
            mpArray[i][j].x=i;
            mpArray[i][j].y=j;
            mpArray[i][j].can_move_to=initArray[i][j];
        }
        mPoint startp(1,1,true);//entry
        mPoint endp(8,8,true);  //exit
       
        mStack mpath;
        mpath.push(startp);
       
        mPoint mp=startp;
        while(true)
        {
            if(mp.x==endp.x && mp.y==endp.y)
                break;//success
            if(mpArray[mp.x+1][mp.y].can_move_to)//search down
            {
                mpArray[mp.x+1][mp.y].can_move_to=false;
                mpath.push(mPoint(mp.x+1,mp.y));
                mp=mpArray[mp.x+1][mp.y];
                continue;
            }
            if(mpArray[mp.x-1][mp.y].can_move_to)//search up
            {
                mpArray[mp.x-1][mp.y].can_move_to=false;
                mpath.push(mPoint(mp.x-1,mp.y));
                mp=mpArray[mp.x-1][mp.y];
                continue;
            }
            if(mpArray[mp.x][mp.y+1].can_move_to)//search right
            {
                mpArray[mp.x][mp.y+1].can_move_to=false;
                mpath.push(mPoint(mp.x,mp.y+1));
                mp=mpArray[mp.x][mp.y+1];
                continue;
            }
            if(mpArray[mp.x][mp.y-1].can_move_to)//serch left
            {
                mpArray[mp.x][mp.y-1].can_move_to=false;
                mpath.push(mPoint(mp.x,mp.y-1));
                mp=mpArray[mp.x][mp.y-1];
                continue;
            }
            if(0==mpath.getLength())
            {
                cout<<"No path!"<<endl;
                return -1;
            }
            mpath.pop();
            mp=mpath.getTop();
        }
        cout<<"Path:"<<endl;
        mpath.printStack();//ouput path
		cout<<"&&&&&&"<<endl;



		int k=visit(1,1);
       system("pause");
        return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值