#include "note.h"
#include<stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include<windows.h>
bool identify(int w_bz,int &k,char* _k) //当w_bz=0是表示判定数值不小于4,当w_bz为其他值时,表示判定数值不大于w_bz
{
k = atoi(_k);
if(w_bz==0)
{
return k>3&&k<=30?false:true;
}
else
return k<=w_bz?false:true;
}
bool set_roadblock(note *masenote[],int nn,int mm,int n,int m)
{
if (masenote[nn][mm].data != 1)
{
masenote[nn][mm].data = 1;
if (nn>0)
{
masenote[nn-1][mm].down = 1;
}
if(nn<n-1)
{
masenote[nn+1][mm].up = 1;
}
if(mm>0)
{
masenote[nn][mm-1].right = 1;
}
if(mm<m-1)
{
masenote[nn][mm+1].left = 1;
}
return false;
}
return true;
}
void print(note *mase[],int n,int m)
{
system("color 4F");
int j;
for (int i = -1;i<n+2;i++)
{
if(i==1)
cout<<" 入口—〉";
else
cout<<" ";
if(i == -1)
{
cout<<" ";
for(j = 1;j<=m;j++)
{
if(j<10)
cout<<" "<<j;
else
cout<<j;
}
cout<<endl;
}
else if(i == 0)
{
cout<<" ";
for ( j = 0;j<m;j++)
{
cout<<"_";
}
cout<<endl;
}
else if( i == n+1)
{
cout<<" ";
for ( j = 0;j<m;j++)
{
cout<<"ˉ";
}
cout<<endl;
}
else
{
for(j=-1;j<m+2;j++)
{
if(j == -1)
{
if(i<10)
cout<<" "<<i;
else
cout<<i;
}
else if(j==0)
{
if(i==1)
cout<<" ";
else
cout<<"|";
}
else if(j==m+1)
if(i==n)
cout<<" —〉出口"<<endl;
else
cout<<"|"<<endl;
else
{
if(mase[i-1][j-1].data == 0)
cout<<" ";
else if(mase[i-1][j-1].data == 1)
cout<<"■";
else if(mase[i-1][j-1].data == 2)
cout<<"〉";
else if(mase[i-1][j-1].data == 3)
cout<<"﹀";
else if(mase[i-1][j-1].data == 4)
cout<<"〈";
else
cout<<"︿";
}
}
}
}
cout<<endl;
}
bool find_outway(note *mase[],int n,int m,int i,int j,bool k)
{
if(i==n-1&&j==m-1)
{
mase[i][j].data=2;
return true;
}
if(mase[i][j].data==1)
{
return false;
}
if(mase[i][j].up!=0&&mase[i][j].right!=0&&mase[i][j].down!=0&&mase[i][j].left!=0)
{
return false;
}
else if(i==n-1&&j==m-1&&mase[i][j].data!=0)
{
return false;
}
else
{
k=false;
if(mase[i][j].right==0&&k==false) // ->
{
mase[i][j].data = 2;
Sleep(1000);
system("cls");
print(mase,n,m);
if (i>0)
{
mase[i-1][j].down = 1;
}
if(i<n-1)
{
mase[i+1][j].up = 1;
}
if(j>0)
{
mase[i][j-1].right = 1;
}
if(j<m-1)
{
mase[i][j+1].left = 1;
}
k = find_outway(mase,n,m,i,j+1,k); // ->
if(k==false)
{
mase[i][j].right = 1;
}
}
if(mase[i][j].down==0&&k==false) // |
{
mase[i][j].data = 3;
Sleep(1000);
system("cls");
print(mase,n,m);
if (i>0)
{
mase[i-1][j].down = 1;
}
if(i<n-1)
{
mase[i+1][j].up = 1;
}
if(j>0)
{
mase[i][j-1].right = 1;
}
if(j<m-1)
{
mase[i][j+1].left = 1;
}
k = find_outway(mase,n,m,i+1,j,k); // |
if(k==false)
{
mase[i][j].down = 1;
}
}
if(mase[i][j].left==0&&k==false) // <-
{
mase[i][j].data = 4;
Sleep(1000);
system("cls");
print(mase,n,m);
if (i>0)
{
mase[i-1][j].down = 1;
}
if(i<n-1)
{
mase[i+1][j].up = 1;
}
if(j>0)
{
mase[i][j-1].right = 1;
}
if(j<m-1)
{
mase[i][j+1].left = 1;
}
k = find_outway(mase,n,m,i,j-1,k); // <-
if(k==false)
{
mase[i][j].left = 1;
}
}
if(mase[i][j].up==0&&k==false) // ~
{
mase[i][j].data = 5;
Sleep(1000);
system("cls");
print(mase,n,m);
if (i>0)
{
mase[i-1][j].down = 1;
}
if(i<n-1)
{
mase[i+1][j].up = 1;
}
if(j>0)
{
mase[i][j-1].right = 1;
}
if(j<m-1)
{
mase[i][j+1].left = 1;
}
k = find_outway(mase,n,m,i-1,j,k); // ~
if(k==false)
{
mase[i][j].up = 1;
}
}
if(k == false)
{
mase[i][j].data = 0;
Sleep(1000);
system("cls");
print(mase,n,m);
}
return k;
}
}
bool findway(note *mase[],int n,int m)
{
int i = 0;
int j =0;
bool k = false;
return find_outway(mase,n,m,i,j,k);
}
void main()
{
char _n[2];
char _m[2];
int n,m;
bool k;
char q[2];
char qoi;
int i = 0,j = 0;
note *tomasenote[30];
do
{
cout<<" 菜单 "<<endl;
cout<<" 1.新建迷宫地图"<<endl;
cout<<" 2.输出路径结果"<<endl;
cout<<" 3.退出!"<<endl;
cout<<"请输入您的选择:";cin>>q;
switch(atoi(q))
{
case 1:
{
cout<<"*****************迷宫创建**************"<<endl;
cout<<"***请输入您要建立的迷宫大小(nxm,n表示行,m表示列(n,m不能大于30)):"<<endl;
do
{
k = false;
cout<<"n = ";cin>>_n;
if(identify(0,n,_n))
{
k = true;
cout<<"对不起!您的输入可能不符合要求或者数太小,请重新输入!"<<endl;
}
} while(k);
do
{
k = false;
cout<<"m = ";cin>>_m;
if(identify(0,m,_m))
{
k = true;
cout<<"对不起!您的输入可能不符合要求或者数太小,请重新输入!"<<endl;
}
} while(k);
cout<<endl;
note masenote[30][30];
for (int p =0;p<30;p++)
{
tomasenote[p] = &masenote[p][0];
if (p==0)
{
for(i=0;i<m;i++)
{
masenote[0][i].up = 1;
if(i==0)
{
masenote[0][0].left = 1;
}
if(i==m-1)
{
masenote[0][i].right = 1;
}
}
}
else if(p>0&&p<n-1)
{
masenote[p][0].left = 1;
masenote[p][m-1].right = 1;
}
else if(p==n-1)
{
for(i=0;i<m;i++)
{
masenote[p][i].down = 1;
if(i==0)
{
masenote[p][0].left = 1;
}
if(i==m-1)
{
masenote[p][i].right = 1;
}
}
}
}
masenote[n-1][m-1].data = 0;
cout<<"----------菜单-------"<<endl;
cout<<" 1.随机生成"<<endl;
cout<<" 2.自己输入"<<endl;
cout<<" 3.退出"<<endl;
cout<<"请输入您的选择:";cin>>q;
switch(atoi(q))
{
case 1:srand((unsigned)time(NULL));//srand()函数产生一个以当前时间开始的随机种子
int s;
do
{
s = rand()%(n*m);
} while(s<(n+m));
int nn,mm;
for (i;i<s;i++)
{
nn = rand();
mm = rand();
nn = nn%n;
mm = mm%m;
set_roadblock(tomasenote,nn,mm,n,m);
}
print(tomasenote,n,m);
break;
case 2:
print(tomasenote,n,m);
cout<<" 请输入您要输入的路障坐标(-1结束):"<<endl;
char nnn[2];
char mmm[2];
int wn,wm;
bool _bz;
i = 1;
do
{
cout<<"第"<<i<<"个路障:"<<endl;
do
{
k = false;
cout<<" n = ";cin>>nnn;
if(identify(n,wn,nnn))
{
k = true;
cout<<"对不起!您的输入可能不符合要求或者数超过范围,请重新输入!"<<endl;
}
} while(k);
if(wn==-1)
{