迷宫c++源代码 by Reason

思路:

定义一个长宽可定义的数组,入口在arry[0][0]处,出口在arry[N][M]处,利用Rand()函数得出0~3区间中的一个整数,然后根据结果可得出正确路径随机的4种走向(上下左右),最后再选取数组中的3份之1(1/2难度太低,1/4路径太明显)点令其为可走点,便可得到一幅必有至少一条正确路径的迷宫。

改进:

正确路径的随机走向减少为2种(右和下),因为有时会出现随机数概率太过平均导致全图几乎都是可走点。


由于未参考任何其他人的思路及代码,纯属自己的个人思路,因此仅供参考,谢谢。

001040_E6sk_1476402.png


/*By Reason*/
#include<iostream>
#include<windows.h>
#include<iomanip>
#include<stdlib.h>
#include<time.h>
#include <conio.h>//为了读取方向键
#define N 20 
#define M 40
using namespace std;
int pane[N][M]={0};
int nowlocationi=0,nowlocationj=0,stepnumber=0,flag=0;
void showpane()
{
cout<<"入口->";
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
if((i==nowlocationi)&&(j==nowlocationj))
cout<<"U";
else 
{
if(pane[i][j]==0)
cout<<".";
else
cout<<" ";
}
}
if(i==N-1)
cout<<"--->出口!"<<endl;
else
{
cout<<"."<<endl;
cout<<"      ";
}
}
cout<<"      ";
for(int t=0;t<M;t++)
cout<<".";
cout<<endl;
cout<<"      By Reason。 "<<endl;
cout<<"      您当前的步数为:"<<stepnumber<<endl;
}
void newgame()
{
stepnumber=0;
nowlocationi=0;
nowlocationj=0;
srand((unsigned)time(0));
for(int i=1;i<N;i++)
for(int j=1;j<M;j++)
{
int p=rand()%2;
if(p==1)
pane[i][j]=1;
else 
pane[i][j]=0;
}
int newgamei=0,newgamej=0;
while(newgamei!=(N-1)||newgamej!=(M-1))
{
pane[0][0]=1;
//srand((unsigned)time(0));
int x=rand()%4;
switch (x)
{
case 0:
if(newgamei<N-1)
pane[++newgamei][newgamej]++;
break;
case 1:
if(newgamei<N-1)
pane[++newgamei][newgamej]++;
break;
case 2:
if((newgamej<M-1))
pane[newgamei][++newgamej]++;
break;
case 3:
if(newgamej<M-1)
pane[newgamei][++newgamej]++;
break;
/*case 4:
if(newgamei>1)
pane[--newgamei][newgamej]++;
break;
case 5:
if(newgamej>1)
pane[newgamei][--newgamej]++;
break;*/
default:
break;
}
}
}
int GetDirection()//读取方向
{
    int ret = 0;
 
    do 
    {
        int ch = _getch();
        if(isascii(ch))
            continue;
 
        ch = _getch();
        switch(ch)
        {
        case 72:   
            ret = 2; // top
            break;
        case 75:   
            ret = 1; // left 
            break;
        case 77:   
            ret = 3; // right
            break;
        case 80:   
            ret = 4; // down
            break;
        default:   
            break;
        }
    } while (ret == 0);
     
    return ret;
}
void move()
{
int c=GetDirection();
switch(c)
{
case 2://上
if(nowlocationi>0&&pane[nowlocationi-1][nowlocationj]!=0)
{
nowlocationi--;
flag=1;
}
else
flag=0;
break;
case 4://下
if(nowlocationi<N-1&&pane[nowlocationi+1][nowlocationj]!=0)
{
nowlocationi++;
flag=1;
}
else
flag=0;
break;
case 1://左
if(nowlocationj>0&&pane[nowlocationi][nowlocationj-1]!=0)
{
nowlocationj--;
flag=1;
}
else
flag=0;
break;
case 3://右
if(nowlocationj<M-1&&pane[nowlocationi][nowlocationj+1]!=0)
{
nowlocationj++;
flag=1;
}
else
flag=0;
break;
default:   
break;
}
}
void main()
{
//system("color e9");
int makesure=1;
while(makesure)
{
system("cls");
newgame();
showpane();
while((nowlocationi!=(N-1))||(nowlocationj!=(M-1)))
{
move();
//Sleep(1000);
if(flag==1)
{
stepnumber++;
system("cls");
showpane();
}
}
cout<<"      你的最后成绩为:"<<stepnumber<<endl;
cout<<"      若要重新开始游戏请输入1,若要结束请输入0。"<<endl;
cin>>makesure;
while(makesure!=1&&makesure!=0)
{
cout<<"   输入不正确,请重新输入!"<<endl;
cin>>makesure;
}
}
cout<<"      再见!"<<endl;
system("pause");
}


转载于:https://my.oschina.net/chenreason/blog/217575

#include<iostream> #include<windows.h> #include "conio.h" using namespace std; const int m=10 ; //迷宫行数 const int n=15 ; //迷宫列数 struct migong //结构体迷宫,,,pre为前驱,,,xy 为当前目标 { int x,y; int pre; }sq[500]; //最大成员500个,,迷宫大小,,,,, int mg[m+2][n+2]; //迷宫数组 外带边2个 ,, int zx[8+1],zy[8+1]; //迷宫方向数组 九宫格,,,他在中间,,,所以8个方向 void printlj(int rear) //打印迷宫路径 { int i; i=rear; //保存当前节点前驱,,为了在,,向前试探失败时,返回,,换一个方向继续 do { cout<<sq[i].x<<sq[i].y; //输出当前,,X,y值,,其实就是地址。。。。 i=sq[i].pre; // 保存这个节点的前驱 }while(i!=0); //i!=0表示通路 } void mglj() //球迷宫最短路径 { int i,j,x,y,v,front,rear,found; sq[1].x=sq[1].y=1;sq[1].pre=0; //从(1,1)开始搜索 。。。。左上角开始 found=0; //初始化。 front=rear=1; //初始化 mg[1][1]=-1; //开始节点就不需要来回遍历了,,,不设为-1,,一样可以算,,对于CPU来说无所谓 while((front<=rear)&&(!found)) //found初始化,,等于0 ,,,,这里是一个BUG,,,因为找到了,,他就退出了,,,很多时候不止一条路径 { x=sq[front].x; // y=sq[front].y; //引入临时变量x,y,,,保存当前,,,因为下面要进行,,,查找遍历,,,为了能够回到当前节点,,在无法试探的情况下 for(v=1;v<=8;v++) //循环扫描8个方向 {
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值