Esempio n. 1
0
 private Problem4(InputParser input)
 {
     Height   = input.GetInt();
     Width    = input.GetInt();
     Distance = input.GetInt();
     char[][] hall = input.GetStrings(Height).Select(s => s.ToCharArray()).ToArray();
     SegmentsFacingDown  = Enumerable.Range(0, Height - 1).Select(i => new List <HSegment>()).ToArray();
     SegmentsFacingUp    = Enumerable.Range(0, Height - 1).Select(i => new List <HSegment>()).ToArray();
     SegmentsFacingRight = Enumerable.Range(0, Width - 1).Select(i => new List <VSegment>()).ToArray();
     SegmentsFacingLeft  = Enumerable.Range(0, Width - 1).Select(i => new List <VSegment>()).ToArray();
     for (int h = 0; h < Height; ++h)
     {
         for (int w = 0; w < Width; ++w)
         {
             if (hall[h][w] == '#')
             {
                 if (h != Height - 1 && hall[h + 1][w] != '#')
                 {
                     if (SegmentsFacingDown[h].Count > 0 && SegmentsFacingDown[h][SegmentsFacingDown[h].Count - 1].right == w - 1)
                     {
                         SegmentsFacingDown[h][SegmentsFacingDown[h].Count - 1].right = w;
                     }
                     else
                     {
                         SegmentsFacingDown[h].Add(new HSegment()
                         {
                             left = w - 1, right = w
                         });
                     }
                 }
                 if (h != 0 && hall[h - 1][w] != '#')
                 {
                     if (SegmentsFacingUp[h - 1].Count > 0 && SegmentsFacingUp[h - 1][SegmentsFacingUp[h - 1].Count - 1].right == w - 1)
                     {
                         SegmentsFacingUp[h - 1][SegmentsFacingUp[h - 1].Count - 1].right = w;
                     }
                     else
                     {
                         SegmentsFacingUp[h - 1].Add(new HSegment()
                         {
                             left = w - 1, right = w
                         });
                     }
                 }
                 if (w != Width - 1 && hall[h][w + 1] != '#')
                 {
                     if (SegmentsFacingRight[w].Count > 0 && SegmentsFacingRight[w][SegmentsFacingRight[w].Count - 1].bottom == h - 1)
                     {
                         SegmentsFacingRight[w][SegmentsFacingRight[w].Count - 1].bottom = h;
                     }
                     else
                     {
                         SegmentsFacingRight[w].Add(new VSegment()
                         {
                             top = h - 1, bottom = h
                         });
                     }
                 }
                 if (w != 0 && hall[h][w - 1] != '#')
                 {
                     if (SegmentsFacingLeft[w - 1].Count > 0 && SegmentsFacingLeft[w - 1][SegmentsFacingLeft[w - 1].Count - 1].bottom == h - 1)
                     {
                         SegmentsFacingLeft[w - 1][SegmentsFacingLeft[w - 1].Count - 1].bottom = h;
                     }
                     else
                     {
                         SegmentsFacingLeft[w - 1].Add(new VSegment()
                         {
                             top = h - 1, bottom = h
                         });
                     }
                 }
             }
             else if (hall[h][w] == 'X')
             {
                 InitialX = w - 0.5;
                 InitialY = h - 0.5;
             }
         }
     }
 }