Advertisement
Guest User

Untitled

a guest
May 24th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.IO;
  5. using System.Security.Permissions;
  6.  
  7. namespace KnightAndPawn
  8. {
  9.     public class Point
  10.     {
  11.         public int X
  12.         {
  13.             get; private set;
  14.         }
  15.  
  16.         public int Y
  17.         {
  18.             get; private set;
  19.         }
  20.  
  21.         public Point(int x, int y)
  22.         {
  23.             X = x;
  24.             Y = y;
  25.         }
  26.  
  27.         public override string ToString()
  28.         {
  29.             return Program.ConvertFromPointToString(this);
  30.  
  31.         }
  32.  
  33.         public override bool Equals(object obj)
  34.         {
  35.             if (obj.GetType() != typeof (Point))
  36.                 return false;
  37.             var oth = (Point) obj;
  38.             return oth.X == X && oth.Y == Y;
  39.         }
  40.  
  41.         public override int GetHashCode()
  42.         {
  43.             return new System.Drawing.Point(X, Y).GetHashCode();
  44.         }
  45.     }
  46.  
  47.     public class Program
  48.     {
  49.         public static string ConvertFromPointToString(Point point)
  50.         {
  51.             var dict = new Dictionary<int, string>{
  52.                 {0, "a"}, {1,"b"}, {2, "c"}, {3, "d"},
  53.                 {4, "e"}, {5, "f"}, {6, "g"}, {7, "h"}
  54.             };
  55.             return dict[point.X] + (point.Y + 1).ToString();
  56.            
  57.         }
  58.  
  59.         public static Point GetCooFromString(string input)
  60.         {
  61.             var dict = new Dictionary<string, int>{
  62.                 {"a", 0}, {"b", 1}, {"c", 2}, {"d", 3},
  63.                 {"e", 4}, {"f", 5}, {"g", 6}, {"h", 7}
  64.             };
  65.             var x = dict[input.First().ToString()];
  66.             var y = int.Parse(input.Last().ToString()) - 1;
  67.             return new Point(x, y);
  68.         }
  69.  
  70.         public static bool CheckBounds(Point currentPoint)
  71.         {
  72.             return currentPoint.X >= 0 && currentPoint.X < 8 &&
  73.                 currentPoint.Y >= 0 && currentPoint.Y < 8;
  74.         }
  75.  
  76.         public static bool IsUnderAttack(Point point, Point pawn)
  77.         {
  78.             var firstPosition = new Point(pawn.X + 1, pawn.Y - 1);
  79.             var secondPosition = new Point(pawn.X - 1, pawn.Y - 1);
  80.             return Equals(point, firstPosition) || Equals(point, secondPosition);
  81.         }
  82.  
  83.         public static IEnumerable<Point> GetNeighbours(Point currentPoint)
  84.         {
  85.             var vectors = new List<Point>{
  86.                 new Point(1, 2), new Point(-1, 2),
  87.                 new Point(-2, 1), new Point(-2, -1),
  88.                 new Point(-1, -2), new Point(1, -2),
  89.                 new Point(2, -1), new Point(2, 1)
  90.             };
  91.            
  92.             return vectors
  93.                 .Select(vect => new Point(vect.X + currentPoint.X, vect.Y + currentPoint.Y))
  94.                 .Where(CheckBounds);
  95.         }
  96.  
  97.         public static Tuple<Point, Point> GetCooFromFile()
  98.         {
  99.             var lines = File.ReadAllLines(@".\in.txt");
  100.             var knight = GetCooFromString(lines[0]);
  101.             var pawn = GetCooFromString(lines[1]);
  102.             return Tuple.Create(knight, pawn);
  103.         }
  104.         public static void Main(string[] args)
  105.         {
  106.             var parents = new Point[8,8];
  107.             var input = GetCooFromFile();
  108.             var knight = input.Item1;
  109.             var pawn = input.Item2;
  110.             var visited = new List<Point>();
  111.  
  112.             var stack = new Stack<Point>();
  113.             stack.Push(knight);
  114.             while (true)
  115.             {
  116.                 var current = stack.Pop();
  117.                 if (visited.Contains(current))
  118.                     continue;
  119.                 visited.Add(current);
  120.                 if (current.Equals(pawn))
  121.                     break;
  122.                 var flag = false;
  123.                 foreach (var nei in GetNeighbours(current))
  124.                 {
  125.                     if (parents[nei.X, nei.Y] != null)
  126.                         continue;
  127.                     if (!visited.Contains(nei) && !IsUnderAttack(nei, pawn))
  128.                     {
  129.                         parents[nei.X, nei.Y] = current;
  130.                         stack.Push(nei);
  131.                     }
  132.                     if (!Equals(nei, pawn)) continue;
  133.                     flag = true;
  134.                     break;
  135.                 }
  136.                 if (flag)
  137.                     break;
  138.  
  139.             }
  140.             var path = new List<Point>();
  141.             var currentPoint = pawn;
  142.             while (!Equals(currentPoint, knight))
  143.             {
  144.                 path.Add(currentPoint);
  145.                 currentPoint = parents[currentPoint.X, currentPoint.Y];
  146.             }
  147.             path.Add(knight);
  148.             path.Reverse();
  149.             string answer = path.Aggregate("", (current, p) => current + (ConvertFromPointToString(p) + "\r\n"));
  150.             File.WriteAllText(@".\out.txt", answer);
  151.         }
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement