-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
70 lines (68 loc) · 1.58 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
namespace ShortestDistanceFromAllBuildings
{
class Program
{
static void Main(string[] args)
{
int[][] grid = new[]
{
new[] {1, 0, 2, 0, 1},
new[] {0, 0, 0, 0, 0},
new[] {0, 0, 1, 0, 0},
};
Console.WriteLine(ShortestDistance(grid));
}
public static int ShortestDistance(int[][] grid)
{
int house = 0;
var empty = new List<int[]>();
for (int i = 0; i < grid.Length; i++)
{
for (int j = 0; j < grid[0].Length; j++)
{
if (grid[i][j] == 1)
house++;
else if (grid[i][j] == 0)
empty.Add(new[] { i, j });
}
}
int res = int.MaxValue;
foreach (var e in empty)
res = Math.Min(res, BFS(e[0], e[1], grid, house));
return res == int.MaxValue ? -1 : res;
}
public static int BFS(int x, int y, int[][] grid, int houses)
{
var queue = new Queue<int[]>();
queue.Enqueue(new[] { x, y, 0 });
var visited = new HashSet<int>();
visited.Add(100 * x + y);
int[] dir = { 1, 0, -1, 0, 1 };
int dis = 0;
while (queue.Count != 0)
{
var cur = queue.Dequeue();
for (int i = 0; i < 4; i++)
{
int newX = cur[0] + dir[i];
int newY = cur[1] + dir[i + 1];
if (newX < 0 || newX >= grid.Length || newY < 0 || newY >= grid[0].Length || grid[newX][newY] == 2)
continue;
if (visited.Add(100 * newX + newY))
{
if (grid[newX][newY] == 1)
{
dis += cur[2] + 1;
houses--;
}
else
queue.Enqueue(new[] {newX, newY, cur[2] + 1});
}
}
}
return houses == 0 ? dis : int.MaxValue;
}
}
}