public VertexMap(int[,] map) { DimX = map.GetLength(1); DimY = map.GetLength(0); Map = new Vertex[DimY, DimX]; for (int i = 0; i < DimY; i++) { for (int j = 0; j < DimX; j++) { Map[i, j] = new Vertex(this, i, j, map[i, j]); } } ProcessMap(); }
public VertexMap(StreamReader reader) { string[] lineSeparator = new string[] { " " }; string line = null; if (reader.Peek() <= 0) throw new ArgumentException("Can't read from reader"); line = reader.ReadLine(); string[] parts = line.Split(lineSeparator, StringSplitOptions.None); if (parts.Length != 2) throw new ArgumentException(string.Format("Expected line with two integers, got invalid line '{0}'", line)); DimY = int.Parse(parts[0]); DimX = int.Parse(parts[1]); Map = new Vertex[DimY, DimX]; int i = 0, j = 0, value; while (reader.Peek() > 0) { line = reader.ReadLine(); if (string.IsNullOrEmpty(line)) continue; parts = line.Split(lineSeparator, StringSplitOptions.None); if (parts.Length != DimX) throw new ArgumentException(string.Format("Wrong number of elements: {0}, expected {1}", parts.Length, DimX)); j = 0; foreach (string valueStr in parts) { if(!int.TryParse(valueStr, out value))throw new ArgumentException(String.Format("Invalid parameter in line [{0}, {1}] - not an int {2}", i,j, valueStr)); Map[i, j] = new Vertex(this, i, j, value); j++; } i++; } ProcessMap(); }
public Route(Vertex vertex) { Vertexes.Add(vertex); vertex.LongestRoute = this; }
private Route FindLongestRoute(Vertex vertex) { if (vertex.Processed) { return vertex.LongestRoute; } vertex.Processed = true; Route bestRoute = null, neighborRoute = null; foreach (Vertex neighbor in vertex.GetNeighbors()) { if (neighbor.Height < vertex.Height) { neighborRoute = FindLongestRoute(neighbor); if (bestRoute == null || neighborRoute.isBetterThan(bestRoute)) bestRoute = neighborRoute; } } vertex.LongestRoute.AppendRoute(bestRoute); return vertex.LongestRoute; }
public Neighbors(Vertex v) { vertex = v; }