我想写一个迷宫,该看哪方面的东西,请指教!!!

wahao 2003-10-16 01:24:03
我想写一个迷宫,该看哪方面的东西,请指教!!!
这个迷宫能自动生成.先谢了!!1
...全文
39 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
apogeecsj 2003-10-17
  • 打赏
  • 举报
回复
/*This is solve to maze problem,if you want auto get the maze use rand() to get
*---------------------------running------------------------------------*/
#include <iostream>
#include <vector>
#include <cstdlib>

using namespace std;

/*------a template link list stack for this problem ---------*/
template< class T >
struct Snode {
T data;
Snode* next;
};

template< class T >
class mStack {
Snode<T>* end;
Snode<T>* p;
public:
int size;
mStack();
bool isEmpty();
bool GetTop( T& d );
void Push( const T& d );
bool Pop();
~mStack() {
delete p;
delete end;
}
};


template< class T >
mStack<T>::mStack() {
size = 0;
end = ( Snode<T>* )malloc( sizeof( Snode<T> ) );
p = end;
}
template< class T >
bool mStack<T>::isEmpty() {
if( size > 0 )
return false;
return true;
}
template< class T >
void mStack<T>::Push( const T& d ) {
Snode<T>* temp;
temp = ( Snode<T>* )malloc( sizeof( Snode<T> ) );
temp->data = d;
temp->next = p;
p = temp;
size++;
}
template< class T >
bool mStack<T>::GetTop( T& d ) {
if( !isEmpty() )
{
d = p->data;
return true;
}
return false;
}
template< class T >
bool mStack<T>::Pop() {
Snode<T>* f;
if( !isEmpty() )
{
f = p;
p = p->next;
free( f );
size--;
return true;
}
return false;
}
/*---------link list stack end------------------------*/

struct Position {
int x;
int y;
};
enum Direction { Right, Down, Left, Up };

struct ElemType {
int step;
Position c_pos; //current position
Direction di; //next direction
};

mStack< ElemType > mstack;

// map information
vector< vector<int> > map;
int m_rows;
int m_cols;
// first enter into maze position
int b_x = 0;
int b_y = 0;
// exit the maze position
int e_x = 0;
int e_y = 0;

void InitMaze( const int rows, const int cols ) {
int i = 0;
int j = 0;
map.resize( rows+2, vector<int>(cols+2) );
for( i = 0; i < rows+1; i++ ) // set wall
{
map[i][0] = 5;
map[i][cols+1] = 5;
}
for( i = 0; i < cols+2; i++ ) //set wall
{
map[0][i] = 5;
map[rows+1][i] = 5;
}
for( i = 1; i < rows+1; i++ ) //get input
{
for( j = 1; j < cols+1; j++ )
{
cin >> map[i][j];
}
}
}

bool is_out( int x, int y ) { //if walked out the map
if( ( e_x == x )&&( e_y == y ) )
return true;
return false;
}

Position get_next_step( const ElemType& p ) {
Position temp;
switch( p.di ) {
case Left: temp.x = p.c_pos.x; temp.y = p.c_pos.y-1; break;
case Right: temp.x = p.c_pos.x; temp.y = p.c_pos.y+1; break;
case Up: temp.x = p.c_pos.x-1; temp.y = p.c_pos.y; break;
case Down: temp.x = p.c_pos.x+1; temp.y = p.c_pos.y; break;
default: cerr << "Unknow dirction" << endl;
}
return temp;
}
void walk( ElemType& cur, int x, int y ) {
map[x][y] = 3;
cur.c_pos.x = x;
cur.c_pos.y = y;
cur.step++;
}

bool MazePath( int b_x, int b_y ) {
ElemType t;
Position pos;
Position temp_pos;
t.step = 0;

if( is_out( b_x, b_y ) )
{
cout << "this position can't be begin walk " << endl;
return false;
}
if( map[b_x][b_y] != 0 )
{
cout << "this position can't be begin walk " << endl;
return false;
}
walk( t, b_x, b_y );
mstack.Push( t );
temp_pos = t.c_pos;
while( 1 )
{
t.di = Right;
pos = get_next_step( t );
if( is_out( pos.x, pos.y ) ) break;
if( map[pos.x][pos.y] == 0 )
{
walk( t, pos.x, pos.y );
mstack.Push( t );
temp_pos = t.c_pos;
}
else {
t.di = Down;
pos = get_next_step( t );
if( is_out( pos.x, pos.y ) ) break;
if( map[pos.x][pos.y] == 0 )
{
walk( t, pos.x, pos.y );
mstack.Push( t );
temp_pos = t.c_pos;
}
else
{
t.di = Left;
pos = get_next_step( t );
if( is_out( pos.x, pos.y ) ) break;
if( map[pos.x][pos.y] == 0 )
{
walk( t, pos.x, pos.y );
mstack.Push( t );
temp_pos = t.c_pos;
}
else {
t.di = Up;
pos = get_next_step( t );
if( is_out( pos.x,pos.y ) ) break;
if( map[pos.x][pos.y] == 0 )
{
walk( t, pos.x, pos.y );
mstack.Push( t );
temp_pos = t.c_pos;
}
else {
if( !mstack.isEmpty() )
{
map[temp_pos.x][temp_pos.y] = 2;
mstack.GetTop( t );
mstack.Pop();
temp_pos = t.c_pos;
}
else
{
cout << "No Path" << endl;
break;
}

}
}
}
}
}
walk( t, pos.x, pos.y ); // deal with the last position
mstack.Push( t );

return true;
}

void PrintMaze( int rows, int cols ) {
for( int i = 1; i < rows+1; i++ )
{
for( int j = 1; j < cols+1; j++ )
{
if( map[i][j] == 1 ) cout << '#' << " ";
else if( map[i][j] == 0 ) cout << " ";
else if( map[i][j] == 2 ) cout << '@' << " ";
else if( map[i][j] == 3 ) cout << '*' << " ";
}
cout << endl;
}
}

int main(int argc, char* argv[])
{

cout << " enter rows and cols: " << endl;
cin >> m_rows;
cin >> m_cols;
cout << "enter information for maze:" << endl;
InitMaze( m_rows , m_cols );
cout << "enter begin position, x and y " << endl;
cin >> b_x;
cin >> b_y;
cout << "enter end position x and y " << endl;
cin >> e_x;
cin >> e_y;
MazePath( b_x, b_y );
cout << "*****after walk******" << endl;
PrintMaze( m_rows, m_cols );


return 0;
}
tudou614 2003-10-16
  • 打赏
  • 举报
回复
其实我发现,卡住许多朋友的都在KMP,就连严老师也是如此说!!!!!!!!!!!!
tudou614 2003-10-16
  • 打赏
  • 举报
回复
严老师的书上就有迷宫的解释,认真看一看!!!!!!!!!!!!
Riemann 2003-10-16
  • 打赏
  • 举报
回复
dcyu那儿有他写的几个迷宫的几个版本
vip.6to23.com/dcyu
ZhangYv 2003-10-16
  • 打赏
  • 举报
回复
把论坛上所有关于迷宫的旧贴看懂,大概就能自己写了吧?

33,006

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧