public Param(int x, int y, int counter, Param parent) { this.x = x; this.y = y; this.counter = counter; this.parent = parent; }
private List<Param> getAdjacent(Param p, int[][] map) { List<Param> adjacent = new List<Param>(); //Up if (p.y != 0 && map[p.y - 1][p.x] == 1) adjacent.Add(new Param(p.x, p.y - 1, p.counter + 1, p)); //Down if (p.y + 1 < map.Length && map[p.y + 1][p.x] == 1) adjacent.Add(new Param(p.x, p.y + 1, p.counter + 1, p)); //Left if (p.x != 0 && map[p.y][p.x - 1] == 1) adjacent.Add(new Param(p.x - 1, p.y, p.counter + 1, p)); //Right if (p.x + 1 < map[0].Length && map[p.y][p.x + 1] == 1) adjacent.Add(new Param(p.x + 1, p.y, p.counter + 1, p)); return adjacent; }
private List<Param> getNextMove(int[][] map, Point position, Point end) { List<Param> path = new List<Param>(); Param start = new Param((int)end.X, (int)end.Y, 0, null); path.Add(start); int current = 0; Param cur = start; while (current != path.Count) { cur = path[current]; List<Param> tmp = (getAdjacent(cur, map)); foreach (Param p in path) { tmp.RemoveAll(x => x.y == p.y && x.x == p.x && x.counter >= p.counter); } path.AddRange(tmp); current++; } return path; }