private static string Solve(int n, int k, int[,] array) { for (int i = 0; i < n; i++) { int lastj = n - 1; for (int j = n - 1; j >= 0; j--) { if (array[i, j] != 0) { array[i, lastj] = array[i, j]; lastj--; } } for (int j = lastj; j >= 0; j--) { array[i, j] = 0; } } // Horiz DisjointTracker <int> tracker1 = new DisjointTracker <int>(); // vert DisjointTracker <int> tracker2 = new DisjointTracker <int>(); // down right DisjointTracker <int> tracker3 = new DisjointTracker <int>(); // up right DisjointTracker <int> tracker4 = new DisjointTracker <int>(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (array[i, j] != 0) { tracker1.Add(GetVal(i, j, n)); tracker2.Add(GetVal(i, j, n)); tracker3.Add(GetVal(i, j, n)); tracker4.Add(GetVal(i, j, n)); } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (array[i, j] != 0) { if (i > 0) { if (array[i, j] == array[i - 1, j]) { tracker2.Union(GetVal(i, j, n), GetVal(i - 1, j, n)); } } if (j > 0) { if (array[i, j] == array[i, j - 1]) { tracker1.Union(GetVal(i, j, n), GetVal(i, j - 1, n)); } } if (i > 0 && j > 0) { if (array[i, j] == array[i - 1, j - 1]) { tracker3.Union(GetVal(i, j, n), GetVal(i - 1, j - 1, n)); } } if (i > 0 && j < n - 1) { if (array[i, j] == array[i - 1, j + 1]) { tracker4.Union(GetVal(i, j, n), GetVal(i - 1, j + 1, n)); } } } } } Dictionary <int, int> counter1 = new Dictionary <int, int>(); Dictionary <int, int> counter2 = new Dictionary <int, int>(); Dictionary <int, int> counter3 = new Dictionary <int, int>(); Dictionary <int, int> counter4 = new Dictionary <int, int>(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (array[i, j] != 0) { IncrementByRepresentative(counter1, tracker1, GetVal(i, j, n)); IncrementByRepresentative(counter2, tracker2, GetVal(i, j, n)); IncrementByRepresentative(counter3, tracker3, GetVal(i, j, n)); IncrementByRepresentative(counter4, tracker4, GetVal(i, j, n)); } } } bool rWin = false; bool bWin = false; UpdateWin(array, counter1, k, ref rWin, ref bWin, n); UpdateWin(array, counter2, k, ref rWin, ref bWin, n); UpdateWin(array, counter3, k, ref rWin, ref bWin, n); UpdateWin(array, counter4, k, ref rWin, ref bWin, n); if (rWin && bWin) { return("Both"); } if (rWin) { return("Red"); } if (bWin) { return("Blue"); } return("Neither"); }
private static string Solve(int n, int k, int[,] array) { for (int i = 0; i < n; i++) { int lastj = n - 1; for (int j = n - 1; j >= 0; j--) { if (array[i, j] != 0) { array[i, lastj] = array[i, j]; lastj--; } } for (int j = lastj; j >= 0; j--) { array[i, j] = 0; } } // Horiz DisjointTracker<int> tracker1 = new DisjointTracker<int>(); // vert DisjointTracker<int> tracker2 = new DisjointTracker<int>(); // down right DisjointTracker<int> tracker3 = new DisjointTracker<int>(); // up right DisjointTracker<int> tracker4 = new DisjointTracker<int>(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (array[i, j] != 0) { tracker1.Add(GetVal(i, j, n)); tracker2.Add(GetVal(i, j, n)); tracker3.Add(GetVal(i, j, n)); tracker4.Add(GetVal(i, j, n)); } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (array[i, j] != 0) { if (i > 0) { if (array[i, j] == array[i - 1, j]) tracker2.Union(GetVal(i, j, n), GetVal(i - 1, j, n)); } if (j > 0) { if (array[i, j] == array[i, j - 1]) tracker1.Union(GetVal(i, j, n), GetVal(i, j - 1, n)); } if (i > 0 && j > 0) { if (array[i, j] == array[i - 1, j - 1]) tracker3.Union(GetVal(i, j, n), GetVal(i - 1, j - 1, n)); } if (i > 0 && j < n - 1) { if (array[i, j] == array[i - 1, j + 1]) tracker4.Union(GetVal(i, j, n), GetVal(i - 1, j + 1, n)); } } } } Dictionary<int, int> counter1 = new Dictionary<int, int>(); Dictionary<int, int> counter2 = new Dictionary<int, int>(); Dictionary<int, int> counter3 = new Dictionary<int, int>(); Dictionary<int, int> counter4 = new Dictionary<int, int>(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (array[i, j] != 0) { IncrementByRepresentative(counter1, tracker1, GetVal(i, j, n)); IncrementByRepresentative(counter2, tracker2, GetVal(i, j, n)); IncrementByRepresentative(counter3, tracker3, GetVal(i, j, n)); IncrementByRepresentative(counter4, tracker4, GetVal(i, j, n)); } } } bool rWin=false; bool bWin = false; UpdateWin(array, counter1, k, ref rWin, ref bWin, n); UpdateWin(array, counter2, k, ref rWin, ref bWin, n); UpdateWin(array, counter3, k, ref rWin, ref bWin, n); UpdateWin(array, counter4, k, ref rWin, ref bWin, n); if (rWin && bWin) return "Both"; if (rWin) return "Red"; if (bWin) return "Blue"; return "Neither"; }
private static string Solve(int r, int[] x1, int[] x2, int[] y1, int[] y2) { DisjointTracker <int> sets = new DisjointTracker <int>(); for (int i = 0; i < r; i++) { sets.Add(i); } for (int i = 0; i < r; i++) { for (int j = i + 1; j < r; j++) { if (x1[i] > x2[j] + 1 || x2[i] < x1[j] - 1 || y1[i] > y2[j] + 1 || y2[i] < y1[j] - 1) { continue; } if (x1[i] == x2[j] + 1 && y1[i] == y2[j] + 1) { continue; } if (x1[j] == x2[i] + 1 && y1[j] == y2[i] + 1) { continue; } sets.Union(i, j); } } Dictionary <int, List <int> > setLookup = new Dictionary <int, List <int> >(); for (int i = 0; i < r; i++) { int rep = sets.GetRepresentative(i); List <int> vals; if (setLookup.TryGetValue(rep, out vals)) { vals.Add(i); } else { vals = new List <int>(); vals.Add(i); setLookup[rep] = vals; } } int max = 0; foreach (List <int> set in setLookup.Values) { int maxx2 = int.MinValue; int maxy2 = int.MinValue; for (int i = 0; i < set.Count; i++) { if (x2[set[i]] > maxx2) { maxx2 = x2[set[i]]; } if (y2[set[i]] > maxy2) { maxy2 = y2[set[i]]; } } int selectedx1 = x1[set[0]]; int selectedy1 = y1[set[0]]; for (int i = 1; i < set.Count; i++) { if (y1[set[i]] - selectedy1 < -1 * (x1[set[i]] - selectedx1)) { selectedy1 = y1[set[i]]; selectedx1 = x1[set[i]]; } } int thisTotal = maxx2 - selectedx1 + maxy2 - selectedy1 + 1; if (thisTotal > max) { max = thisTotal; } } return(max.ToString()); }