Program.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. namespace D6._1
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. if (args.Length < 2) return;
  12. if (File.Exists(args[0]) == false) return;
  13. if (uint.TryParse(args[1], out uint size) == false) return;
  14. var coordinates = new List<((uint x, uint y) coord, bool isInfinite)>();
  15. var file = File.OpenText(args[0]);
  16. do
  17. {
  18. var line = file.ReadLine();
  19. if (line == null) break;
  20. var cl = line.Split(", ");
  21. coordinates.Add(((uint.Parse(cl[0]), uint.Parse(cl[1])), false));
  22. } while (true);
  23. var area = new int[coordinates.Count];
  24. FillMapArea(size, coordinates, area);
  25. FlagPointOnEdges(size, coordinates);
  26. int maxArea = FindMaxCoveredArea(coordinates, area);
  27. Console.WriteLine($"Answer : {maxArea}");
  28. }
  29. private static int FindMaxCoveredArea(List<((uint x, uint y) coord, bool isInfinite)> coordinates, int[] area)
  30. {
  31. int maxArea = 0;
  32. for (int i = 0; i < area.Length; ++i)
  33. {
  34. if (coordinates.ElementAt(i).isInfinite) continue;
  35. if (area[i] > maxArea) maxArea = area[i];
  36. }
  37. return maxArea;
  38. }
  39. private static void FlagPointOnEdges(uint size, List<((uint x, uint y) coord, bool isInfinite)> coordinates)
  40. {
  41. for (uint i = 0; i < size; ++i)
  42. {
  43. var x0yi = GetClosest(coordinates, (0, i));
  44. MarkInfinite(coordinates, x0yi);
  45. var xiy0 = GetClosest(coordinates, (i, 0));
  46. MarkInfinite(coordinates, xiy0);
  47. var xsizeyi = GetClosest(coordinates, (size, i));
  48. MarkInfinite(coordinates, xsizeyi);
  49. var xiysize = GetClosest(coordinates, (i, size));
  50. MarkInfinite(coordinates, xiysize);
  51. }
  52. }
  53. private static void FillMapArea(uint size, List<((uint x, uint y) coord, bool isInfinite)> coordinates, int[] area)
  54. {
  55. for (uint x = 0; x < size; x++)
  56. {
  57. for (uint y = 0; y < size; y++)
  58. {
  59. var closest = GetClosest(coordinates, (x, y));
  60. area[closest]++;
  61. }
  62. }
  63. }
  64. private static int GetClosest(List<((uint x, uint y) coord, bool isInfinite)> coordinates, (uint x, uint y) point)
  65. {
  66. long lowest = int.MaxValue;
  67. int lowestId = 0;
  68. for (int i = 0; i < coordinates.Count; i++)
  69. {
  70. var (x, y) = coordinates[i].coord;
  71. var manhattan = Math.Abs((int)point.x - (int)x) + Math.Abs((int)point.y - (int)y);
  72. if (manhattan < lowest)
  73. {
  74. lowest = manhattan;
  75. lowestId = i;
  76. }
  77. }
  78. return lowestId;
  79. }
  80. private static void MarkInfinite(List<((uint x, uint y) coord, bool isInfinite)> coordinates, int id)
  81. {
  82. var coord = coordinates[id];
  83. coord.isInfinite = true;
  84. coordinates[id] = coord;
  85. }
  86. }
  87. }