fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5.  
  6. namespace Test
  7. {
  8. [DebuggerDisplay("Pont({X}, {Y})")]
  9. public class Point
  10. {
  11. #region Constructors
  12.  
  13. public Point(int x, int y)
  14. {
  15. X = x;
  16. Y = y;
  17. }
  18.  
  19. #endregion // Constructors
  20.  
  21. #region Properties
  22.  
  23. public int X
  24. {
  25. get;
  26. private set;
  27. }
  28.  
  29. public int Y
  30. {
  31. get;
  32. private set;
  33. }
  34.  
  35. #endregion // Properties
  36.  
  37. #region Methods
  38.  
  39. public Point Add(Point point)
  40. {
  41. return new Point(X + point.X, Y + point.Y);
  42. }
  43.  
  44. #endregion // Methods
  45.  
  46. #region Overrides of Object
  47.  
  48. /// <summary>
  49. /// Returns a string that represents the current object.
  50. /// </summary>
  51. /// <returns>
  52. /// A string that represents the current object.
  53. /// </returns>
  54. public override string ToString()
  55. {
  56. return string.Format("Point({0}, {1})", X, Y);
  57. }
  58.  
  59. #endregion
  60. }
  61.  
  62. public static class Map
  63. {
  64. #region Properties
  65.  
  66. private static bool[,] CellsWithShips
  67. {
  68. get;
  69. set;
  70. }
  71.  
  72. #endregion // Properties
  73.  
  74. #region Methods
  75.  
  76. public static IEnumerable<Point> GetAllShipPoints()
  77. {
  78. return Enumerable.Range(0, CellsWithShips.GetLength(0))
  79. .SelectMany(x => Enumerable.Range(0, CellsWithShips.GetLength(1)).Select(y => new Point(x, y)))
  80. .Where(p => CellsWithShips[p.X, p.Y]);
  81. }
  82.  
  83. public static void Init(int width, int height)
  84. {
  85. CellsWithShips = new bool[width, height];
  86. }
  87.  
  88. public static void Wound(Point location)
  89. {
  90. CellsWithShips[location.X, location.Y] = true;
  91. }
  92.  
  93. public static void Kill(Point location)
  94. {
  95. Wound(location);
  96. foreach(var point in GetShipPointsAndTheirNeighbors(location).ToList())
  97. {
  98. CellsWithShips[point.X, point.Y] = false;
  99. }
  100. }
  101.  
  102. /// <summary>
  103. /// This version does not work for strange reasons, it just skips a half of points. See TestKill_DoesNotWork_1 test case
  104. /// </summary>
  105. /// <param name="location"></param>
  106. public static void Kill_DoesNotWork(Point location)
  107. {
  108. Wound(location);
  109. foreach(var point in GetShipPointsAndTheirNeighbors(location))
  110. {
  111. CellsWithShips[point.X, point.Y] = false;
  112. }
  113. }
  114.  
  115. private static IEnumerable<Point> GetShipPointsAndTheirNeighbors(Point location)
  116. {
  117. return GetShipPoints(location).SelectMany(Near);
  118. }
  119.  
  120. private static IEnumerable<Point> Near(Point location)
  121. {
  122. return new[]
  123. {
  124. location.Add(new Point(0, -1)),
  125. location.Add(new Point(0, 0))
  126. };
  127. }
  128.  
  129. private static IEnumerable<Point> GetShipPoints(Point location)
  130. {
  131. var beforePoint = new[]
  132. {
  133. location,
  134. location.Add(new Point(0, -1)),
  135. location.Add(new Point(0, -2)),
  136. location.Add(new Point(0, -3))
  137. };
  138. return beforePoint.TakeWhile(p => CellsWithShips[p.X, p.Y]);
  139. }
  140.  
  141. #endregion // Methods
  142. }
  143.  
  144. public static class Program
  145. {
  146. private static void LoadMap()
  147. {
  148. Map.Init(20, 20);
  149.  
  150. Map.Wound(new Point(1, 4));
  151. Map.Wound(new Point(1, 5));
  152. Map.Wound(new Point(1, 6));
  153. }
  154.  
  155. private static int TestKill()
  156. {
  157. LoadMap();
  158. Map.Kill(new Point(1, 7));
  159. return Map.GetAllShipPoints().Count();
  160. }
  161.  
  162. private static int TestKillDoesNotWork()
  163. {
  164. LoadMap();
  165. Map.Kill_DoesNotWork(new Point(1, 7));
  166. return Map.GetAllShipPoints().Count();
  167. }
  168.  
  169. private static void Main()
  170. {
  171. Console.WriteLine("Test kill: {0}", TestKill());
  172. Console.WriteLine("Test kill (does not work): {0}", TestKillDoesNotWork());
  173. }
  174. }
  175. }
  176.  
Success #stdin #stdout 0.05s 24304KB
stdin
Standard input is empty
stdout
Test kill: 0
Test kill (does not work): 2