using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GrapCity.Competition.CastleRush.Ai;
using GrapCity.Competition.CastleRush.Ai.View;
namespace _D01B6320_82D9_4D54_AFC9_C502657F2D99_
{
public class AimStartOnPosition
{
//全局变量
int[,] Arr = new int[60, 60];//迷宫算法:最大迷宫为50*50
Position BeginPos;
MapView MAP = new MapView();
bool canplace(int prePosValue, int x, int y)//判断当前点能否走通
{
if (x >= 0 && y >= 0 && x < MAP.Map.GetLength(0) && y < MAP.Map.GetLength(1) && Arr[x, y] != -1)//未越界且不是障碍物(-1)
{
if (Arr[x, y] == 0) return true;//该点还未走过
else return (prePosValue + 1) < Arr[x, y];//该点走过,选择更近的路径
}
return false;
}
public Stack<Position> path = new Stack<Position>();//记录路径
void search(Position CurP)
{
int x, y, num;
Position NewCurP;
num = Arr[CurP.X, CurP.Y];
x = CurP.X - 1; y = CurP.Y; //(左)
NewCurP = new Position(x, y);
{
if (canplace(num, x, y))
{
Arr[x, y] = num + 1;
path.Push(NewCurP);//进栈
search(NewCurP); //深度优先搜索
path.Pop();//出栈
}
}
x = CurP.X; y = CurP.Y + 1;//(下)
NewCurP = new Position(x, y);
{
if (canplace(num, x, y))
{
Arr[x, y] = num + 1;
path.Push(NewCurP);//进栈
search(NewCurP);
path.Pop();//出栈
}
}
x = CurP.X + 1; y = CurP.Y;//(右)
NewCurP = new Position(x, y);
{
if (canplace(num, x, y))
{
Arr[x, y] = num + 1;
path.Push(NewCurP);//进栈
search(NewCurP);
path.Pop();//出栈
}
}
x = CurP.X; y = CurP.Y - 1;//(上)
NewCurP = new Position(x, y);
{
if (canplace(num, x, y))
{
Arr[x, y] = num + 1;
path.Push(NewCurP);//进栈
search(NewCurP);
path.Pop();//出栈
}
}
}
public int[,] ShortestPath(Position BeginPosition, MapView map)//特殊之处:BeginPosition
{
MAP = map;//全局化map变量
BeginPos = new Position(BeginPosition.X, BeginPosition.Y);//将起始点全局化
for (int i = 0; i < map.Map.GetLength(0); i++)//列 //初始化地图数组Arr
{
for (int j = 0; j < map.Map.GetLength(1); j++)//行
{
if (map.Map[i, j].GetItemType() == ItemType.River
|| map.Map[i, j].GetItemType() == ItemType.Mine
|| map.Map[i, j].GetItemType() == ItemType.Caslte)
{ Arr[i, j] = -1; }//骑士不可走过
else //路、骑士
{ Arr[i, j] = 0; }//骑士可走
}
}
Arr[BeginPos.X, BeginPos.Y] = 1;//起点位置为1
search(BeginPos);//搜索最短路径
for (int i = 0; i < map.Map.GetLength(0); i++)//列 //初始化地图数组Arr
{
for (int j = 0; j < map.Map.GetLength(1); j++)//行
{
if (Arr[i, j] == 0)
{ Arr[i, j] = 10000; }//对于那些不可达的路(仁保持着原数组值),修改为无穷远10000
}
}
return Arr;
}
}
}