| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- namespace D6._1
- {
- class Program
- {
- static void Main(string[] args)
- {
- if (args.Length < 2) return;
- if (File.Exists(args[0]) == false) return;
- if (uint.TryParse(args[1], out uint size) == false) return;
- var coordinates = new List<((uint x, uint y) coord, bool isInfinite)>();
- var file = File.OpenText(args[0]);
- do
- {
- var line = file.ReadLine();
- if (line == null) break;
- var cl = line.Split(", ");
- coordinates.Add(((uint.Parse(cl[0]), uint.Parse(cl[1])), false));
- } while (true);
- var area = new int[coordinates.Count];
- FillMapArea(size, coordinates, area);
- FlagPointOnEdges(size, coordinates);
- int maxArea = FindMaxCoveredArea(coordinates, area);
- Console.WriteLine($"Answer : {maxArea}");
- }
- private static int FindMaxCoveredArea(List<((uint x, uint y) coord, bool isInfinite)> coordinates, int[] area)
- {
- int maxArea = 0;
- for (int i = 0; i < area.Length; ++i)
- {
- if (coordinates.ElementAt(i).isInfinite) continue;
- if (area[i] > maxArea) maxArea = area[i];
- }
- return maxArea;
- }
- private static void FlagPointOnEdges(uint size, List<((uint x, uint y) coord, bool isInfinite)> coordinates)
- {
- for (uint i = 0; i < size; ++i)
- {
- var x0yi = GetClosest(coordinates, (0, i));
- MarkInfinite(coordinates, x0yi);
- var xiy0 = GetClosest(coordinates, (i, 0));
- MarkInfinite(coordinates, xiy0);
- var xsizeyi = GetClosest(coordinates, (size, i));
- MarkInfinite(coordinates, xsizeyi);
- var xiysize = GetClosest(coordinates, (i, size));
- MarkInfinite(coordinates, xiysize);
- }
- }
- private static void FillMapArea(uint size, List<((uint x, uint y) coord, bool isInfinite)> coordinates, int[] area)
- {
- for (uint x = 0; x < size; x++)
- {
- for (uint y = 0; y < size; y++)
- {
- var closest = GetClosest(coordinates, (x, y));
- area[closest]++;
- }
- }
- }
- private static int GetClosest(List<((uint x, uint y) coord, bool isInfinite)> coordinates, (uint x, uint y) point)
- {
- long lowest = int.MaxValue;
- int lowestId = 0;
- for (int i = 0; i < coordinates.Count; i++)
- {
- var (x, y) = coordinates[i].coord;
- var manhattan = Math.Abs((int)point.x - (int)x) + Math.Abs((int)point.y - (int)y);
- if (manhattan < lowest)
- {
- lowest = manhattan;
- lowestId = i;
- }
- }
- return lowestId;
- }
- private static void MarkInfinite(List<((uint x, uint y) coord, bool isInfinite)> coordinates, int id)
- {
- var coord = coordinates[id];
- coord.isInfinite = true;
- coordinates[id] = coord;
- }
- }
- }
|