Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.IO;
- using System.Text;
- using System.Collections;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- /**
- * Auto-generated code below aims at helping you parse
- * the standard input according to the problem statement.
- **/
- class Player
- {
- static Dictionary<string, Coord> dicDir = new Dictionary<string, Coord> {
- { "up", new Coord(0,-1) },
- { "right", new Coord(1, 0) },
- { "down", new Coord(0, 1) },
- { "left", new Coord(-1, 0) },
- };
- private const int MAX_HEIGHT = 20; // hauteur
- private const int MAX_WIDTH = 30; // largeur
- private const string NODE_START = "START"; // largeur
- private const string NODE_END = "END"; // largeur
- private const string NODE_VISITED = "VISITED"; // largeur
- private const string NODE_UNVISITED = "UNVISITED"; // largeur
- static List<Node> mapGrid = new List<Node>();
- static List<Node> unvisitedNodeList = new List<Node>();
- static List<Link> links = new List<Link>();
- static List<string> removeNode = new List<string>();
- static Node currentNode = null;
- static Node startNode = null;
- static Node endNode = null;
- public static void CreateMap()
- {
- for (int x = 0; x < MAX_WIDTH; x++)
- {
- for (int y = 0; y < MAX_HEIGHT; y++)
- {
- mapGrid.Add(new Node("X" + x + "Y" + y, (new Coord(x, y)), new List<Link>(), NODE_UNVISITED, new List<Node>()));
- }
- }
- }
- /**
- * PB : les liens se cumulent dans mes nodes
- * cr�e tous les liens � la cr�ation de la map
- * si une node est dead, supprimer les liens comme je pensais au d�but quoi
- * time out due au fait que les nodes cumulent les lien.
- * ou alors voir pour clear les liens des nodes � chaque tour
- *
- *
- *
- */
- static void Main(string[] args)
- {
- string[] inputs;
- /** Create map with every node **/
- CreateMap();
- links.Clear();
- // game loop
- while (true)
- {
- links.Clear();
- removeNode.Clear();
- currentNode = null;
- startNode = null;
- endNode = null;
- inputs = Console.ReadLine().Split(' ');
- int N = int.Parse(inputs[0]); // total number of players (2 to 4).
- int P = int.Parse(inputs[1]); // your player number (0 to 3).
- for (int i = 0; i < N; i++)
- {
- inputs = Console.ReadLine().Split(' ');
- int X0 = int.Parse(inputs[0]); // starting X coordinate of lightcycle (or -1)
- int Y0 = int.Parse(inputs[1]); // starting Y coordinate of lightcycle (or -1)
- int X1 = int.Parse(inputs[2]); // starting X coordinate of lightcycle (can be the same as X0 if you play before this player)
- int Y1 = int.Parse(inputs[3]); // starting Y coordinate of lightcycle (can be the same as Y0 if you play before this player)
- //Console.Error.WriteLine(X1);
- //Console.Error.WriteLine(Y1);
- if (i == P)
- {
- //Console.Error.WriteLine("current + start");
- //Console.Error.WriteLine(currentNode != null);
- SetStartNode(X1, Y1);
- }
- else
- {
- SetEndNode(X1, Y1);
- }
- PrepareToRemove(X1, Y1);
- }
- //Console.Error.WriteLine(links.Count);
- CreateLinkToNode();
- unvisitedNodeList.Clear();
- SetUnvisitedList();
- /** Cancel node not empty **/
- /** Verification if this path is possible **/
- // Write an action using Console.WriteLine()
- /** DIJSKTRA **/
- Dijkstra.FindShortestPath();
- //Console.Error.WriteLine("test1");
- //Console.Error.WriteLine(unvisitedNodeList.Count);
- //Console.Error.WriteLine(endNode.ShortestPath);
- //Console.Error.WriteLine(startNode.Name);
- //Console.Error.WriteLine("test 2 ");
- //Console.Error.WriteLine(currentNode.ShortestPath);
- //Console.Error.WriteLine("test 3");
- //Console.Error.WriteLine(endNode.ShortestPath);
- //Console.Error.WriteLine("test");
- //Node removeThisNode2 = mapGrid.Find(x => x.Name == "X5Y3");
- //Console.Error.WriteLine(test.Length);
- var nieghboursPath = endNode.ShortestPath.Split(' ');
- Console.Error.WriteLine(nieghboursPath[2]);
- Console.Error.WriteLine(startNode.Name);
- Console.WriteLine("LEFT"); // A single line with UP, DOWN, LEFT or RIGHT
- //Console.Error.WriteLine(currentNode != null);
- foreach (var nodeName in removeNode)
- {
- Node removeThisNode = mapGrid.Find(x => x.Name == nodeName);
- RemoveColoriseNode(removeThisNode.Position.x, removeThisNode.Position.y);
- }
- unvisitedNodeList.Clear();
- }
- }
- #region Dijkstra
- private static void SetStartNode(int x0, int y0)
- {
- Node node = mapGrid.Find(x => x.Position.x == x0 && x.Position.y == y0);
- startNode = node;
- startNode.Distance = 0;
- startNode.Visited = NODE_START;
- currentNode = node;
- startNode = node;
- }
- private static void SetEndNode(int x1, int y1)
- {
- Node node = mapGrid.Find(x => x.Position.x == x1 && x.Position.y == y1);
- endNode = node;
- endNode.Visited = NODE_END;
- // Ajout d'un node en neighbour plante , penser � d�commenter pathfind pour continuer
- }
- private static void SetUnvisitedList()
- {
- foreach (var node in mapGrid)
- {
- if (node.Visited == NODE_UNVISITED)
- {
- unvisitedNodeList.Add(node);
- }
- }
- }
- class Dijkstra
- {
- public static string[] FindShortestPath()
- {
- var key = 0;
- do
- {
- key++;
- var tentativeDistance = currentNode.Distance + 1;
- //Console.Error.WriteLine("count unvisited ");
- foreach (var neighbour in currentNode.Neighbours)
- {
- if (neighbour.Visited == NODE_UNVISITED)
- {
- //Console.Error.WriteLine(neighbour.Name);
- if (neighbour.Distance > tentativeDistance)
- {
- neighbour.Distance = tentativeDistance;
- neighbour.ShortestPath = currentNode.ShortestPath + " " + currentNode.Name;
- //Console.Error.WriteLine(neighbour.Distance);
- }
- }
- }
- currentNode.Visited = NODE_VISITED;
- unvisitedNodeList.Remove(currentNode);
- if (currentNode.Name == endNode.Name)
- break;
- currentNode = unvisitedNodeList.OrderBy(x => x.Distance).FirstOrDefault();
- }
- //while ((currentNode != null && currentNode.Distance != int.MaxValue) || endNode.Visited == NODE_VISITED || key > 10);
- while ( key < 1000 );
- //Console.Error.WriteLine("ma clé");
- //Console.Error.WriteLine(key);
- //Console.Error.WriteLine(currentNode != null);
- //Console.Error.WriteLine(currentNode.Distance != int.MaxValue);
- if (endNode.Distance == int.MaxValue)
- return null; // No path to this gateway exists
- else
- //Console.Error.WriteLine(endNode.ToString());
- //return (currentNode.ShortestPath + " " + currentNode.Name).TrimStart().Split(' ');
- return null;
- //
- }
- }
- #endregion
- private static void PrepareToRemove(int x1, int y1)
- {
- removeNode.Add("X" + x1 + "Y" + y1);
- }
- private static void RemoveColoriseNode(int x0, int y0)
- {
- Node removeThisNode = mapGrid.Find(x => x.Position.x == x0 && x.Position.y == y0);
- mapGrid.Remove(removeThisNode);
- }
- private static void CreateLinkToNode()
- {
- foreach (Node e in mapGrid)
- {
- /* reset node */
- e.Links.Clear();
- e.Neighbours.Clear();
- e.ShortestPath = "";
- e.Visited = NODE_UNVISITED;
- // create link up
- var linkAvailable =
- from node in mapGrid
- where (e.Position.x + 1 == node.Position.x && node.Position.y == e.Position.y)
- || (e.Position.x - 1 == node.Position.x && node.Position.y == e.Position.y)
- || (e.Position.x == node.Position.x && node.Position.y == e.Position.y + 1)
- || (e.Position.x == node.Position.x && node.Position.y == e.Position.y - 1)
- select new { Name = node.Name, NodeX = node.Position.x, NodeY = node.Position.y };
- foreach (var link in linkAvailable)
- {
- bool containsItem = links.Any((item => (item.A == link.Name && item.B == e.Name)));
- if (link.Name != null && !containsItem)
- {
- links.Add(new Link(link.Name, e.Name));
- e.Links.Add(new Link(link.Name, e.Name));
- }
- }
- foreach (var oneLink in e.Links)
- {
- // Link A = TARGET / Link B = SOURCE
- e.Neighbours.Add(mapGrid.Find(x => x.Name == oneLink.A));
- //Console.Error.WriteLine(oneLink);
- }
- //Console.Error.WriteLine(e.ShortestPath.Length);
- //Console.Error.WriteLine("\n\n");
- }
- }
- public class Coord
- {
- public int x { get; set; }
- public int y { get; set; }
- public Coord(int x, int y)
- {
- this.x = x;
- this.y = y;
- }
- }
- public class Link
- {
- public string A { get; set; }
- public string B { get; set; }
- public string[] Nodes { get { return new[] { A, B }; } }
- public override string ToString()
- {
- return A + " " + B;
- }
- public Link(string A, string B)
- {
- this.A = A;
- this.B = B;
- }
- }
- public class Node
- {
- public string Name { get; set; }
- public Coord Position { get; set; }
- public List<Link> Links { get; set; }
- public int Distance { get; set; }
- public string Visited { get; set; }
- public List<Node> Neighbours { get; set; }
- public string ShortestPath { get; set; }
- public Node(string name, Coord position, List<Link> links, string visited, List<Node> neighbours)
- {
- this.Name = name;
- this.Position = position;
- this.Links = links;
- this.Distance = Int32.MaxValue;
- this.Visited = visited;
- this.Neighbours = neighbours;
- }
- public override string ToString()
- {
- return string.Format("{0} ({1} pts), neighbours {2}, visited {3}", Name, Distance, Neighbours.Count, Visited);
- }
- }
- }
Add Comment
Please, Sign In to add comment