-
Notifications
You must be signed in to change notification settings - Fork 35
/
DFSConnectedCellinaGrid.cs
133 lines (98 loc) · 3.48 KB
/
DFSConnectedCellinaGrid.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// DFS: Connected Cell in a Grid
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Solution {
// Complete the maxRegion function below.
static int maxRegion(int[][] matrix)
{
var xOffset = new[] { -1, 0, 1, -1 };
var yOffset = new[] { -1, -1, -1, 0 };
var ds = new DisjointSets();
var rows = matrix.Length;
var cols = matrix[0].Length;
var max = 0;
Func<int, int, bool> isValid = (int x, int y) =>
{
return x >= 0 && x < cols && y >= 0 && y < rows;
};
for (int y = 0; y < rows; y++)
{
for (int x = 0; x < cols; x++)
{
if(matrix[y][x] == 0) continue;
var current = new CellKey(x, y);
ds.AddNew(current);
max = Math.Max(max, 1);
for (int index = 0; index < xOffset.Length; index++)
{
var ox = x + xOffset[index];
var oy = y + yOffset[index];
if (!isValid(ox, oy)) continue;
if(matrix[oy][ox] == 0) continue;
var offset = new CellKey(ox, oy);
if (ds.Find(current) != ds.Find(offset))
max = Math.Max(max, ds.Union(current, offset));
}
}
}
return max;
}
struct CellKey
{
public int X;
public int Y;
public CellKey(int x, int y)
{
X = x;
Y = y;
}
public static bool operator !=(CellKey lhs, CellKey rhs) => !(lhs == rhs);
public static bool operator ==(CellKey lhs, CellKey rhs) => lhs.X == rhs.X && lhs.Y == rhs.Y;
public override bool Equals(object obj) => this == (CellKey) obj;
public override int GetHashCode() => base.GetHashCode();
public override string ToString() => $"{Y} - {X}";
}
class DisjointSets
{
Dictionary<CellKey, CellKey> Parents = new Dictionary<CellKey, CellKey>();
Dictionary<CellKey, int> Counts = new Dictionary<CellKey, int>();
public void AddNew(CellKey key)
{
Parents.Add(key, key);
Counts.Add(key, 1);
}
public CellKey Find(CellKey value) => Parents[value] == value ? value : Find(Parents[value]);
public int Union(CellKey left, CellKey right)
{
var pLeft = Find(left);
var pRight = Find(right);
Parents[pRight] = pLeft;
Counts[pLeft] = Counts[pLeft] + Counts[pRight];
return Counts[pLeft];
}
}
static void Main(string[] args) {
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
int n = Convert.ToInt32(Console.ReadLine());
int m = Convert.ToInt32(Console.ReadLine());
int[][] grid = new int[n][];
for (int i = 0; i < n; i++) {
grid[i] = Array.ConvertAll(Console.ReadLine().Split(' '), gridTemp => Convert.ToInt32(gridTemp));
}
int res = maxRegion(grid);
textWriter.WriteLine(res);
textWriter.Flush();
textWriter.Close();
}
}