Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.06 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Program
  6. {
  7.     public struct Cord
  8.     {
  9.         public int X { get; set; }
  10.         public int Y { get; set; }
  11.         public static readonly Cord Up = new Cord { X = 0, Y = -1 };
  12.         public static readonly Cord Down = new Cord { X = 0, Y = 1 };
  13.         public static readonly Cord Left = new Cord { X = -1, Y = 0 };
  14.         public static readonly Cord Right = new Cord { X = 1, Y = 0 };
  15.     }
  16.  
  17.     public class City
  18.     {
  19.         public int Width { get; private set; }
  20.         public int Height { get; private set; }
  21.         public int Prefered { get; private set; }
  22.         public bool[,] NotPassable { get; private set; }
  23.  
  24.         public City(int width, int height, int prefered)
  25.         {
  26.             NotPassable = new bool[width, height];
  27.         }
  28.  
  29.         private static readonly Cord[][] PreferedNeigbors = new Cord[][]
  30.         {
  31.             new Cord[] { Cord.Up, Cord.Down, Cord.Left, Cord.Right },
  32.             new Cord[] { Cord.Right, Cord.Left, Cord.Down, Cord.Up },
  33.             new Cord[] { Cord.Left, Cord.Up, Cord.Down, Cord.Right },
  34.             new Cord[] { Cord.Down, Cord.Right, Cord.Up, Cord.Left },
  35.         };
  36.  
  37.         private IEnumerable<Cord> GetNeigbors(Cord cord)
  38.         {
  39.             return PreferedNeigbors[Prefered]
  40.                  .Select((n) => new Cord { X = n.X + cord.X, Y = n.Y + cord.Y })
  41.                  .Where((n) => n.X > 0 && n.Y > 0 && n.X < Width && n.Y < Height);
  42.         }
  43.  
  44.         private class QueueNode
  45.         {
  46.             public Cord Cord { get; set; }
  47.             public QueueNode LastNode { get; set; }
  48.         }
  49.  
  50.         public List<Cord> GetPath(Cord start, Cord end)
  51.         {
  52.             var path = new List<Cord>();
  53.             var queue = new Queue<QueueNode>();
  54.             var visited = new bool[Width, Height];
  55.  
  56.             queue.Enqueue(new QueueNode
  57.             {
  58.                 Cord = start,
  59.                 LastNode = null,
  60.             });
  61.  
  62.             while(queue.Count != 0)
  63.             {
  64.                 var current = queue.Dequeue();
  65.  
  66.                 if(current.Cord.X == end.X && current.Cord.Y == end.Y)
  67.                 {
  68.                     for(var n = current; n != null; n = n.LastNode)
  69.                     {
  70.                         path.Append(n.Cord);
  71.                     }
  72.                     break;
  73.                 }
  74.                 foreach(var n in GetNeigbors(current.Cord))
  75.                 {
  76.                     if(!visited[n.X, n.Y] && !NotPassable[n.X, n.Y])
  77.                     {
  78.                         visited[n.X, n.Y] = true;
  79.                         queue.Enqueue(new QueueNode
  80.                         {
  81.                             Cord = current.Cord,
  82.                             LastNode = current,
  83.                         });
  84.                     }
  85.                 }
  86.             }
  87.  
  88.             return path;
  89.         }
  90.     }
  91.  
  92.  
  93.     class Program
  94.     {
  95.         static void Main(string[] args)
  96.         {
  97.             Console.WriteLine("Hello World!");
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement