Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Dungeon
  9. {
  10.     public class DungeonTask
  11.     {
  12.         public static MoveDirection[] FindShortestPath(Map map)
  13.         {
  14.             var shortWay = GetShortWay(map);
  15.             var wayToTheCheck = GetWayToTheCheck(map);
  16.             if (shortWay is null) return new MoveDirection[0];
  17.             if (shortWay.Any(point => map.Chests.Contains(point))) return GetIfNotExit(shortWay);
  18.             if (wayToTheCheck.FirstOrDefault() is null) return GetIfNotCheck(shortWay);
  19.             var wayToExitThisCheck = GetWayToExitThisCheck(wayToTheCheck, map);
  20.             var ans = (wayToExitThisCheck.Item1
  21.                 .Reverse()
  22.                 .Concat(wayToExitThisCheck.Item2.Reverse().Skip(1)));
  23.             return GetFiniteWay(ans);
  24.         }
  25.  
  26.         public static SinglyLinkedList<Point> GetShortWay(Map map)
  27.         {
  28.            return BfsTask.FindPaths(map, map.InitialPosition, new Point[] { map.Exit })
  29.                 .OrderBy(x => x.Length)
  30.                 .FirstOrDefault();
  31.         }
  32.  
  33.         public static IEnumerable<SinglyLinkedList<Point>> GetWayToTheCheck(Map map)
  34.         {
  35.             return BfsTask.FindPaths(map, map.InitialPosition, map.Chests);
  36.         }
  37.  
  38.         public static Tuple<SinglyLinkedList<Point>, SinglyLinkedList<Point>> GetWayToExitThisCheck
  39.             (IEnumerable<SinglyLinkedList<Point>> wayToTheCheck,Map map)
  40.         {
  41.             return wayToTheCheck
  42.                 .Select(pathToChest =>
  43.                 {
  44.                     var shortWayInExit = BfsTask.FindPaths(
  45.                         map, pathToChest.Value, new Point[] { map.Exit })
  46.                         .OrderBy(x => x.Length)
  47.                         .First();
  48.                     return Tuple.Create(pathToChest, shortWayInExit);
  49.                 })
  50.                 .OrderBy(ite => ite.Item1.Length + ite.Item2.Length)
  51.                 .FirstOrDefault();
  52.         }
  53.  
  54.         public static MoveDirection[] GetFiniteWay(IEnumerable<Point> ans)
  55.         {
  56.             return ans
  57.                 .Zip(ans.Skip(1), (previousPoint, point) =>
  58.                      new Size(point.X - previousPoint.X,point.Y - previousPoint.Y))
  59.                 .Select(offset => Walker.ConvertOffsetToDirection(offset))
  60.                 .ToArray();
  61.         }
  62.  
  63.         public static MoveDirection[] GetIfNotExit(SinglyLinkedList<Point> shortWay)
  64.         {
  65.            return shortWay.Reverse()
  66.                 .Zip(shortWay.Reverse().Skip(1), (previousPoint, point) =>
  67.                      new Size(point.X - previousPoint.X,point.Y - previousPoint.Y))
  68.                 .Select(offset => Walker.ConvertOffsetToDirection(offset))
  69.                 .ToArray();
  70.         }
  71.  
  72.         public static MoveDirection[] GetIfNotCheck(SinglyLinkedList<Point> shortWay)
  73.         {
  74.             return shortWay.Reverse()
  75.                 .Zip(shortWay.Reverse().Skip(1), (previousPoint, point) =>
  76.                      new Size(point.X - previousPoint.X,point.Y - previousPoint.Y))
  77.                 .Select(offset => Walker.ConvertOffsetToDirection(offset))
  78.                 .ToArray();
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement