Ejemplo n.º 1
0
 private void button1_Click(object sender, EventArgs e)
 {
     // 読み込み
     string[] lines = File.ReadAllLines("Maze.txt");
     for (int i = 0; i < lines.Length; i++) {
         xMax = xMax < lines[i].Length ? lines[i].Length : xMax;
     }
     yMax = lines.Length;
     maze = new string[xMax, yMax];
     mazeNum = new int[xMax, yMax];
     for (int y = 0; y < lines.Length; y++) {
         for (int x = 0; x < lines[y].Length; x++) {
             maze[x, y] = lines[y][x].ToString();
             if (maze[x, y] == "S") {
                 start = new Location(x, y);
                 mazeNum[x, y] = 1;
                 searchQueue.Enqueue(start);
             } else if (maze[x, y] == "G") {
                 goal = new Location(x, y);
                 mazeNum[x, y] = -1;
                 searchQueue.Enqueue(goal);
             }
         }
     }
     Print();
     textBox1.Text += "\r\n";
 }
Ejemplo n.º 2
0
        private void button2_Click(object sender, EventArgs e)
        {
            DateTime startTime = DateTime.Now;
            // 開始
            int[,] offset = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
            bool endFlag = false;
            Location item, r1, r2;
            r1 = null;
            r2 = null;
            int x, y, no;
            while (!endFlag) {
                if (searchQueue.Count == 0) {
                    textBox1.Text += "回答無し";
                    return;
                }
                item = searchQueue.Dequeue();
                no = mazeNum[item.x, item.y];
                no += no > 0 ? 1 : -1;
                for (int i = 0; i < offset.GetLength(0); i++) {
                    x = item.x + offset[i, 0];
                    y = item.y + offset[i, 1];
                    if (SearchCheck(x, y, no)) {
                        r1 = item;
                        r2 = new Location(x, y);
                        endFlag = true;
                        break;
                    }
                }
            }
            while (maze[r1.x, r1.y] == " ") {
                maze[r1.x, r1.y] = "$";
                RouteCheck(r1);
            }
            while (maze[r2.x, r2.y] == " ") {
                maze[r2.x, r2.y] = "$";
                RouteCheck(r2);
            }
            DateTime goalTime = DateTime.Now;

            Print();
            textBox1.Text += string.Format("{0:ss.fffffff} {1}", goalTime - startTime, (goalTime - startTime).Ticks);
        }
Ejemplo n.º 3
0
        private void RouteCheck(Location loc)
        {
            int x, y, no;
            no = mazeNum[loc.x, loc.y];
            no += no > 0 ? -1 : 1;

            int[,] offset = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
            for (int i = 0; i < offset.GetLength(0); i++) {
                x = loc.x + offset[i, 0];
                y = loc.y + offset[i, 1];
                if (0 <= x && x < xMax && 0 <= y && y < yMax) {
                    if (mazeNum[x, y] == no) {
                        loc.x = x;
                        loc.y = y;
                        break;
                    }
                }
            }
        }