private void QueueUp(PrioriQ Q, int[,] lot, HashSet <int[]> visited, int[] pair, int cost) { if (lot[pair[0], pair[1]] != 0 && !visited.Contains(pair)) { Q.Enqueue(new Item(pair[0], pair[1], cost)); } }
public int removeObs(int r, int c, int[,] lot) { if (r <= 0 || c <= 0 || lot == null) { return(-1); } PrioriQ Q = new PrioriQ(); HashSet <int[]> visited = new HashSet <int[]>(); //0,0 is always flat Q.Enqueue(new Item(0, 0, 0)); while (Q.Count > 0) { Item item = Q.Dequeue(); //if(visited.Contains() int[] pair = new int[2]; pair[0] = item.Row; pair[1] = item.Col; if (visited.Contains(pair)) { continue; } if (lot[pair[0], pair[1]] == 9) { return(item.Length); } visited.Add(pair); if (item.Row > 0) { QueueUp(Q, lot, visited, new int[] { pair[0] - 1, pair[1] }, item.Length + 1); } if (item.Row < r - 1) { QueueUp(Q, lot, visited, new int[] { pair[0] + 1, pair[1] }, item.Length + 1); } if (item.Col > 0) { QueueUp(Q, lot, visited, new int[] { pair[0], pair[1] - 1 }, item.Length + 1); } if (item.Col < c - 1) { QueueUp(Q, lot, visited, new int[] { pair[0], pair[1] + 1 }, item.Length + 1); } } return(-1); }