迷宫的游戏规则想必大家都会吧,我就不多说了,直接看代码吧!

C语言:
/*
*  Author: Wang Runzhen
*  Date  : 2009 年 8 月 14 日
*/

#include<iostream>
#include<fstream>
using namespace std;

//用于保存行走路线
struct Stack
{
int info[50][3];
int point;
};

//初始化栈
void InitStack(Stack*s)
{
s->point=-1;
}
//入栈
void PushStack(Stack*s,int x,int y,int d)
{
s->point++;
s->info[s->point][0]=x;
s->info[s->point][1]=y;
s->info[s->point][2]=d;

}

//出栈
void PopStack(Stack*s,int &d)
{
d=s->info[s->point][2];
s->point;
}

//判断为空
int Isempty(Stack*s)
{
if(s->point==-1)
return 1;
else
return 0;
}
//方向数组
int dir[8][2]= {{0,1},{1,1},{1,0},{1,1},{0,1},{1,1},{1,0},{1,1}};
//全局数组-迷宫
int map[10][10];

//打印出迷宫的路径

void Path()
{
Stack s;
InitStack(&s);
int i,j,x=1,y=1,d,dd=0,flag,found=0;
for(; !found;) //2 is the out
{
flag=0;
for(d=dd; d<8; d++) //对于某一点,从八个方向查找通行路
{
i=x+dir[d][0];
j=y+dir[d][1];
if(map[i][j]==2)
{
found=1;
cout<<“(“<<i<<“,”<<j<<“)”<<endl;
break;
}
if(map[i][j]==0)//如果可行
{
cout<<“(“<<i<<“,”<<j<<“)”<<endl;
x=i;
y=j;
map[x][y]=-1;
PushStack(&s,x,y,d);//路线压栈
flag=1;//判断
dd=0;
break;
}

}
if(flag==0)//八个方向都没有路线
{
PopStack(&s,dd);//popstack

d=dd;
}
}

}

//打印出迷宫
void Display()
{
int i,j;
cout<<”     “<<“0 1 2 3 4 5 6 7 8 9 \n\n<<endl<<endl;
for(i=0; i<10; i++)
{
cout<<i<<”    “;
for(j=0; j<10; j++)
cout<<map[i][j]<<” “;
cout<<endl;
}
}
///////////////////////////////
//函数功能:主函数
///////////////////////////////
int main()
{
ifstream fin(“map.txt”);
int i,j;

for(i=0; i<10; i++)
{
for(j=0; j<10; j++)
fin>>map[i][j];
}

Display();

int x,y,d;
Stack s;
InitStack(&s);
Path();

return 0;
}

Tags: ,,. 2,436 views
Home

No Comments Now!

Be the first to comment on this entry.

Leave a comment

Name(required)
Mail (required),(will not be published)
Website(recommended)

Fields in bold are required. Email addresses are never published or distributed.

Some HTML code is allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
URLs must be fully qualified (eg: http://blog.nlogn.cn),and all tags must be properly closed.

Line breaks and paragraphs are automatically converted.

Please keep comments relevant. Off-topic, offensive or inappropriate comments may be edited or removed.