using System; using System.Collections.Generic; using System.Text; namespace 迷宫问题_DFS { class Program { struct Point { public int X; public int Y; } struct MapState { public int[,] Map; public Point Current; public bool Flag; } const int WIDTH=10; const int HEIGHT=10; static MapState[] list = new MapState[100000]; static int[,] box = new int[WIDTH, HEIGHT] { {1,1,1,1,1,1,1,1,1,1}, {1,0,1,1,1,0,0,0,1,1}, {1,0,0,1,1,0,1,0,1,1}, {1,0,0,0,0,0,1,0,1,1}, {1,1,0,1,0,1,1,0,1,1}, {1,1,0,1,0,1,1,0,1,1}, {1,0,0,0,1,1,1,0,1,1}, {1,1,0,0,0,0,0,0,1,1}, {1,1,1,1,1,1,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1} }; static void Main(string[] args) { Console.WriteLine("原来的样子:"); for(int i=0;i<WIDTH;i++) {
for (int y = 0; y < HEIGHT; y++) { if (box[i, y] == 1) { Console.ForegroundColor = ConsoleColor.Green ; Console.Write(" "+ box[i, y].ToString ()); } else if (box[i, y] == 0) { Console.ForegroundColor = ConsoleColor.White ; Console.Write(" "+ box[i, y].ToString ()); } else if (box[i, y] ==2) { Console.ForegroundColor = ConsoleColor.Red ; Console.Write(" "+box[i, y].ToString()); } } Console.WriteLine(); } Walk(); Console.WriteLine("走出的样子:"); for (int i = 0; i < WIDTH; i++) { for (int y = 0; y < HEIGHT; y++) { if (list[Resualt1].Map[i, y] == 1 && list[Resualt2].Map[i, y] == 1) { Console.ForegroundColor = ConsoleColor.Green; Console.Write(" 1"); } else if (list[Resualt1].Map[i, y] == 0 && list[Resualt2].Map[i, y] ==0) { Console.ForegroundColor = ConsoleColor.White; Console.Write(" 0"); } else if (list[Resualt1].Map[i, y] == 2 || list[Resualt2].Map[i, y] == 2) { Console.ForegroundColor = ConsoleColor.Red; if (list[Resualt1].Map[i, y] == 2) { Console.Write(" " + list[Resualt1].Map[i, y].ToString()); continue; } if (list[Resualt2].Map[i, y] == 2) { Console.Write(" " + list[Resualt2].Map[i, y].ToString()); continue; } } } Console.WriteLine(); } Console.Read(); } static void Print(int[,] box) { for (int i = 0; i < WIDTH; i++) { Console.WriteLine(); for (int y = 0; y < HEIGHT; y++) { if (box[i, y] == 1) { Console.ForegroundColor = ConsoleColor.Green; Console.Write(" " + box[i, y].ToString()); } else if (box[i, y] == 0) { Console.ForegroundColor = ConsoleColor.White; Console.Write(" " + box[i, y].ToString()); } else if (box[i, y] == 2) { Console.ForegroundColor = ConsoleColor.Red; Console.Write(" "+box[i, y].ToString()); } } } } static int Resualt1 = 0; static int Resualt2 = 0; static int f = 0; static int l = 1; static void Walk() { list[f] = CreateMapState(1,1,box,true); list[f + 1] = CreateMapState(8, 8, box,false); do { Move(list[f], 0, 1,list[f].Flag ); Move(list[f], 1, 0, list[f].Flag); Move(list[f], 0, -1, list[f].Flag); Move(list[f], -1, 0, list[f].Flag); f++; } while (CheckOut()); } static bool CheckOut() { for (int i =0 ; i < l; i++) { for (int k = i+1; k < l-1; k++) { if (list[i].Current.X == list[k].Current.X && list[i].Current.Y == list[k].Current.Y && list[i].Flag!=list[k].Flag ) { Resualt1 = i; Resualt2 = k; return false; } } } return true; } static void Move(MapState map,int x,int y,bool flag) { if (map.Map[map.Current.X + x, map.Current.Y + y] != 1 && map.Map[map.Current.X + x, map.Current.Y + y] != 2) { if (map.Current.X + x > 0 && map.Current.Y + y > 0) { l++; list[l] = CreateMapState(map.Current.X + x, map.Current.Y + y, map.Map, flag); } }
但是每走都要记下当前地图的状态 CreateMapState干的就是这个 Walk是开始走迷宫 CheckOut是检查出口和入口推出的路线有无重叠点 如果没有就继续推 如果有就EXIT···打出路线 Print是我调试用的。。没用…… struct MapState { public int[,] Map;地图的位置状态 public Point Current;但前走到哪个点 public bool Flag;是入口推出点还是出口推出的点 }
{
若栈不空且栈顶位置尚有其他方向未被探索, 则设定新的当前位置为: 沿顺时针方向旋转 找到的栈顶位置的下一相邻块; 若栈不空但栈顶位置的四周均不可通, 则{ 删去栈顶位置; // 从路径中删去该通道块 若栈不空,则重新测试新的栈顶位置, 直至找到一个可通的相邻块或出栈至栈空;}}while (栈不空)
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;namespace MazeSecond
{ public struct StateItem
{
public int dir;
public Point point;
public int sex;
}
public struct Point
{
public int x;
public int y;
}
class Program
{
static Stack UptoDownStack = new Stack();
static Stack DownToUpStack = new Stack();
static void Main(string[] args)
{
//初始化迷宫
int[,] MazeItem = new int[10, 10]
{
{1,1,1,1,1,1,1,1,1,1},
{1,0,1,1,1,0,0,0,1,1},
{1,0,0,1,1,0,1,0,1,1},
{1,0,0,0,0,0,1,0,1,1},
{1,1,0,1,0,1,1,0,1,1},
{1,1,0,1,0,1,1,0,1,1},
{1,0,0,0,1,1,1,0,1,1},
{1,1,0,0,0,0,0,0,1,1},
{1,1,1,1,1,1,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
//输出迷宫
for (int i = 0; i <= MazeItem.GetUpperBound(0); i++)
{
for (int j = 0; j <= MazeItem.GetUpperBound(1); j++)
{
Console.Write(MazeItem[i, j]);
}
Console.WriteLine();
}
//起点初始化
StateItem UptoDownItem = new StateItem();
UptoDownItem.dir = 1;
UptoDownItem.point = new Point();
UptoDownItem.point.x = 1;
UptoDownItem.point.y = 1;
UptoDownItem.sex = 0;
//终点初始化
StateItem DowntoUPItem = new StateItem();
DowntoUPItem.dir = 1;
DowntoUPItem.point = new Point();
DowntoUPItem.point.x = 8;
DowntoUPItem.point.y = 8;
DowntoUPItem.sex = 1; UptoDownStack.Push(UptoDownItem);
DownToUpStack.Push(DowntoUPItem);
do
{
StateItem UptoDown=ReturnStatItem(UptoDownItem);
StateItem DowntoUP = ReturnStatItem(DowntoUPItem);
UptoDownStack.Push(UptoDown);
DownToUpStack.Push(DowntoUP);
if (UptoDown.point.x == DowntoUP.point.x && UptoDown.point.y == DowntoUP.point.y)
{
break;
} } while (UptoDownStack.Count > 0); while(DownToUpStack.Count > 0)
{
UptoDownStack.Push(DownToUpStack.Pop());
} if (UptoDownStack.Count > 0)
{
while (UptoDownStack.Count > 0)
{
StateItem st = (StateItem)UptoDownStack.Pop();
Console.WriteLine(st.point.x + "," + st.point.y);
}
}
else
{
Console.WriteLine("无");
}
}
public static StateItem ReturnStatItem(StateItem stateItem)
{
//初始化迷宫
int[,] MazeItem = new int[10, 10]
{
{1,1,1,1,1,1,1,1,1,1},
{1,0,1,1,1,0,0,0,1,1},
{1,0,0,1,1,0,1,0,1,1},
{1,0,0,0,0,0,1,0,1,1},
{1,1,0,1,0,1,1,0,1,1},
{1,1,0,1,0,1,1,0,1,1},
{1,0,0,0,1,1,1,0,1,1},
{1,1,0,0,0,0,0,0,1,1},
{1,1,1,1,1,1,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
switch (stateItem.dir)
{
case 1:
stateItem.point.y = stateItem.point.y - 1;
break;
case 2:
stateItem.point.x = stateItem.point.x + 1;
break;
case 3:
stateItem.point.y = stateItem.point.y + 1;
break;
case 4:
stateItem.point.x = stateItem.point.x - 1;
break;
default :
break;
}
if (MazeItem[stateItem.point.x, stateItem.point.y] == 0)
{
return stateItem;
MazeItem[stateItem.point.x, stateItem.point.y] = 2;
stateItem.dir = 1;
}
else
{
if (stateItem.dir != 4)
{
stateItem.dir++;
switch (stateItem.dir)
{
case 1:
stateItem.point.y = stateItem.point.y + 1;
break;
case 2:
stateItem.point.x = stateItem.point.x - 1;
break;
case 3:
stateItem.point.y = stateItem.point.y - 1;
break;
default:
break;
}
return stateItem;
}
else
{
if (stateItem.sex == 0)
{
UptoDownStack.Pop();//第一次抛栈
stateItem = (StateItem)UptoDownStack.Pop();//第二次抛栈
if (stateItem.dir != 4)
{
stateItem.dir++;
}
return stateItem;
}
else
{
DownToUpStack.Pop();
stateItem = (StateItem)DownToUpStack.Pop();
if (stateItem.dir != 4)
{
stateItem.dir++;
}
return stateItem;
}
}
}
}
}
}
这是我想从起点和终点同时出发设计出来的,不过发现错了还蛮地方的,希望有人帮忙看.我这个设计太过繁琐了,希望会看到更简便的设计,看能不能用队列或是表来实现
/// 走迷宫
/// </summary>
/// <param name="maze">迷宫矩阵</param>
/// <param name="curr">当前位置</param>
/// <param name="dest">目标位置</param>
/// <returns>返回是否走到终点</returns>
private bool SpyMaze(ref int[,] maze, Point curr, Point dest)
{
if (!Rectangle.FromLTRB(0, 0, maze.GetLength(0),
maze.GetLength(1)).Contains(curr)) return false; // 走到边界
if (maze[curr.X, curr.Y] != 0) return false; // 墙或者是已经走过
maze[curr.X, curr.Y] = 2; // 标记已经走过
if (curr == dest) return true; // 走到目的地
Point[] offset = {
new Point(00, -1), new Point(+1, 00),
new Point(00, +1), new Point(-1, 00)
};
bool result = false;
for (int i = 0; !result && i < offset.Length; i++)
{
Point move = curr;
move.Offset(offset[i]);
result = SpyMaze(ref maze, move, dest);
}
if (!result) maze[curr.X, curr.Y] = 0; // 还原
return result;
}private void button1_Click(object sender,EventArgs e)
{
int[,] maze = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0},
{1,0,1,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,0,1,1,1,0},
{1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,0},
{1,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,0,1,0,1,0,1,0},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,0,1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,1,0},
{1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0},
{1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0},
{1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,0,1,0,1,0,1,1,1,0,1,0,1,0,1,0},
{1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,1,0},
{1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,0,1,0},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
};
SpyMaze(ref maze, new Point(0, 0),
new Point(maze.GetLength(0) - 1, maze.GetLength(1) - 1));
for (int i = 0; i < maze.GetLength(0); i++)
{
for (int j = 0; j < maze.GetLength(1); j++)
{
if (maze[i, j] == 2)
Console.Write("{0}", maze[i, j]);
else Console.Write(maze[i, j]);
}
Console.WriteLine();
}
}
双向搜索(入口和出口一起推)····
这个样子最快······或者递归(穷举每一条分支)。。前提是数据量不大
using System;
using System.Collections.Generic;
using System.Text;
namespace 迷宫问题_DFS
{
class Program
{
struct Point
{
public int X;
public int Y;
}
struct MapState
{
public int[,] Map;
public Point Current;
public bool Flag;
}
const int WIDTH=10;
const int HEIGHT=10;
static MapState[] list = new MapState[100000];
static int[,] box = new int[WIDTH, HEIGHT]
{
{1,1,1,1,1,1,1,1,1,1},
{1,0,1,1,1,0,0,0,1,1},
{1,0,0,1,1,0,1,0,1,1},
{1,0,0,0,0,0,1,0,1,1},
{1,1,0,1,0,1,1,0,1,1},
{1,1,0,1,0,1,1,0,1,1},
{1,0,0,0,1,1,1,0,1,1},
{1,1,0,0,0,0,0,0,1,1},
{1,1,1,1,1,1,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
static void Main(string[] args)
{
Console.WriteLine("原来的样子:");
for(int i=0;i<WIDTH;i++)
{
for (int y = 0; y < HEIGHT; y++)
{
if (box[i, y] == 1)
{
Console.ForegroundColor = ConsoleColor.Green ;
Console.Write(" "+ box[i, y].ToString ()); }
else if (box[i, y] == 0)
{
Console.ForegroundColor = ConsoleColor.White ;
Console.Write(" "+ box[i, y].ToString ()); }
else if (box[i, y] ==2)
{
Console.ForegroundColor = ConsoleColor.Red ;
Console.Write(" "+box[i, y].ToString());
}
}
Console.WriteLine();
}
Walk();
Console.WriteLine("走出的样子:");
for (int i = 0; i < WIDTH; i++)
{
for (int y = 0; y < HEIGHT; y++)
{
if (list[Resualt1].Map[i, y] == 1 && list[Resualt2].Map[i, y] == 1)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(" 1"); }
else if (list[Resualt1].Map[i, y] == 0 && list[Resualt2].Map[i, y] ==0)
{
Console.ForegroundColor = ConsoleColor.White;
Console.Write(" 0"); }
else if (list[Resualt1].Map[i, y] == 2 || list[Resualt2].Map[i, y] == 2)
{
Console.ForegroundColor = ConsoleColor.Red;
if (list[Resualt1].Map[i, y] == 2)
{
Console.Write(" " + list[Resualt1].Map[i, y].ToString());
continue;
}
if (list[Resualt2].Map[i, y] == 2)
{
Console.Write(" " + list[Resualt2].Map[i, y].ToString());
continue;
}
}
}
Console.WriteLine();
}
Console.Read();
}
static void Print(int[,] box)
{
for (int i = 0; i < WIDTH; i++)
{
Console.WriteLine();
for (int y = 0; y < HEIGHT; y++)
{
if (box[i, y] == 1)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(" " + box[i, y].ToString()); }
else if (box[i, y] == 0)
{
Console.ForegroundColor = ConsoleColor.White;
Console.Write(" " + box[i, y].ToString()); }
else if (box[i, y] == 2)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(" "+box[i, y].ToString());
}
}
}
}
static int Resualt1 = 0;
static int Resualt2 = 0;
static int f = 0; static int l = 1;
static void Walk()
{
list[f] = CreateMapState(1,1,box,true);
list[f + 1] = CreateMapState(8, 8, box,false);
do
{
Move(list[f], 0, 1,list[f].Flag );
Move(list[f], 1, 0, list[f].Flag);
Move(list[f], 0, -1, list[f].Flag);
Move(list[f], -1, 0, list[f].Flag);
f++;
}
while (CheckOut());
}
static bool CheckOut()
{
for (int i =0 ; i < l; i++)
{
for (int k = i+1; k < l-1; k++)
{
if (list[i].Current.X == list[k].Current.X && list[i].Current.Y == list[k].Current.Y && list[i].Flag!=list[k].Flag )
{
Resualt1 = i;
Resualt2 = k;
return false;
}
}
}
return true;
}
static void Move(MapState map,int x,int y,bool flag)
{
if (map.Map[map.Current.X + x, map.Current.Y + y] != 1 && map.Map[map.Current.X + x, map.Current.Y + y] != 2)
{
if (map.Current.X + x > 0 && map.Current.Y + y > 0)
{
l++;
list[l] = CreateMapState(map.Current.X + x, map.Current.Y + y, map.Map, flag);
}
}
}
static MapState CreateMapState(int x,int y,int[,] tmp,bool flag)
{
MapState box;
box.Flag = flag;
box.Map = (int[,])tmp.Clone ();
box.Map [x, y] = 2;
box.Current.X = x;
box.Current.Y = y;
return box;
}
}
}
System.Console.WriteLine("Debug日志");
和
System.Diagnostics.Debug.WriteLine("Debug日志");
相当在VS Output窗口可以看到日志,这样省去加一个TextBox控件并且还能移植到控制台程序,何乐不为?
1111111111112: {0}11111111111111111111011111112: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}10000000000000000001012: {0}2: {0}2: {0}2: {0}2: {0}12: {0}1111111111010111111111111111101012: {0}1112: {0}12: {0}100000000001010000000000
0000101012: {0}1012: {0}12: {0}1111111111110111111111111110101012: {0}1012: {0}12: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}0000000010101012: {0}1012: {0}10111111111111111112: {0}1111111110101012: {0}1012: {0}10100000000000000012: {0}1000000000101012: {0}1012: {0}10101111111111111112: {0}1111101110101012: {0}1012: {0}10101000000000000002: {0}2: {0}2: {0}2: {0}2: {0}101010101012: {0}1012: {0}101010111111111111111112: {0}101010101012: {0}1012: {0}101010100000000000000012: {0}101010101012: {0}1012: {0}101010101111111111111012: {0}
101010101012: {0}1012: {0}1010101012: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}1012: {0}101010101012: {0}1012: {0}1010101012: {0}1111111112: {0}1012: {0}101010101012: {0}1012: {0}1010101012: {0}1000000012: {0}1012: {0}101010101012: {0}1012: {0}1010101012: {0}1011111112: {0}1012: {0}101010101012: {0}1012: {0}1010101012: {0}1012: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}1012: {0}101010101012: {0}1012: {0}1010111012: {0}1012: {0}1111111012: {0}101010101012: {0}1012: {0}1010000012: {0}1012: {0}1000000012: {0}101010101012: {0}1012: {0}1010111012: {0}1012: {0}1111111112: {0}101010101012: {0}1012: {0}1010101012: {0}1012: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}2: {0}101010101012: {0}1我在输出窗口中看到的是这些,没有转化,是为什么?
Console.Write("{0}", maze[i, j]);这类是细节输出的问题,自己动手改改就可以了。
我想请问下,为什么,我把你上面那个迷宫的数组的初始化改掉,输出的结果就会出问题,甚至没有结果?
我刚才把你那个数组改为 {1,1,1,1,1,1,1,1,1,1},
{1,0,1,1,1,0,0,0,1,1},
{1,0,0,1,1,0,1,0,1,1},
{1,0,0,0,0,0,1,0,1,1},
{1,1,0,1,0,1,1,0,1,1},
{1,1,0,1,0,1,1,0,1,1},
{1,0,0,0,1,1,1,0,1,1},
{1,1,0,0,0,0,0,0,1,1},
{1,1,1,1,1,1,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}输出的结果只是把这些数组一一输出,没有结果
{
class Program
{
static int[,] MazeItem = new int[10, 10] //初始化迷宫
{
{1,1,1,1,1,1,1,1,1,1},
{1,0,1,1,1,0,0,0,1,1},
{1,0,0,1,1,0,1,0,1,1},
{1,0,0,0,0,0,1,0,1,1},
{1,1,0,1,0,1,1,0,1,1},
{1,1,0,1,0,1,1,0,1,1},
{1,0,0,0,1,1,1,0,1,1},
{1,1,0,0,0,0,0,0,1,1},
{1,1,1,1,1,1,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
}; static void Main(string[] args)
{
if (!LetsGo(1, 1, 1, 5))
Console.WriteLine("走不通。");
else
Console.WriteLine(" 打印的是从出口到入口的路径。");
} static bool LetsGo(int startX, int starY, int endX, int endY)
{
if (startX < 0 || startX >= 10 || endX < 0 || endX >= 10 || MazeItem[startX, starY] == 1)
return false; MazeItem[startX, starY] = 1; //避免重复
if ((startX == endX && starY == endY) ||
LetsGo(startX - 1, starY, endX, endY) || LetsGo(startX + 1, starY, endX, endY) ||
LetsGo(startX, starY + 1, endX, endY) || LetsGo(startX, starY - 1, endX, endY))
{
print(startX, starY);
return true;
}
else
return false;
} static void print(int x, int y)
{
Console.Write(string.Format("[{0},{1}]", x, y));
}
}
}
编程测验应该全都比这个复杂许多。
你能不能把你设计的那段代码的设计思路贴上来~~~
你是统代码,没有标注,看起来,有点吃力~~~
CreateMapState干的就是这个
Walk是开始走迷宫
CheckOut是检查出口和入口推出的路线有无重叠点
如果没有就继续推 如果有就EXIT···打出路线
Print是我调试用的。。没用……
struct MapState
{
public int[,] Map;地图的位置状态
public Point Current;但前走到哪个点
public bool Flag;是入口推出点还是出口推出的点
}