創作內容

16 GP

C# 老鼠走迷宮

作者:貓貓風 ฅ●ω●ฅ│2018-12-08 21:49:43│巴幣:32│人氣:2749
.












這主題應該算是資料結構常常會出的經典問題

主要有兩種解法, 一是遞迴(recursive)  二是堆疊(Stack)

本篇採用Stack的寫法來實作老鼠走迷宮

原理非常簡單,將走過的路徑存到Stack中

如果遇到死路則將Stack中紀錄的路徑返還,然後繼續尋找其他沒有走過的路

重複上述方式直到找到終點

以下為實作程式碼,迷宮可自行繪製,不過迷宮尺寸也要隨著迷宮大小一併修改


  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Collections;  
  10. using System.Threading;  
  11.   
  12. namespace Maze  
  13. {  
  14.     public partial class Form1 : Form  
  15.     {  
  16.         private Label[] _label_wall = new Label[1000];  
  17.         private Label[] _label_path = new Label[1000];  
  18.         private Label[] _label_step = new Label[1000];  
  19.         private Label[] _label_obj = new Label[1];  
  20.         private Label[] _label_Exit = new Label[1];  
  21.         private int _label_wall_x = 30, _label_wall_y = 30, _label_path_x =
  22.         30,_label_path_y = 30,  
  23.         _label_obj_x = 90, _label_obj_y = 60;  
  24.         int _label_step_index = 0;  
  25.         Stack _st = new Stack();  
  26.         private Thread _th;  
  27.         private delegate void UpdateUI(parameter.status current, Control ctl);//宣告委派  
  28.         int pre_x;  
  29.         int pre_y;  
  30.         bool first = true;  
  31.         int[] _record = new int[1000];  
  32.         int index =1;  
  33.         int r_count = 0, l_count = 0, u_count = 0, d_count = 0, _pre_index = 0;  
  34.         bool _check = true;  
  35.         /*int[] _maze = {
  36.                 1,0,1,1,1,
  37.                 1,0,0,1,0,
  38.                 1,0,0,0,1,
  39.                 1,0,1,0,1,
  40.                 1,1,1,0,1,
  41.                 1,0,0,0,1,
  42.                 1,0,1,0,1,
  43.                 1,0,1,1,1
  44.             };*/  
  45.   
  46.         /*int[] _maze = {
  47.                 1,0,1,1,1,
  48.                 1,0,0,1,0,
  49.                 1,0,0,0,1,
  50.                 1,0,1,0,1,
  51.                 1,1,1,0,1,
  52.                 1,0,0,0,1,
  53.                 1,0,1,0,1,
  54.                 1,0,1,1,1,
  55.                 1,0,0,1,1,
  56.                 1,0,0,0,1,
  57.                 1,0,1,0,1,
  58.                 1,1,0,0,1,
  59.                 1,0,0,1,1,
  60.                 1,0,1,1,1
  61.             };*/  
  62.   
  63.         int _row = 12;   
  64.         /*int[] _maze = {
  65.                 1,0,1,1,1,1,1,
  66.                 1,0,0,1,0,0,1,
  67.                 1,0,0,0,0,0,1,
  68.                 1,0,1,0,0,0,1,
  69.                 1,0,1,0,0,0,1,
  70.                 1,0,0,0,0,1,1,
  71.                 1,0,1,0,0,1,1,
  72.                 1,0,1,1,0,1,1,
  73.                 1,0,1,1,0,1,1,
  74.                 1,0,1,0,1,0,1,
  75.                 1,0,0,0,0,0,1,
  76.                 1,1,0,1,1,0,1,
  77.                 1,0,0,0,0,0,1,
  78.                 1,0,1,1,1,1,1
  79.             };*/  
  80.   
  81.         /*int[] _maze = {
  82.                 1,0,1,1,1,1,1,
  83.                 1,0,0,1,0,0,1,
  84.                 1,0,0,0,0,0,1,
  85.                 1,1,1,0,1,0,1,
  86.                 1,0,1,0,1,0,1,
  87.                 1,1,0,0,0,1,1,
  88.                 1,0,0,0,0,1,1,
  89.                 1,0,1,1,1,1,1,
  90.                 1,0,0,0,0,1,1,
  91.                 1,0,1,1,1,0,1,
  92.                 1,0,0,0,0,0,1,
  93.                 1,1,1,1,1,0,1,
  94.                 1,0,0,0,0,0,1,
  95.                 1,0,1,1,1,1,1
  96.             };*/  
  97.   
  98.         /*int[] _maze = {
  99.                 1,0,1,1,1,1,1,1,1,1,
  100.                 1,0,0,0,0,1,0,0,0,1,
  101.                 1,0,1,0,0,0,1,0,0,1,
  102.                 1,0,1,0,1,0,1,0,0,1,
  103.                 1,0,0,0,1,0,1,0,0,1,
  104.                 1,0,1,0,0,1,0,0,1,1,
  105.                 1,0,0,1,0,0,0,0,0,1,
  106.                 1,0,1,0,1,1,1,1,0,1,
  107.                 1,0,0,0,0,1,0,0,0,1,
  108.                 1,0,1,0,1,1,0,0,1,1,
  109.                 1,0,1,1,0,0,0,0,0,1,
  110.                 1,0,0,0,1,1,0,0,0,1,
  111.                 1,0,1,0,0,0,1,0,0,1,
  112.                 1,1,1,0,1,1,1,1,1,1
  113.             };*/  
  114.   
  115.         int[] _maze = {  
  116.                 1,0,1,1,1,1,1,1,1,1,1,1,  
  117.                 1,0,1,0,0,1,1,1,1,0,1,1,  
  118.                 1,0,0,0,0,0,0,0,0,0,1,1,  
  119.                 1,1,1,0,1,0,1,1,1,1,0,1,  
  120.                 1,0,0,0,1,0,1,0,0,0,0,1,  
  121.                 1,0,1,0,0,1,0,0,1,1,0,1,  
  122.                 1,0,0,1,0,0,0,0,0,0,0,1,  
  123.                 1,0,1,0,1,1,1,1,1,0,0,1,  
  124.                 1,0,0,0,0,1,0,0,0,0,1,1,  
  125.                 1,0,1,0,1,1,1,0,1,0,0,1,  
  126.                 1,0,1,0,0,0,1,0,0,1,0,1,  
  127.                 1,0,1,1,0,1,1,1,0,0,1,1,  
  128.                 1,0,1,0,0,0,1,1,0,1,1,1,  
  129.                 1,1,1,0,1,1,1,1,1,1,1,1  
  130.             };  
  131.   
  132.         public Form1()  
  133.         {  
  134.             InitializeComponent();  
  135.         }  
  136.   
  137.         private void Form1_Load(object sender, EventArgs e)  
  138.         {  
  139.   
  140.             _label_obj[0] = new Label();  
  141.             _label_obj[0].AutoSize = false;  
  142.             _label_obj[0].Width = 20;  
  143.             _label_obj[0].Height = 5;  
  144.             _label_obj[0].Text = "";  
  145.             _label_obj[0].Font = new Font("Verdana", 12, FontStyle.Bold);  
  146.             _label_obj[0].ForeColor = Color.Orange;  
  147.             _label_obj[0].BackColor = Color.Lime;  
  148.             _label_obj[0].Location = new Point(_label_obj_x, _label_obj_y);  
  149.             this.Controls.Add(_label_obj[0]);  
  150.             _label_obj[0].BringToFront();  
  151.   
  152.             for (int i = 0; i < _maze.Length; i++)  
  153.             {  
  154.                 if (i % _row == 0)  
  155.                 {  
  156.                      _label_wall_x = 30;  
  157.                      _label_wall_y += 30;  
  158.                      _label_path_x = 30;  
  159.                      _label_path_y += 30;  
  160.                 }  
  161.   
  162.                 //產生牆壁  
  163.                 if (_maze[i] == 1)  
  164.                 {  
  165.                     _label_wall[i] = new Label();  
  166.                     _label_wall[i].AutoSize = false;  
  167.                     _label_wall[i].Width = 20;  
  168.                     _label_wall[i].Height = 5;  
  169.                     _label_wall[i].Text = "-";  
  170.                     _label_wall[i].Font = new Font("Verdana", 12, FontStyle.Bold);  
  171.                     //_temperture_txt[i].TextAlign = ContentAlignment.MiddleCenter;  
  172.                     _label_wall[i].ForeColor = Color.Orange;  
  173.                     _label_wall[i].BackColor = Color.Black;  
  174.                     _label_wall[i].Location = new Point(_label_wall_x, _label_wall_y);  
  175.                     this.Controls.Add(_label_wall[i]);  
  176.                     //_label_wall[i].BringToFront();  
  177.                     _label_wall_x += 60;  
  178.                     _label_path_x += 60;  
  179.                 }  
  180.   
  181.                 //產生通道  
  182.                 else if (_maze[i] == 0)  
  183.                 {  
  184.                     _label_path[i] = new Label();  
  185.                     _label_path[i].AutoSize = false;  
  186.                     _label_path[i].Width = 20;  
  187.                     _label_path[i].Height = 5;  
  188.                     _label_path[i].Text = "";  
  189.                     _label_path[i].Font = new Font("Verdana", 12, FontStyle.Bold);  
  190.                     _label_path[i].ForeColor = Color.Orange;  
  191.                     _label_path[i].BackColor = Color.Orange;  
  192.                     _label_path[i].Location = new Point(_label_path_x, _label_path_y);  
  193.                     this.Controls.Add(_label_path[i]);  
  194.                     //_label_wall[i].BringToFront();  
  195.                     _label_wall_x += 60;  
  196.                     _label_path_x += 60;  
  197.                 }  
  198.             }  
  199.             _th = new Thread(walking);  
  200.             _th.Start();  
  201.         }  
  202.   
  203.         private void walking()  
  204.         {  
  205.             //顯示初始座標  
  206.             updatePath(parameter.status.Running, _label_path[0]);  
  207.             updatePath(parameter.status.Running, _label_obj[0]);  
  208.             //定義初始移動位置與方向  
  209.             _pre_index = index;  
  210.             r_count = _pre_index += 1;  
  211.             _pre_index = index;  
  212.             d_count = _pre_index += _row;  
  213.             _pre_index = index;  
  214.             l_count = _pre_index -= 1;  
  215.             _pre_index = index;  
  216.             u_count = _pre_index -= _row;  
  217.               
  218.             while ((r_count < _maze.Length) && (d_count< _maze.Length) && (l_count<
  219.             _maze.Length) && (u_count< _maze.Length))  
  220.             {  
  221.                 if (_maze[r_count] == 0)  
  222.                 {  
  223.                     _check = true; //有路  
  224.   
  225.                     if (_check)  
  226.                     {  
  227.                         _st.Push(r_count);//在 stack 加進一格的座標  
  228.                     }  
  229.   
  230.                     _label_obj_x += 60; //向右走  
  231.                     _pre_index = r_count;  
  232.                     _maze[r_count] = 1; // 走過  
  233.                     updatePath(parameter.status.Running, _label_path[0]);  
  234.                     updatePath(parameter.status.Running, _label_obj[0]);  
  235.                 }  
  236.   
  237.                 else if (_maze[d_count] == 0)  
  238.                 {  
  239.                     _check = true; //有路  
  240.   
  241.                     if (_check)  
  242.                     {  
  243.                         _st.Push(d_count);//在 stack 加進一格的座標  
  244.                     }  
  245.   
  246.                     _label_obj_y += 30; //向下走  
  247.                     _pre_index = d_count;  
  248.                     _maze[d_count] = 1; // 走過  
  249.                     updatePath(parameter.status.Running, _label_path[0]);  
  250.                     updatePath(parameter.status.Running, _label_obj[0]);  
  251.                 }  
  252.   
  253.                 else if (_maze[l_count] == 0)  
  254.                 {  
  255.                     _check = true; //有路  
  256.                     if(_check)  
  257.                     {  
  258.                         _st.Push(l_count);//在 stack 加進一格的座標  
  259.                     }  
  260.                     _label_obj_x -= 60; //向左走  
  261.                     _pre_index = l_count;  
  262.                     _maze[l_count] = 1; // 走過  
  263.                     updatePath(parameter.status.Running, _label_path[0]);  
  264.                     updatePath(parameter.status.Running, _label_obj[0]);                   
  265.                 }  
  266.                 else if (_maze[u_count] == 0)  
  267.                 {  
  268.                     _check = true; //有路  
  269.   
  270.                     if(_check)  
  271.                     {  
  272.                         _st.Push(u_count);//在 stack 加進一格的座標  
  273.                     }  
  274.   
  275.                     _label_obj_y -= 30; //向上走  
  276.                     _pre_index = u_count;  
  277.                     _maze[u_count] = 1; // 走過  
  278.                     updatePath(parameter.status.Running, _label_path[0]);  
  279.                     updatePath(parameter.status.Running, _label_obj[0]);              
  280.                 }     
  281.                 else  
  282.                 {  
  283.                     _check = false;  
  284.                     index = (int)_st.Peek();//三面都圍牆時 將當前位置設定為牆  
  285.                     _maze[index] = 1; // 走過  
  286.                     _st.Pop();//在 stack 移除後退的格子的座標,退到前一步  
  287.                     index = (int)_st.Peek();  
  288.                     _maze[index] = 0;  
  289.   
  290.                     if(_maze[index] == 0) //更新移動路徑  
  291.                     {  
  292.                         updatePath(parameter.status.Running, _label_path[0]);  
  293.                         updatePath(parameter.status.Running, _label_obj[0]);  
  294.                     }  
  295.                 }  
  296.   
  297.                 //更新往四個方向座標  
  298.                 r_count = _pre_index + 1;  
  299.                 d_count = _pre_index + _row;  
  300.                 l_count = _pre_index - 1;  
  301.                 u_count = _pre_index - _row;  
  302.                 Thread.Sleep(50);  
  303.             }  
  304.   
  305.             updatePath(parameter.status.Exit, _label_obj[0]);  
  306.             MessageBox.Show("Find Exit");  
  307.   
  308.         }  
  309.   
  310.         private void updatePath(parameter.status current, Control ctl)  
  311.         {  
  312.             if (this.InvokeRequired)  
  313.             {  
  314.                 UpdateUI uu = new UpdateUI(updatePath);  
  315.                 this.Invoke(uu, current, ctl);  
  316.             }  
  317.             else  
  318.             {  
  319.                 if (current == parameter.status.Exit)  
  320.                 {  
  321.                     _label_Exit[0] = new Label();  
  322.                     _label_Exit[0].AutoSize = false;  
  323.                     _label_Exit[0].Width = 20;  
  324.                     _label_Exit[0].Height = 5;  
  325.                     _label_Exit[0].Text = "";  
  326.                     _label_Exit[0].Font = new Font("Verdana", 12, FontStyle.Bold);  
  327.                     _label_Exit[0].ForeColor = Color.Orange;  
  328.                     _label_Exit[0].BackColor = Color.Red;  
  329.                     _label_Exit[0].Location = new Point(_label_obj_x, _label_obj_y);  
  330.                     this.Controls.Add(_label_Exit[0]);  
  331.                     _label_Exit[0].BringToFront();  
  332.                 }  
  333.                 else  
  334.                 {  
  335.                     if(!first)  
  336.                     {  
  337.                         _label_step[_label_step_index] = new Label();  
  338.                         _label_step[_label_step_index].AutoSize = false;  
  339.                         _label_step[_label_step_index].Width = 20;  
  340.                         _label_step[_label_step_index].Height = 5;  
  341.                         _label_step[_label_step_index].Text = "";  
  342.                         _label_step[_label_step_index].Font = new Font("Verdana", 12,
  343.                          FontStyle.Bold);  
  344.                         _label_step[_label_step_index].ForeColor = Color.Orange;  
  345.                         _label_step[_label_step_index].BackColor = Color.Blue;  
  346.                         _label_step[_label_step_index].Location = new Point(pre_x, pre_y);  
  347.                         this.Controls.Add(_label_step[_label_step_index]);  
  348.                         _label_step[_label_step_index].BringToFront();  
  349.   
  350.                     }  
  351.                     //ctl.Text = str;  
  352.                     _label_obj[0].Location = new Point(_label_obj_x, _label_obj_y);  
  353.                     this.Controls.Add(_label_obj[0]);  
  354.                     _label_obj[0].BringToFront();  
  355.                     pre_x = _label_obj_x;  
  356.                     pre_y = _label_obj_y;  
  357.                     first = false;  
  358.                 }     
  359.             }  
  360.         }  
  361.     }  
  362. }  

以下為實際執行結果

引用網址:https://home.gamer.com.tw/TrackBack.php?sn=4220099
All rights reserved. 版權所有,保留一切權利

相關創作

同標籤作品搜尋:涼涼風|C#

留言共 9 篇留言

奇米 .輕
媽呀 好難啊看不懂

12-08 22:18

貓貓風 ฅ●ω●ฅ
其實就是走迷宮XD12-08 22:24
小魚
心得1:
這迷宮也太醜了 XD

心得2:
感謝提供做法~

12-08 23:33

貓貓風 ฅ●ω●ฅ
可以自己做過XD12-08 23:53
小魚
怪了,怎麼跑版了...

12-08 23:56

貓貓風 ฅ●ω●ฅ
有東西沒複製到吧 或是解析度問題12-08 23:58
小魚
不是啦, 我說我的 心得1 跟 心得2 跑版了

12-09 00:08

貓貓風 ฅ●ω●ฅ
電腦版看很正常12-09 00:15
小魚
我就是用電腦版看得啊,
原本 心得 跟 1 是同一行,
被換行了 XD

12-09 00:18

小魚
啊啊,
我知道了,
原來是Google翻譯惹的禍 XD

12-09 00:19

小魚
Google翻譯真的常常讓人誤會,
尤其是原本就是打中文的.

12-09 00:20

Fuwawa
老鼠走迷宮 我的大腦現在也跟著走了

12-09 20:56

貓貓風 ฅ●ω●ฅ
有這麼誇張嗎 XD12-09 21:09
小刀
厲害!

12-10 14:24

貓貓風 ฅ●ω●ฅ
[e12]12-11 01:10
我要留言提醒:您尚未登入,請先登入再留言

16喜歡★s1234567 可決定是否刪除您的留言,請勿發表違反站規文字。

前一篇:C# DataGridV... 後一篇:C# 迷宮產生器 - 使...

追蹤私訊切換新版閱覽

作品資料夾

chocomint路人
UTAU音源配布~柊雪誕生四周年快樂~!賀新網站落成ヾ(≧▽≦*)o(✿◡‿◡)/看更多我要大聲說10小時前


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】