跳到主文
部落格全站分類:心情日記
因為之前去考試有考到 maze 相關的精典考題,所以我回來就順道把他給弄成一個簡單的程式碼(笑~
程式是完全靠堆疊來弄起來(但我只是用迴圈,而非使用遞迴)
不過我是想過,把每點都丟入堆疊一次改成當有叉路時,在入口才推進堆疊一次,而只能直直前進的就設成用迴圈
不過寫完後就突然懶了起來(笑~
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Maze : Form { private List<int[]> map = new List<int[]>(); private Point startPoint = new Point(); private Point endPoint = new Point(); public Maze() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { button2.Enabled = false; var f = System.IO.File.OpenText(openFileDialog1.FileName); string s; int len = -1; bool HaveStart = false; bool HaveEnd = false; map.Clear(); while (!f.EndOfStream) { s = f.ReadLine(); if (len == -1) len = s.Length; else if (len != s.Length) { MessageBox.Show("錯誤!地圖需為矩陣(n*m)才能進行走迷宮!"); return; } else if (!System.Text.RegularExpressions.Regex.IsMatch(s, @"^[0123]+$")) { MessageBox.Show("錯誤!迷宮檔只能有 0 (通道) 、 1 (牆壁) 、 2 (起點) 和 3(終點)"); return; } int[] a = new int[len]; for (int i = 0; i < len; i++) { a[i] = Convert.ToInt32(s.Substring(i, 1)); if (a[i] == 2 && HaveStart) { MessageBox.Show("起點只能有 1 個"); return; } else if (a[i] == 2) { HaveStart = true; startPoint.X = map.Count; startPoint.Y = i; } if (a[i] == 2 && HaveEnd) { MessageBox.Show("終點只能有 1 個"); return; } else if (a[i] == 2) { HaveEnd = true; endPoint.X = map.Count; endPoint.Y = i; } } map.Add(a); } f.Close(); button2.Enabled = true; } } private void button2_Click(object sender, EventArgs e) { listBox1.Items.Clear(); listBox1.Items.Add("小老鼠,上燈檯,偷吃油,下不來,喵喵喵,貓來了,嘰哩咕嚕滾下來!"); Traverse(startPoint.X, startPoint.Y); } private void Traverse(int x, int y) { //Map value: // 0:通道 // 1:牆壁 // 2:起點 // 3:終點 // 4:已走過 Stack<Point> p = new Stack<Point>(); int val, val_n, val_e, val_w, val_s; Point _p; p.Push(new Point(x, y)); while (p.Count > 0) { _p = p.Pop(); val = map[_p.X][_p.Y]; val_n = _p.X > 0 ? map[_p.X - 1][_p.Y] : 1; val_e = _p.Y < map[0].Count() - 1 ? map[_p.X][_p.Y + 1] : 1; val_w = _p.Y > 0 ? map[_p.X][_p.Y - 1] : 1; val_s = _p.X < map.Count - 1 ? map[_p.X + 1][_p.Y] : 1; //判斷目前所在的座標點 if (val == 3) { listBox1.Items.Add(string.Format("({0},{1})({2})老鼠終於找到終點了,可喜可賀!", ReturnNumStr(_p.X,2), ReturnNumStr(_p.Y,2), ReturnNumStr(listBox1.Items.Count + 1,2))); return; } else { listBox1.Items.Add(string.Format("({0},{1})({2})老鼠還在找路中...!", ReturnNumStr(_p.X,2), ReturnNumStr(_p.Y,2), ReturnNumStr(listBox1.Items.Count + 1,2))); map[_p.X][_p.Y] = 4; } int[] findVal = { 0, 3 }; if(findVal.Count(a => a== val_n) > 0) p.Push(new Point(_p.X - 1, _p.Y)); if (findVal.Count(a => a == val_e) > 0) p.Push(new Point(_p.X, _p.Y + 1)); if (findVal.Count(a => a == val_w) > 0) p.Push(new Point(_p.X, _p.Y - 1)); if (findVal.Count(a => a == val_s) > 0) p.Push(new Point(_p.X + 1, _p.Y)); } listBox1.Items.Add("老兄...老鼠找不到出路耶,你的地圖沒解呦!"); } private string ReturnNumStr(int num, int digit) { string FullStrNum = "0123456789"; StringBuilder numstr = new StringBuilder(num.ToString()); for (int i = 0; i < 10; i++) { numstr.Replace(i.ToString(), FullStrNum.Substring(i, 1)); } for(int i = numstr.Length; i<=digit; i++) numstr.Insert(0, " "); return numstr.ToString(); } } }
寫法不是很好,不過其實最重要要能先弄出結果呀!
天翼翔幻
MyWCT 發表在 痞客邦 留言(0) 人氣()
留言列表