-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoveObstacle-Amazon.linq
More file actions
61 lines (56 loc) · 1.14 KB
/
removeObstacle-Amazon.linq
File metadata and controls
61 lines (56 loc) · 1.14 KB
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
<Query Kind="Program" />
void Main()
{
removeObstacle(3, 3,
new int[,] {
{1,0,0},
{1,0,0},
{1,9,0}
}).Dump();
}
int removeObstacle(int numRows, int numColumns, int[,] lot)
{
int result = -1;
var visited = new List<int[]>();
var toBeVisited = new Queue<int[]>();
toBeVisited.Enqueue(new []{0,0});
while(toBeVisited.Count > 0)
{
var point = toBeVisited.Dequeue();
if(visited.Any(x => x[0] == point[0] && x[1] == point[1]) == false)
{
visited.Add(point);
result = result + 1;
if(lot[point[0],point[1]] == 9)
{
return result;
}
else
{
var row = point[0];
var col = point[1];
//step left
if (col > 0 && lot[row, col - 1] != 0)
{
toBeVisited.Enqueue(new[] { row, col - 1});
}
//step right
if (col < (numColumns - 1) && lot[row, col + 1] != 0)
{
toBeVisited.Enqueue(new[] { row, col + 1});
}
//step up
if (row > 0 && lot[row - 1, col] != 0)
{
toBeVisited.Enqueue(new[] { row - 1, col} );
}
//step down
if (row < (numRows - 1) && lot[row + 1, col] != 0)
{
toBeVisited.Enqueue(new[] { row + 1, col} );
}
}
}
}
return -1;
}