| 
                
                  | 唐老鴨 
         
  
 | 分享:        ▼         
 
 
   下面是引用湛藍威尼斯於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的值.... 來決定要往哪邊走阿.... 這樣才能產生不同的迷宮囉.... 
 
 |  
                  |  |  
                
                  | 唐老鴨 
         
  
 | 分享:        ▲
                    
                      ▼         
 
   補充.... 上面我寫的程式其實有點小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;
       }
}
 我直接貼上去他就亂了.... 所以一樣自己排版吧    ....
 
 [ 此文章被唐老鴨在2005-12-14 04:38重新編輯 ] 
 |  
                  |  |  
                
                  | 湛藍威尼斯 
         
  
 | 分享:        ▲
                    
                      ▼         
 
   下面是引用唐老鴨於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  [12 樓]
                    
                    
                     From:台灣教育部
 |  Posted:2005-12-14 19:47 | |  |  
                
                  | 唐老鴨 
         
  
 | 分享:        ▲
                    
                      ▼         
 
   你少打了一行#include <time.h> 這樣我VC就可以過了.... 不過....你的程式完全不會走耶= =.... 只會重畫地圖耶    ..... 先問幾個問題喔.... 老師要求的迷宮走法有規定用啥演算法.... BFS...DFS...回朔搜尋...etc.... 又或者有規定要找出最短的走法嗎.... 看你用到堆疊.... 我才會想問這些問題的= =....
 |  
                  |  |  
                
                  | 湛藍威尼斯 
         
  
 | 分享:        ▲
                    
                      ▼         
 
   下面是引用唐老鴨於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  [14 樓]
                    
                    
                     From:台灣中華電信
 |  Posted:2005-12-16 06:08 | |  |  
                
                  | kioko 
         
  
 | 分享:        ▲
                    
                      ▼         
 
   我稍微改版了一下這是你原先的版本 不過 太多讀取的畫面了
 所以稍微修改了一點點
 自動行走迷宮型態 ...
 
 #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  [15 樓]
                    
                    
                     From:APNIC |  Posted:2009-09-10 16:54 | |  |  
                
                  | kioko 
         
  
 | 分享:        ▲
                    
                      ▼         
 
   另一種版本手動行走迷宮型態 ...
 
 #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  [16 樓]
                    
                    
                     From:APNIC |  Posted:2009-09-10 16:57 | |  |  
                
                  | Inndy 
         
  
 | 分享:        ▲         
 
   下面是引用 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  [17 樓]
                    
                    
                     From:台灣中華電信 |  Posted:2010-02-04 21:47 | |  |  |