Guest User

Untitled

a guest
Sep 22nd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.26 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.IO;
  4. using System.Text;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8.  
  9. /**
  10. * Auto-generated code below aims at helping you parse
  11. * the standard input according to the problem statement.
  12. **/
  13. class Player
  14. {
  15. static Dictionary<string, Coord> dicDir = new Dictionary<string, Coord> {
  16. { "up", new Coord(0,-1) },
  17. { "right", new Coord(1, 0) },
  18. { "down", new Coord(0, 1) },
  19. { "left", new Coord(-1, 0) },
  20. };
  21.  
  22. private const int MAX_HEIGHT = 20; // hauteur
  23. private const int MAX_WIDTH = 30; // largeur
  24. private const string NODE_START = "START"; // largeur
  25. private const string NODE_END = "END"; // largeur
  26. private const string NODE_VISITED = "VISITED"; // largeur
  27. private const string NODE_UNVISITED = "UNVISITED"; // largeur
  28.  
  29.  
  30. static List<Node> mapGrid = new List<Node>();
  31. static List<Node> unvisitedNodeList = new List<Node>();
  32.  
  33. static List<Link> links = new List<Link>();
  34. static List<string> removeNode = new List<string>();
  35.  
  36. static Node currentNode = null;
  37. static Node startNode = null;
  38. static Node endNode = null;
  39.  
  40. public static void CreateMap()
  41. {
  42. for (int x = 0; x < MAX_WIDTH; x++)
  43. {
  44. for (int y = 0; y < MAX_HEIGHT; y++)
  45. {
  46. mapGrid.Add(new Node("X" + x + "Y" + y, (new Coord(x, y)), new List<Link>(), NODE_UNVISITED, new List<Node>()));
  47.  
  48. }
  49.  
  50. }
  51. }
  52. /**
  53. * PB : les liens se cumulent dans mes nodes
  54. * cr�e tous les liens � la cr�ation de la map
  55. * si une node est dead, supprimer les liens comme je pensais au d�but quoi
  56. * time out due au fait que les nodes cumulent les lien.
  57. * ou alors voir pour clear les liens des nodes � chaque tour
  58. *
  59. *
  60. *
  61. */
  62.  
  63.  
  64.  
  65. static void Main(string[] args)
  66. {
  67. string[] inputs;
  68. /** Create map with every node **/
  69. CreateMap();
  70. links.Clear();
  71.  
  72.  
  73. // game loop
  74. while (true)
  75. {
  76.  
  77. links.Clear();
  78. removeNode.Clear();
  79.  
  80. currentNode = null;
  81. startNode = null;
  82. endNode = null;
  83.  
  84.  
  85.  
  86. inputs = Console.ReadLine().Split(' ');
  87. int N = int.Parse(inputs[0]); // total number of players (2 to 4).
  88. int P = int.Parse(inputs[1]); // your player number (0 to 3).
  89. for (int i = 0; i < N; i++)
  90. {
  91. inputs = Console.ReadLine().Split(' ');
  92. int X0 = int.Parse(inputs[0]); // starting X coordinate of lightcycle (or -1)
  93. int Y0 = int.Parse(inputs[1]); // starting Y coordinate of lightcycle (or -1)
  94. int X1 = int.Parse(inputs[2]); // starting X coordinate of lightcycle (can be the same as X0 if you play before this player)
  95. int Y1 = int.Parse(inputs[3]); // starting Y coordinate of lightcycle (can be the same as Y0 if you play before this player)
  96. //Console.Error.WriteLine(X1);
  97. //Console.Error.WriteLine(Y1);
  98.  
  99. if (i == P)
  100. {
  101. //Console.Error.WriteLine("current + start");
  102. //Console.Error.WriteLine(currentNode != null);
  103.  
  104. SetStartNode(X1, Y1);
  105. }
  106. else
  107. {
  108. SetEndNode(X1, Y1);
  109. }
  110. PrepareToRemove(X1, Y1);
  111.  
  112. }
  113.  
  114. //Console.Error.WriteLine(links.Count);
  115. CreateLinkToNode();
  116. unvisitedNodeList.Clear();
  117. SetUnvisitedList();
  118.  
  119.  
  120. /** Cancel node not empty **/
  121. /** Verification if this path is possible **/
  122.  
  123. // Write an action using Console.WriteLine()
  124.  
  125.  
  126. /** DIJSKTRA **/
  127. Dijkstra.FindShortestPath();
  128.  
  129. //Console.Error.WriteLine("test1");
  130. //Console.Error.WriteLine(unvisitedNodeList.Count);
  131. //Console.Error.WriteLine(endNode.ShortestPath);
  132. //Console.Error.WriteLine(startNode.Name);
  133. //Console.Error.WriteLine("test 2 ");
  134. //Console.Error.WriteLine(currentNode.ShortestPath);
  135. //Console.Error.WriteLine("test 3");
  136. //Console.Error.WriteLine(endNode.ShortestPath);
  137. //Console.Error.WriteLine("test");
  138. //Node removeThisNode2 = mapGrid.Find(x => x.Name == "X5Y3");
  139.  
  140. //Console.Error.WriteLine(test.Length);
  141. var nieghboursPath = endNode.ShortestPath.Split(' ');
  142. Console.Error.WriteLine(nieghboursPath[2]);
  143. Console.Error.WriteLine(startNode.Name);
  144. Console.WriteLine("LEFT"); // A single line with UP, DOWN, LEFT or RIGHT
  145.  
  146. //Console.Error.WriteLine(currentNode != null);
  147.  
  148. foreach (var nodeName in removeNode)
  149. {
  150. Node removeThisNode = mapGrid.Find(x => x.Name == nodeName);
  151. RemoveColoriseNode(removeThisNode.Position.x, removeThisNode.Position.y);
  152. }
  153. unvisitedNodeList.Clear();
  154. }
  155. }
  156.  
  157. #region Dijkstra
  158. private static void SetStartNode(int x0, int y0)
  159. {
  160. Node node = mapGrid.Find(x => x.Position.x == x0 && x.Position.y == y0);
  161. startNode = node;
  162. startNode.Distance = 0;
  163. startNode.Visited = NODE_START;
  164. currentNode = node;
  165. startNode = node;
  166.  
  167. }
  168.  
  169. private static void SetEndNode(int x1, int y1)
  170. {
  171. Node node = mapGrid.Find(x => x.Position.x == x1 && x.Position.y == y1);
  172. endNode = node;
  173. endNode.Visited = NODE_END;
  174. // Ajout d'un node en neighbour plante , penser � d�commenter pathfind pour continuer
  175.  
  176. }
  177.  
  178. private static void SetUnvisitedList()
  179. {
  180. foreach (var node in mapGrid)
  181. {
  182. if (node.Visited == NODE_UNVISITED)
  183. {
  184. unvisitedNodeList.Add(node);
  185. }
  186.  
  187. }
  188. }
  189.  
  190. class Dijkstra
  191. {
  192.  
  193. public static string[] FindShortestPath()
  194. {
  195. var key = 0;
  196. do
  197. {
  198. key++;
  199. var tentativeDistance = currentNode.Distance + 1;
  200. //Console.Error.WriteLine("count unvisited ");
  201. foreach (var neighbour in currentNode.Neighbours)
  202. {
  203. if (neighbour.Visited == NODE_UNVISITED)
  204. {
  205. //Console.Error.WriteLine(neighbour.Name);
  206. if (neighbour.Distance > tentativeDistance)
  207. {
  208. neighbour.Distance = tentativeDistance;
  209. neighbour.ShortestPath = currentNode.ShortestPath + " " + currentNode.Name;
  210. //Console.Error.WriteLine(neighbour.Distance);
  211. }
  212. }
  213. }
  214.  
  215. currentNode.Visited = NODE_VISITED;
  216. unvisitedNodeList.Remove(currentNode);
  217. if (currentNode.Name == endNode.Name)
  218. break;
  219.  
  220. currentNode = unvisitedNodeList.OrderBy(x => x.Distance).FirstOrDefault();
  221. }
  222. //while ((currentNode != null && currentNode.Distance != int.MaxValue) || endNode.Visited == NODE_VISITED || key > 10);
  223. while ( key < 1000 );
  224.  
  225. //Console.Error.WriteLine("ma clé");
  226. //Console.Error.WriteLine(key);
  227. //Console.Error.WriteLine(currentNode != null);
  228. //Console.Error.WriteLine(currentNode.Distance != int.MaxValue);
  229.  
  230.  
  231. if (endNode.Distance == int.MaxValue)
  232. return null; // No path to this gateway exists
  233. else
  234. //Console.Error.WriteLine(endNode.ToString());
  235. //return (currentNode.ShortestPath + " " + currentNode.Name).TrimStart().Split(' ');
  236. return null;
  237. //
  238. }
  239.  
  240. }
  241. #endregion
  242.  
  243. private static void PrepareToRemove(int x1, int y1)
  244. {
  245. removeNode.Add("X" + x1 + "Y" + y1);
  246. }
  247.  
  248. private static void RemoveColoriseNode(int x0, int y0)
  249. {
  250. Node removeThisNode = mapGrid.Find(x => x.Position.x == x0 && x.Position.y == y0);
  251.  
  252. mapGrid.Remove(removeThisNode);
  253. }
  254.  
  255. private static void CreateLinkToNode()
  256. {
  257. foreach (Node e in mapGrid)
  258. {
  259. /* reset node */
  260. e.Links.Clear();
  261. e.Neighbours.Clear();
  262. e.ShortestPath = "";
  263. e.Visited = NODE_UNVISITED;
  264.  
  265. // create link up
  266. var linkAvailable =
  267. from node in mapGrid
  268. where (e.Position.x + 1 == node.Position.x && node.Position.y == e.Position.y)
  269. || (e.Position.x - 1 == node.Position.x && node.Position.y == e.Position.y)
  270. || (e.Position.x == node.Position.x && node.Position.y == e.Position.y + 1)
  271. || (e.Position.x == node.Position.x && node.Position.y == e.Position.y - 1)
  272. select new { Name = node.Name, NodeX = node.Position.x, NodeY = node.Position.y };
  273.  
  274.  
  275.  
  276. foreach (var link in linkAvailable)
  277. {
  278. bool containsItem = links.Any((item => (item.A == link.Name && item.B == e.Name)));
  279. if (link.Name != null && !containsItem)
  280. {
  281. links.Add(new Link(link.Name, e.Name));
  282. e.Links.Add(new Link(link.Name, e.Name));
  283. }
  284. }
  285.  
  286. foreach (var oneLink in e.Links)
  287. {
  288. // Link A = TARGET / Link B = SOURCE
  289. e.Neighbours.Add(mapGrid.Find(x => x.Name == oneLink.A));
  290. //Console.Error.WriteLine(oneLink);
  291. }
  292.  
  293.  
  294. //Console.Error.WriteLine(e.ShortestPath.Length);
  295. //Console.Error.WriteLine("\n\n");
  296.  
  297. }
  298. }
  299.  
  300. public class Coord
  301. {
  302. public int x { get; set; }
  303. public int y { get; set; }
  304.  
  305. public Coord(int x, int y)
  306. {
  307. this.x = x;
  308. this.y = y;
  309. }
  310. }
  311.  
  312.  
  313.  
  314. public class Link
  315. {
  316. public string A { get; set; }
  317. public string B { get; set; }
  318. public string[] Nodes { get { return new[] { A, B }; } }
  319.  
  320. public override string ToString()
  321. {
  322. return A + " " + B;
  323. }
  324. public Link(string A, string B)
  325. {
  326. this.A = A;
  327. this.B = B;
  328. }
  329. }
  330.  
  331. public class Node
  332. {
  333. public string Name { get; set; }
  334. public Coord Position { get; set; }
  335. public List<Link> Links { get; set; }
  336. public int Distance { get; set; }
  337. public string Visited { get; set; }
  338. public List<Node> Neighbours { get; set; }
  339. public string ShortestPath { get; set; }
  340.  
  341. public Node(string name, Coord position, List<Link> links, string visited, List<Node> neighbours)
  342. {
  343. this.Name = name;
  344. this.Position = position;
  345. this.Links = links;
  346. this.Distance = Int32.MaxValue;
  347. this.Visited = visited;
  348. this.Neighbours = neighbours;
  349. }
  350.  
  351. public override string ToString()
  352. {
  353. return string.Format("{0} ({1} pts), neighbours {2}, visited {3}", Name, Distance, Neighbours.Count, Visited);
  354. }
  355. }
  356. }
Add Comment
Please, Sign In to add comment