-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
114 lines (108 loc) · 4.26 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Collections.Generic;
namespace PacificAtlanticWaterFlow
{
class Program
{
static void Main(string[] args)
{
var matrix = new int[][]
{
new int[] {1, 1},
new int[] {1, 1},
new int[] {1, 1},
};
Console.WriteLine(PacificAtlantic_BFS(matrix));
}
static IList<IList<int>> PacificAtlantic_BFS(int[][] matrix)
{
var res = new List<IList<int>>();
if (matrix.Length == 0 || matrix[0].Length == 0) return res;
var reachPacific = new bool[matrix.Length, matrix[0].Length];
var reachAtlantic = new bool[matrix.Length, matrix[0].Length];
int[] direction = { 0, 1, 0, -1, 0 };
for (int i = 0; i < matrix[0].Length; i++)
{
reachPacific[0, i] = true;
reachAtlantic[matrix.Length - 1, i] = true;
BFS(matrix, 0, i, direction, reachPacific);
BFS(matrix, matrix.Length - 1, i, direction, reachAtlantic);
}
for (int i = 0; i < matrix.Length; i++)
{
reachPacific[i, 0] = true;
reachAtlantic[i, matrix[0].Length - 1] = true;
BFS(matrix, i, 0, direction, reachPacific);
BFS(matrix, i, matrix[0].Length - 1, direction, reachAtlantic);
}
for (int x = 0; x < matrix.Length; x++)
{
for (int y = 0; y < matrix[0].Length; y++)
{
if (reachPacific[x, y] && reachAtlantic[x, y])
res.Add(new List<int> { x, y });
}
}
return res;
}
static void BFS(int[][] matrix, int x, int y, int[] direction, bool[,] visit)
{
var queue = new Queue<int[]>();
queue.Enqueue(new[] { x, y });
while (queue.Count != 0)
{
var cur = queue.Dequeue();
for (int i = 0; i < 4; i++)
{
int newX = direction[i] + cur[0], newY = direction[i + 1] + cur[1];
if (newX < 0 || newX >= matrix.Length || newY < 0 || newY >= matrix[0].Length)
continue;
if (matrix[newX][newY] >= matrix[cur[0]][cur[1]] && !visit[newX, newY])
{
visit[newX, newY] = true;
queue.Enqueue(new[] { newX, newY });
}
}
}
}
static IList<IList<int>> PacificAtlantic_DFS(int[][] matrix)
{
var res = new List<IList<int>>();
if (matrix.Length == 0 || matrix[0].Length == 0) return res;
var reachPacific = new bool[matrix.Length, matrix[0].Length];
var reachAtlantic = new bool[matrix.Length, matrix[0].Length];
int[] direction = { 0, 1, 0, -1, 0 };
for (int i = 0; i < matrix[0].Length; i++)
{
DFS(matrix, 0, i, direction, reachPacific);
DFS(matrix, matrix.Length - 1, i, direction, reachAtlantic);
}
for (int i = 0; i < matrix.Length; i++)
{
DFS(matrix, i, 0, direction, reachPacific);
DFS(matrix, i, matrix[0].Length - 1, direction, reachAtlantic);
}
for (int x = 0; x < matrix.Length; x++)
{
for (int y = 0; y < matrix[0].Length; y++)
{
if (reachPacific[x, y] && reachAtlantic[x, y])
res.Add(new List<int> { x, y });
}
}
return res;
}
static void DFS(int[][] matrix, int x, int y, int[] direction, bool[,] visit)
{
visit[x, y] = true;
for (int i = 0; i < 4; i++)
{
int newX = direction[i] + x, newY = direction[i + 1] + y;
if (newX < 0 || newX >= matrix.Length || newY < 0 || newY >= matrix[0].Length)
continue;
if (matrix[newX][newY] >= matrix[x][y] && !visit[newX, newY])
DFS(matrix, newX, newY, direction, visit);
}
}
}
}