请老大们帮忙~ 迷宫题

Home Home
引用 | 编辑 湛蓝威尼斯
2005-11-11 08:43
楼主
推文 x0
随机会有不同的地图~
走迷 ..

访客只能看到部份内容,免费 加入会员



献花 x0
引用 | 编辑 aa
2005-11-11 11:13
1楼
  
你是要用程式产生不同的迷宫还是用程式去走迷宫..? 表情

献花 x0
引用 | 编辑 湛蓝威尼斯
2005-11-11 21:14
2楼
  
产生迷宫后~ 走迷宫~~
需有图~!
例如
100000
010000
001110
000011

要看到有在走~
表情

献花 x0
引用 | 编辑 唐老鸭
2005-11-12 09:50
3楼
  
下面是引用湛蓝威尼斯于2005-11-11 21:14发表的 :
产生迷宫后~ 走迷宫~~
需有图~!
例如
100000
010000
.......

题目再给明确一点....
例如老师有规定阵列要多大还是有些什么限定的...
晚点写一个产生迷宫的参考程式给你 ....
走的部份这里有可以参考的程式 表情...

献花 x0
引用 | 编辑 唐老鸭
2005-11-16 11:09
4楼
  
刚花了点时间帮你把自动产生迷宫的程式写出来了 表情 .....
33*33的迷宫应该够你用了吧....
至于走的方法请你自己参考程式区有你要的写法....
下面我帮你加了些注解...
但也许对你会有些复杂....
请自己努力看懂吧....
不然就参考一些有关游戏设计的书吧....
我自己也有东西要做....
所以我没时间解释给你听 表情 ....

复制程式
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#define row 35
#define column 35

int maze[row][column];

void generatemaze(int x,int y)
{
       int direction;
       maze[x][y] |= 0x1;  // 标示此格已设定
    
       while (maze[x][y+2]==0 || maze[x+2][y]==0 || maze[x][y-2]==0 || maze[x-2][y]==0) // 如果不是外墙
    {
         direction = rand()%4+1;  // 决定下一个位置
         
               if (direction==1 && maze[x][y+2]==0) // 向右走
               {
                      maze[x][y+1] |= 0x1; // 拆掉右墙
                      generatemaze(x,y+2);
         }
         else if (direction==2 && maze[x-2][y]==0) // 向上走
               {
                      maze[x-1][y] |= 0x1; // 拆掉上墙
                      generatemaze(x-2,y);
         }
         else if (direction==3 && maze[x][y-2]==0) // 向左走
               {
                      maze[x][y-1] |= 0x1; // 拆掉右墙
                      generatemaze(x,y-2); 
         }
         else if (direction==4 && maze[x+2][y]==0) // 向下走
               {
                      maze[x+1][y] |= 0x1; // 拆掉上墙
                      generatemaze(x+2,y); 
         }
    }      
}
void main()
{
       int Si=2,Sj=2,Ei=row-3,Ej=column-3;
       int x,y;
       
       srand(time(NULL));
       
       for (x=0;x<row;x++)
       {
              for (y=0;y<column;y++)
              {
                     if (x==0 || y==0 || y==1 || x==row-2 || x==row-1 || y==column-1 )
                            maze[x][y]=2; // 设定外墙
            else
                            maze[x][y]=0;  // 初始迷宫内部
        }
    }
       
       generatemaze(Ei,Ej); // 产生迷宫
    maze[Si-1][Sj] |= 0x1; // 拆掉入口上墙
       maze[Ei][Ej+1] |= 0x1; // 拆掉出口右墙

       for(x=1; x<row-1; x++)
       {
              for(y=1; y<column-1; y++)
              {
                     if (maze[x][y] == 0 || maze[x][y] == 2)
                            cout<<"█"; 
            else
                            cout<<"  ";
              }
              cout<<endl;
       }
}
请自己排版吧...好懒 表情 .....

献花 x1
引用 | 编辑 湛蓝威尼斯
2005-11-28 21:24
5楼
  
最近忙期中~ 忙报告~ 今天才有时间上来看~
看谢大大回答~ 我们作业是随机产生一个 1024*768 的地图~
老鼠走迷宫~ 从左上(入口)到右下(出口)走迷宫~
所以根据大大给的程式码~ 改个变数数据应该就可以 1024*768
非常感谢大大给的范例~ 走迷宫方式我再爬文~
研究不出来~ 再题出来问~!! 表情 表情

献花 x0
引用 | 编辑 rakish
2005-11-30 15:00
6楼
  
书上的..刚好看到...
复制程式
int maze[7][10]={
 1,1,1,1,1,1,1,1,1,1,
 1,0,1,0,1,0,0,0,0,1,
 1,0,1,0,1,0,1,1,0,1,
 1,0,1,0,1,1,1,0,0,1,
 1,0,1,0,0,0,0,0,1,1,
 1,0,0,0,1,1,1,0,0,1,
 1,1,1,1,1,1,1,1,1,1};

int find(int x,int y){//入口
 if(x==1&&y==1){ //出口
  maze[x][y]=2;
  return 1;
 }else{
  if(maze[x][y]==0){
   maze[x][y]=2;  //表示走过
   if(( find(x-1,y)
    +find(x+1,y)
    +find(x,y+1)
    +find(x,y-1))>0)
    return 1;
   }else{
    maze[x][y]=0;
    return 0;
   }
  }else{
    return 0;
  }
 }
}

void main(){
 int i,j;
 find(2,5); //入口
}


献花 x0
引用 | 编辑 teexit
2005-11-30 21:22
7楼
  
呵呵
大家都会在这边提供OTZ
一说到迷宫
我们最近也要写
听说教到POINT的时候还要我们写RPG的迷宫
听说还要打怪物
XD

献花 x0
引用 | 编辑 湛蓝威尼斯
2005-12-09 22:44
8楼
  
还要打怪物
好难唷~!! 表情

献花 x0
引用 | 编辑 湛蓝威尼斯
2005-12-13 02:34
9楼
  
下面是引用唐老鸭于2005-11-16 11:09发表的 :
刚花了点时间帮你把自动产生迷宫的程式写出来了 表情 .....
33*33的迷宫应该够你用了吧....
至于走的方法请你自己参考程式区有你要的写法....
下面我帮你加了些注解...
但也许对你会有些复杂....
.......

唐老鸭大大~ 可以跟我讲 0x1 <== 这是? 是随便给的初始值吗~?
|= <== 这符号在您的程式码中的意义
我查到这 |= 符号的意义是 执行运算子之后设定
direction = rand()%4+1; // 决定下一个位置
rand()%4+1; <= 这大概低意思~
如果唐老鸭大大很忙低话~
有哪位大大可以告诉我呢~
拜托拜托~

献花 x0
引用 | 编辑 唐老鸭
2005-12-14 04:14
10楼
  
下面是引用湛蓝威尼斯于2005-12-13 02:34发表的 :


唐老鸭大大~ 可以跟我讲 0x1 <== 这是? 是随便给的初始值吗~?
|= <== 这符号在您的程式码中的意义
我查到这 |= 符号的意义是 执行运算子之后设定
.......

0代表内墙....1代表内部通道....2代表外墙....
上面的程式只有用到上墙及右墙来产生所有墙面....
也就是假设你向右走....
便拆掉前一个位置的右墙...
向上同理....

0x1代表16进制的1....
其实程式只是把原来的值做位元运算而已....
像maze[x][y] |= 0x1;
用上面的程式其实就是等于 maze[x][y] = 1;...
这样写看起来其实没什么意义....
但是如果你的程式是视窗程式的话...
用位元运算的写法可以节省记忆体空间...
当然程式逻辑跟上面会有一点点不同....

rand()%4+1;
就是随机取1到4的值....
来决定要往哪边走阿....
这样才能产生不同的迷宫啰....

献花 x0
引用 | 编辑 唐老鸭
2005-12-14 04:25
11楼
  
补充....
上面我写的程式其实有点小BUG 表情 ....
用这里的自动走迷宫的程式也许会有问题....
虽然看程式的执行结果是看不出来....

原想让你自己发现自己改的....
不过你好像看不太懂...
所以好人做到底好了 表情 ....

复制程式
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#define row 35
#define column 35

int maze[row][column];

void generatemaze(int x,int y)
{
       int direction;
       maze[x][y] |= 0x1;  // 标示此格已设定
    
       while (maze[x][y+2]==0 || maze[x+2][y]==0 || maze[x][y-2]==0 || maze[x-2][y]==0) // 如果不是外墙
    {
         direction = rand()%4+1;  // 决定下一个位置
         
               if (direction==1 && maze[x][y+2]==0) // 向右走
               {
                      maze[x][y+1] |= 0x1; // 拆掉右墙
                      generatemaze(x,y+2);
         }
         else if (direction==2 && maze[x-2][y]==0) // 向上走
               {
                      maze[x-1][y] |= 0x1; // 拆掉上墙
                      generatemaze(x-2,y);
         }
         else if (direction==3 && maze[x][y-2]==0) // 向左走
               {
                      maze[x][y-1] |= 0x1; // 拆掉右墙
                      generatemaze(x,y-2); 
         }
         else if (direction==4 && maze[x+2][y]==0) // 向下走
               {
                      maze[x+1][y] |= 0x1; // 拆掉上墙
                      generatemaze(x+2,y); 
         }
    }      
}
void main()
{
       int Si=2,Sj=2,Ei=row-3,Ej=column-3;
       int x,y;
       
       srand(time(NULL));
       
       for (x=0;x<row;x++)
       {
              for (y=0;y<column;y++)
              {
                     if (x==0 || y==0 || x==1 || y==1 || x==row-2 || x==row-1 || y==column-2 || y==column-1) //改这里
                            maze[x][y]=2; // 设定外墙
            else
                            maze[x][y]=0;  // 初始迷宫内部
        }
    }
       
       generatemaze(Ei,Ej); // 产生迷宫
    maze[Si-1][Sj] = 0x1; // 拆掉入口上墙 //改这里
       maze[Ei][Ej+1] = 0x1; // 拆掉出口右墙 //改这里

       for(x=1; x<row-1; x++)
       {
              for(y=1; y<column-1; y++)
              {
                     if (maze[x][y] == 0 || maze[x][y] == 2)
                            cout<<"█"; 
            else
                            cout<<"  ";
              }
              cout<<endl;
       }
}

我直接贴上去他就乱了....
所以一样自己排版吧 表情 ....

献花 x0
引用 | 编辑 湛蓝威尼斯
2005-12-14 19:47
12楼
  
下面是引用唐老鸭于2005-12-14 04:25发表的 :
补充....
上面我写的程式其实有点小BUG 表情 ....
用这里的自动走迷宫的程式也许会有问题....
虽然看程式的执行结果是看不出来....

.......

唐老大~ >"< 对不起~
我资质驽钝~
我有改一些加走路的程式码~
可是跑不出来~ 可以帮我 debug 一下吗~
>"< 可以指证一下我的错误~ 哪边错吗??
拜托了~!!! 天呀~~ 学不好~
以下是我的修改过后~
复制程式
#include <iostream>
#define row 35
#define column 35
using namespace std;
int maze[row][column];
struct link                                     //链结串列 
{
  int x,y;
  struct link *next;
};
typedef struct link *list;                      //定义Link的指标叫做list
list path=NULL;
void print();                               //印出来 
void movemouse (int,int);                       //走 
list push (list stack , int x ,int y );
void pop (list &stack ,int &x ,int &y );
void generatemaze(int x,int y)
{
 int direction;
 maze[x][y] |= 0x1;  // 标示此格已设定
 while (maze[x][y+2]==0 || maze[x+2][y]==0 || maze[x][y-2]==0 || maze[x-2][y]==0) // 如果不是外墙
 {
  direction = rand()%4+1;  // 决定下一个位置
  if (direction==1 && maze[x][y+2]==0) // 向右走
  {
   maze[x][y+1] |= 0x1; // 拆掉右墙
   generatemaze(x,y+2);
   }
    else if (direction==2 && maze[x-2][y]==0) // 向上走
    {
     maze[x-1][y] |= 0x1; // 拆掉上墙
     generatemaze(x-2,y);
    }
     else if (direction==3 && maze[x][y-2]==0) // 向左走
     {
      maze[x][y-1] |= 0x1; // 拆掉右墙
      generatemaze(x,y-2); 
     }
      else if (direction==4 && maze[x+2][y]==0) // 向下走
      {
       maze[x+1][y] |= 0x1; // 拆掉下墙
       generatemaze(x+2,y); 
      }
   }      
}
int main()
{
 char option;
 do{
    int Si=2,Sj=2,Ei=row-3,Ej=column-3;
    int x,y;
    srand(time(NULL));
    for (x=0;x<row;x++)
    {
     for (y=0;y<column;y++)
     {
      if (x==0 || y==0 || x==1 || y==1 || x==row-2 || x==row-1 || y==column-2 || y==column-1) //改这里
      maze[x][y]=2; // 设定外墙
      else
      maze[x][y]=0;  // 初始迷宫内部
      }
    }
       
    generatemaze(Ei,Ej); // 产生迷宫
    maze[Si-1][Sj] = 0x1; // 拆掉入口上墙 //改这里
    maze[Ei][Ej+1] = 0x1; // 拆掉出口右墙 //改这里
    for(x=1; x<row-1; x++)
    {
     for(y=1; y<column-1; y++)
     {
      if (maze[x][y] == 0 || maze[x][y] == 2)
      cout<<"█"; 
      else
      cout<<"  ";
      }
      cout<<endl;
     }
     cout<<"重新设定地图请按 C ,任意字输入就会开始"<<endl;
     cin>>option;
    }while(option=='c'||option=='C');
    movemouse(2,2);
    cout<<"找到出口啦~ ^^ ~"<<endl; 
 system("pause");      
 return 0;
}
void printmap ()
{
     system("cls");
     for(int x=0;x<row;x++)
     {
     for(int y=0;y<column;y++)
     {
         switch(maze[row][column])
         {
             case 0:
                      cout<<"  ";
                      break;
             case 1:
                      cout<<"■";
                      break;
             case 2:
                      cout<<"@";
                      break;
             case 3:
                      cout<<"X";
                      break;
         }
     }
     cout<<endl;
     } 
    
}      

void movemouse (int x,int y)            
{
     //Sleep(200);                                    
     printmap ();
     if(x!=33||y!=33)
     {
         if(maze[x-1][y]==0)                            //up
         {
             maze[x][y]=2;
             path=push (path ,  x , y );
             printmap ();
             x--;
             movemouse(x,y) ; 
         }
         else if( maze[x][y+1]==0 )                     //right
         {
             maze[x][y]=2;
             path=push (path ,  x , y );
             printmap ();
             y++;
             movemouse(x,y) ; 
         }
         else if(maze[x+1][y]==0)                      //down
         {
             maze[x][y]=2;
             path=push (path ,  x , y );
             printmap ();
             x++;
             movemouse(x,y) ; 
         }
         else if(maze[x][y-1]==0)                      //left
         {
             maze[x][y]=2;
             path=push (path ,  x , y );
             printmap ();
             y--;
             movemouse(x,y) ; 
         }  
         else
         {
             maze[x][y]=3;
             printmap ();
             pop(path,x,y); 
             maze[x][y]=2;
             printmap ();         
             movemouse(x,y);
         }
     }
     else
     {
         maze[33][33]=2;
         printmap ();
      
     }
}

list push(list stack,int x,int y)
{
     list node=new link;
     node->x=x;
     node->y=y;
     node->next=stack;
     stack=node;
     return stack;
}

void pop(list &stack,int &x,int &y)
{
     list top=stack;
     x=top->x;
     y=top->y;
     stack=stack->next;
     delete top;
}


献花 x1
引用 | 编辑 唐老鸭
2005-12-15 07:04
13楼
  
你少打了一行#include <time.h>
这样我VC就可以过了....
不过....你的程式完全不会走耶= =....
只会重画地图耶 表情 .....

先问几个问题喔....
老师要求的迷宫走法有规定用啥演算法....
BFS...DFS...回朔搜寻...etc....
又或者有规定要找出最短的走法吗....
看你用到堆叠....
我才会想问这些问题的= =....

献花 x1
引用 | 编辑 湛蓝威尼斯
2005-12-16 06:08
14楼
  
下面是引用唐老鸭于2005-12-15 07:04发表的 :
你少打了一行#include <time.h>
这样我VC就可以过了....
不过....你的程式完全不会走耶= =....
只会重画地图耶 表情 .....

.......

我已经自己 DEBUG 完搂~ 哈~
太感谢唐老鸭大大了~
老师没有规定啥~
只是要我们写出一个可以看到一个迷宫有东西在走~
因为是资料结构处理的课~ 刚好课本是写在堆叠的部份~!!
所以就用用看~ 可是推叠有点多~
我就用 LINK LIST 的方法下去模拟推叠写~
虽然 DEBUG 完可以跑~ 可是我是直接带数值下去跑~
而且是用一直印地图的方式下去跑~
所以才会一直印地图~~
应该有更好的写法~ 唐老鸭大大可以不用帮我重写一个啦~
太麻烦你了~ 你还有你的事要忙呢~ ^^ ~
哈~ 地图很漂亮~ 我正努力看地图写法~ 研究中~
作业也已经交搂~ 哈~ 只有一句话~
非常感谢~ ^^
下面是我弄完后成功可以跑低~
因为我用 DEV 写的~ 我已经可以跑~
唐老鸭大大可能要加一些标头档~

复制程式
#include <iostream>
#define row 35
#define column 35
using namespace std;
int maze[row][column];
struct link                                     
{
  int x,y;
  struct link *next;
};
typedef struct link *list;                      //定义Link的指标
list path=NULL;
void printmap();                               //印出来 
void movemouse (int,int);                       //走 
list push (list stack , int x ,int y );           //push
void pop (list &stack ,int &x ,int &y );         //pop
void generatemaze(int x,int y)
{
 int direction;
 maze[x][y] |= 0x1;  // 标示此格已设定
 while (maze[x][y+2]==0 || maze[x+2][y]==0 || maze[x][y-2]==0 || maze[x-2][y]==0) // 如果不是外墙
 {
  direction = rand()%4+1;  // 决定下一个位置
  if (direction==1 && maze[x][y+2]==0) // 向右走
  {
   maze[x][y+1] |= 0x1; // 拆掉右墙
   generatemaze(x,y+2);
   }
    else if (direction==2 && maze[x-2][y]==0) // 向上走
    {
     maze[x-1][y] |= 0x1; // 拆掉上墙
     generatemaze(x-2,y);
    }
     else if (direction==3 && maze[x][y-2]==0) // 向左走
     {
      maze[x][y-1] |= 0x1; // 拆掉右墙
      generatemaze(x,y-2); 
     }
      else if (direction==4 && maze[x+2][y]==0) // 向下走
      {
       maze[x+1][y] |= 0x1; // 拆掉下墙
       generatemaze(x+2,y); 
      }
   }      
}
int main()
{
 char option;
 do{
    int Si=2,Sj=2,Ei=row-3,Ej=column-3;
    int x,y;
    srand(time(NULL));
    for (x=0;x<row;x++)
    {
     for (y=0;y<column;y++)
     {
      if (x==0 || y==0 ||x==row-1|| y==column-1) 
      maze[x][y]=2; // 设定外墙
      else
      maze[x][y]=0;  // 初始迷宫内部
      }
    }
       
    generatemaze(Ei,Ej); // 产生迷宫
    maze[Si-1][Sj] = 0x1; // 拆掉入口上墙 
    maze[Ei][Ej+1] = 0x1; // 拆掉出口右墙 
     
    for(x=0; x<row; x++)
    {
     for(y=0; y<column; y++)
     {
              //cout<<maze[x][y];
     if (maze[x][y] == 0)
      {
      maze[x][y]=2;
      cout<<"█"; 
      }
      else if (maze[x][y]=1)
      {
      maze[x][y]=0;
      cout<<"  ";
      }
      }
      cout<<endl;
     }
     maze[0][2]=4;
     maze[32][34]=4;
     for(x=1; x<row-1; x++)
     {
     for(y=1; y<column-1; y++)
     {
     cout<<maze[x][y];
     }
     cout<<endl;
     }
     
     printmap();
     cout<<"重新设定地图请按 C ,任意字输入就会开始"<<endl;
    
     cin>>option;
    }while(option=='c'||option=='C');
    movemouse(1,2);
    cout<<"你终于找到出口啦~ ^^ ~"<<endl; 
 system("pause");      
 return 0;
}
void printmap ()
{
     system("cls");
     //Sleep(1000);
     for(int x=0;x<row;x++)
     {
     for(int y=0;y<column;y++)
     {
      
         switch(maze[x][y])
         {
             case 0:
                      cout<<"  ";
                      break;
             case 1:
                      cout<<"@";
                      break;
             case 2:
                      cout<<"■";
                      break;
             case 3:
                      cout<<"禁";
                      break;
             case 4:
                      cout<<"  ";
                      break;
         }
     }
     cout<<endl;
     } 
    
}      

void movemouse (int x,int y)            
{
     cout<<x<<","<<y<<endl;                                 
     printmap ();
     if(x!=32||y!=33)
     {
         if(maze[x+1][y]==0)                            //down
         {
             maze[x][y]=1;
             path=push (path ,  x , y );
             printmap ();
             x++;
             movemouse(x,y) ; 
         }
         else if( maze[x][y+1]==0 )                     //right
         {
             maze[x][y]=1;
             path=push (path ,  x , y );
             printmap ();
             y++;
             movemouse(x,y) ; 
         }
         else if(maze[x-1][y]==0)                      //up
         {
             maze[x][y]=1;
             path=push (path ,  x , y );
             printmap ();
             x--;
             movemouse(x,y) ; 
         }
         else if(maze[x][y-1]==0)                      //left
         {
             maze[x][y]=1;
             path=push (path ,  x , y );
             printmap ();
             y--;
             movemouse(x,y) ; 
         }  
         else
         {
             maze[x][y]=3;
             printmap ();
             pop(path,x,y); 
             maze[x][y]=1;
             printmap ();         
             movemouse(x,y);
         }
     }
     else
     {
         maze[32][33]=1;
         printmap ();
      
     }
}

list push(list stack,int x,int y)
{
     list node=new link;
     node->x=x;
     node->y=y;
     node->next=stack;
     stack=node;
     return stack;
}

void pop(list &stack,int &x,int &y)
{
     list top=stack;
     x=top->x;
     y=top->y;
     stack=stack->next;
     delete top;
}


献花 x0
引用 | 编辑 kioko
2009-09-10 16:54
15楼
  
我稍微改版了一下
这是你原先的版本 不过 太多读取的画面了
所以稍微修改了一点点
自动行走迷宫型态 ...

#include <iostream>
#define row 35
#define column 35
using namespace std;
int maze[row][column];
struct link                        
{
int x,y;
struct link *next;
};
typedef struct link *list;               //定义Link的指标
list path=NULL;
void printmap();                     //印出来
void movemouse (int,int);               //走
list push (list stack , int x ,int y );       //push
void pop (list &stack ,int &x ,int &y );       //pop
void generatemaze(int x,int y)
{
int direction;
maze[x][y] |= 0x1; // 标示此格已设定
while (maze[x][y+2]==0 || maze[x+2][y]==0 || maze[x][y-2]==0 || maze[x-2][y]==0) // 如果不是外墙
{
direction = rand()%4+1; // 决定下一个位置
if (direction==1 && maze[x][y+2]==0) // 向右走
{
  maze[x][y+1] |= 0x1; // 拆掉右墙
  generatemaze(x,y+2);
  }
  else if (direction==2 && maze[x-2][y]==0) // 向上走
  {
  maze[x-1][y] |= 0x1; // 拆掉上墙
  generatemaze(x-2,y);
  }
  else if (direction==3 && maze[x][y-2]==0) // 向左走
  {
    maze[x][y-1] |= 0x1; // 拆掉右墙
    generatemaze(x,y-2);
  }
    else if (direction==4 && maze[x+2][y]==0) // 向下走
    {
    maze[x+1][y] |= 0x1; // 拆掉下墙
    generatemaze(x+2,y);
    }
  }    
}
int main()
{
char option;
do{
  int Si=2,Sj=2,Ei=row-3,Ej=column-3;
  int x,y;
  srand(time(NULL));
  for (x=0;x<row;x++)
  {
  for (y=0;y<column;y++)
  {
    if (x==0 || y==0 ||x==row-1|| y==column-1)
    maze[x][y]=2; // 设定外墙
    else
    maze[x][y]=0; // 初始迷宫内部
    }
  }
   
  generatemaze(Ei,Ej); // 产生迷宫
  maze[Si-1][Sj] = 0x1; // 拆掉入口上墙
  maze[Ei][Ej+1] = 0x1; // 拆掉出口右墙
   
  for(x=0; x<row; x++)
  {
  for(y=0; y<column; y++)
  {
        //cout<<maze[x][y];
  if (maze[x][y] == 0)
    {
    maze[x][y]=2;
    cout<<"█";
    }
    else if (maze[x][y]=1)
    {
    maze[x][y]=0;
    cout<<" ";
    }
    }
    cout<<endl;
  }
  maze[0][2]=4;
  maze[32][34]=4;
 
  cout<<"重新设定地图请按 C ,任意字输入就会开始"<<endl;
 
  cin>>option;
  }while(option=='c'||option=='C');
  movemouse(1,2);
  cout<<"你终于找到出口啦~ ^^ ~"<<endl;
system("pause");    
return 0;
}
void printmap ()
{
  system("cls");
  //Sleep(1000);
  for(int x=0;x<row;x++)
  {
  for(int y=0;y<column;y++)
  {
   
      switch(maze[x][y])
      {
        case 0:
              cout<<" ";
              break;
        case 1:
              cout<<"@";
              break;
        case 2:
              cout<<"■";
              break;
        case 3:
              cout<<" ";
              break;
        case 4:
              cout<<" ";
              break;
      }
  }
  cout<<endl;
  }
 
}    

void movemouse (int x,int y)        
{
  cout<<x<<","<<y<<endl;                      
  printmap ();
  if(x!=32||y!=33)
  {
      if(maze[x+1][y]==0)                   //down
      {
        maze[x][y]=1;
        path=push (path , x , y );
        x++;
        movemouse(x,y) ;
      }
      else if( maze[x][y+1]==0 )               //right
      {
        maze[x][y]=1;
        path=push (path , x , y );
        y++;
        movemouse(x,y) ;
      }
      else if(maze[x-1][y]==0)               //up
      {
        maze[x][y]=1;
        path=push (path , x , y );
        x--;
        movemouse(x,y) ;
      }
      else if(maze[x][y-1]==0)               //left
      {
        maze[x][y]=1;
        path=push (path , x , y );
        y--;
        movemouse(x,y) ;
      }  
      else
      {
        maze[x][y]=3;
        printmap ();
        pop(path,x,y);
        maze[x][y]=1;      
        movemouse(x,y);
      }
  }
  else
  {
      maze[32][33]=1;
      printmap ();
   
  }
}

list push(list stack,int x,int y)
{
  list node=new link;
  node->x=x;
  node->y=y;
  node->next=stack;
  stack=node;
  return stack;
}

void pop(list &stack,int &x,int &y)
{
  list top=stack;
  x=top->x;
  y=top->y;
  stack=stack->next;
  delete top;
}

献花 x0
引用 | 编辑 kioko
2009-09-10 16:57
16楼
  
另一种版本
手动行走迷宫型态 ...

#include <iostream>
#define row 35
#define column 35
using namespace std;
int maze[row][column];
struct link                        
{
int x,y;
struct link *next;
};
typedef struct link *list;               // 定义Link的指标
list path=NULL;
void printmap();                     // 印出来
void movemouse (int,int);               // 走
list push (list stack, int x, int y );       // push
void pop (list &stack,int &x,int &y );       // pop

void generatemaze(int x,int y){
     int direction;
     maze[x][y] |= 0x1;                              // 标示此格已设定
     while (maze[x][y+2]==0 || maze[x+2][y]==0 || maze[x][y-2]==0 || maze[x-2][y]==0){ // 如果不是外墙      
           direction = rand()%4+1;                        // 决定下一个位置
           if (direction==1 && maze[x][y+2]==0){   // 向右走
                 maze[x][y+1] |= 0x1;                        // 拆掉右墙
                 generatemaze(x,y+2);
           }
           else if (direction==2 && maze[x-2][y]==0){ // 向上走
                 maze[x-1][y] |= 0x1;                        // 拆掉上墙
                 generatemaze(x-2,y);
           }
           else if (direction==3 && maze[x][y-2]==0){ // 向左走
                 maze[x][y-1] |= 0x1;                        // 拆掉右墙
                 generatemaze(x,y-2);
           }
           else if (direction==4 && maze[x+2][y]==0){ // 向下走
                 maze[x+1][y] |= 0x1;                        // 拆掉下墙
                 generatemaze(x+2,y);
           }
     }    
}

main(){
  char option;
     do{
           int Si=2,Sj=2,Ei=row-3,Ej=column-3;
           int x,y;
           srand(time(NULL));
           for (x=0;x<row;x++){
                 for (y=0;y<column;y++){
                       if (x==0 || y==0 ||x==row-1|| y==column-1)
                             maze[x][y]=2;                        // 设定外墙
                       else
                             maze[x][y]=0;                        // 初始迷宫内部
                 }
           }
           generatemaze(Ei,Ej);                                // 产生迷宫
           maze[Si-1][Sj] = 0x1;                                // 拆掉入口上墙
           maze[Ei][Ej+1] = 0x1;                            // 拆掉出口右墙
           system("cls");
           for(x=0; x<row; x++){
                 for(y=0; y<column; y++){
                       if (maze[x][y] == 0){
                             maze[x][y]=2;
                             cout<<"█";
                       }
                       else if (maze[x][y]=1){
                             maze[x][y]=0;
                             cout<<" ";
                       }
                 }
                 cout<<endl;
           }
           maze[0][2]=4;
           maze[32][34]=4;
           cout<<"重新设定地图请按 C ,任意字输入就会开始"<<endl;
           cout<<"游戏中时 按键Q重新开始 按键A往左 按键D往右 按键W往上 按键S往下 按键X返回"<<endl;
           cin>> option;
     }while(option=='c'||option=='C');
  movemouse(1,2);    
     return 0;
}

void printmap (){
  system("cls");
  for(int x=0;x<row;x++){
       for(int y=0;y<column;y++){
        switch(maze[x][y]){
          case 0:
            cout<<" ";
            break;
             case 1:
            cout<<"@";
            break;
             case 2:
            cout<<"■"; //
            break;
             case 3:
            cout<<" ";
                             maze[x][y]=0;
            break;
             case 4:
            cout<<"*";
            break;
           }
       }
       cout<<endl;
  }  
}    

void movemouse (int x,int y){
  cout<<x<<","<<y<<endl;                      
  printmap ();
  while(x!=32||y!=33){
           switch(getch()){
                 case'a':case'A':
                       if(maze[x][y-1]==0){               // left
                    maze[x][y]=1;
                    path=push(path ,x ,y );
                    y--;
                    movemouse(x,y) ;
                       }
                       break;
                 case'w':case'W':
                       if(maze[x-1][y]==0){               // up
                    maze[x][y]=1;
                    path=push(path ,x ,y );
                    x--;
                    movemouse(x,y) ;
                }
                       break;
                 case'd':case'D':
                       if( maze[x][y+1]==0 ){             // right
                    maze[x][y]=1;
                    path=push(path ,x ,y );
                    y++;
                    movemouse(x,y) ;
                }
                       break;
                 case's':case'S':
                       if(maze[x+1][y]==0){               // down
                    maze[x][y]=1;
                    path=push(path ,x ,y );
                    x++;
                    movemouse(x,y) ;
                }
                       break;
                 case'x':case'X':
                       maze[x][y]=3;
                       pop(path,x,y);      
                       movemouse(x,y);
                       break;
                 case'q':case'Q':
                       main();
                       break;      
                 default:
                       movemouse(x,y);
                       break;
           }
     }
     cout<<"你终于找到出口啦~ ^^ ~"<<endl;
     system("pause");
}

list push(list stack,int x,int y){
  list node=new link;
  node->x=x;
  node->y=y;
  node->next=stack;
  stack=node;
  return stack;
}

void pop(list &stack,int &x,int &y){
  list top=stack;
  x=top->x;
  y=top->y;
  stack=stack->next;
  delete top;
}

献花 x0
引用 | 编辑 Inndy
2010-02-04 21:47
17楼
  
下面是引用 rakish 于 2005-11-30 15:00 发表的 : 到引言文
书上的..刚好看到...
复制程式
int maze[7][10]={
 1,1,1,1,1,1,1,1,1,1,
 1,0,1,0,1,0,0,0,0,1,
.......

有BUG!!
搞了半天可读性好低...
花了好久的时间整理...
话说...DeBUG是好了...
可是这支程式是在做啥??
怎都看不懂?
复制程式
#include <iostream.h>

int maze[7][10]={
  1,1,1,1,1,1,1,1,1,1,
  1,0,1,0,1,0,0,0,0,1,
  1,0,1,0,1,0,1,1,0,1,
  1,0,1,0,1,1,1,0,0,1,
  1,0,1,0,0,0,0,0,1,1,
  1,0,0,0,1,1,1,0,0,1,
  1,1,1,1,1,1,1,1,1,1};

int find(int x,int y){//入口   A
   if(x==1&&y==1){ //出口  B
     maze[x][y]=2;
     return 1;
   }else{                 // B
      if(maze[x][y]==0){  // C
         maze[x][y]=2;  //表示走过
            if((find(x-1,y)
               +find(x+1,y)
               +find(x,y+1)
               +find(x,y-1))>0){
               return 1;
            }else{
               maze[x][y]=0;
               return 0;
            }
      }else{
         return 0;
      }
   } //B
}    //A

int main(){
  int i,j;
  find(2,5); //入口
  system("pause");
}



献花 x0