Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace attempt
- {
- class CalculateWeight
- {
- double weight;
- public double calculateWeight(string s, string y)
- {
- char[] seperator = { ',',']','[',' ' };
- string[] weightArray1;
- string[] weightArray2;
- weightArray1 = s.Split(seperator, StringSplitOptions.RemoveEmptyEntries);
- weightArray2 = y.Split(seperator, StringSplitOptions.RemoveEmptyEntries);
- int x1 = Convert.ToInt32(weightArray1[0]);
- int y1 = Convert.ToInt32(weightArray1[1]);
- int x2 = Convert.ToInt32(weightArray2[0]);
- int y2 = Convert.ToInt32(weightArray2[1]);
- int test1 = y2 - y1;
- int test2 = x2 - y1;
- double test3 = test1 * test1 - test2 * test2;
- if(test3 < 0)
- {
- test3 = test3 * -1;
- }
- weight = Math.Sqrt(test3);
- return weight;
- }
- }
- class DirectedEdge
- {
- // Starting vertex
- private int _starting;
- // Target vertex
- private int _target;
- // Weight from x to y
- private double _weight;
- public DirectedEdge(int s, int t, double weight)
- {
- this._starting = s;
- this._target = t;
- this._weight = weight;
- }
- // Return starting vertex
- public int From()
- {
- return _starting;
- }
- // Return weight
- public double Weight()
- {
- return _weight;
- }
- public override string ToString()
- {
- return String.Format("{0:d}->{1:d} weight is: {2:f}", _starting, _target, _weight);
- }
- }
- class EdgeWeightedDiGraph
- {
- // Number of vertices
- private int _vertices;
- // Number of edges
- private int _edges;
- // Linked list of adjacent nodes
- private LinkedList<DirectedEdge>[] _adjacent;
- public EdgeWeightedDiGraph(int v)
- {
- this._vertices = v;
- this._edges = 0;
- // Create linked list from adj[x] with all vertices connected to it contained
- _adjacent = new LinkedList<DirectedEdge>[v];
- for(int x = 0; x < _vertices; x++)
- {
- _adjacent[x] = new LinkedList<DirectedEdge>();
- }
- }
- // Return the number of vertices
- public int Vertices()
- {
- return _vertices;
- }
- // Return the number of edges
- public int Edges()
- {
- return _edges;
- }
- public void AddEdge(DirectedEdge edge)
- {
- _adjacent[edge.From()].AddFirst(edge);
- _edges++;
- }
- // Iterate through vertices linked list
- public IEnumerable<DirectedEdge> Adj(int v)
- {
- return _adjacent[v];
- }
- //Iterate through all edges
- public IEnumerable<DirectedEdge> iterateEdges()
- {
- LinkedList<DirectedEdge> linkedList = new LinkedList<DirectedEdge>();
- for (int v = 0; v < _vertices; v++)
- {
- foreach (DirectedEdge e in _adjacent[v])
- {
- linkedList.AddFirst(e);
- }
- }
- return linkedList;
- }
- }
- public class ListWithDuplicates : List<KeyValuePair<int, int>>
- {
- public void Add(int key, int value)
- {
- var element = new KeyValuePair<int, int>(key, value);
- this.Add(element);
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- /*
- * User input
- */
- string cav = "";
- string userInput = Convert.ToString(Console.Read());
- cav = System.IO.File.ReadAllText(@"C:\Caverns\input1.cav");
- /*
- * Read in file
- */
- char[] seperator = { ',' };
- string[] cavArray; // array for storing cav file
- int caveCounter = 1; // current cave
- int size; // amount of caves
- CalculateWeight calc = new CalculateWeight();
- // Reading cav file into string
- // Split cav string into array
- cavArray = cav.Split(seperator, StringSplitOptions.RemoveEmptyEntries);
- // Move cav array into list
- List<int> cavList = new List<int>();
- for (int i = 0; i < cavArray.Length; i++)
- {
- cavList.Add(Convert.ToInt32(cavArray[i]));
- }
- // Finding amount of caves
- size = Convert.ToInt32(cavList[0]);
- // Initialised directed weighted graph
- EdgeWeightedDiGraph graph = new EdgeWeightedDiGraph(size);
- cavList.RemoveAt(0);
- // Creating list for coordinates
- List<int> coordList = new List<int>();
- // Add coords to that list then remove them
- for(int i = 0; i < size * 2; i++)
- {
- coordList.Add(cavList[0]);
- cavList.RemoveAt(0);
- }
- // Creating list for connections
- List<int> connectionList = new List<int>();
- // Add connections to list
- for(int i = 0; i< size * size; i++)
- {
- connectionList.Add(cavList[0]);
- cavList.RemoveAt(0);
- }
- // Splitting list of coordinates into seperate coordinate list
- var list = new ListWithDuplicates();
- for(int i = 0; i < size * 2; i++)
- {
- if(caveCounter % 2 == 0)
- {
- int x = coordList[0];
- coordList.RemoveAt(0);
- int y = coordList[0];
- coordList.RemoveAt(0);
- // Add to list
- list.Add(x, y);
- }
- caveCounter++;
- }
- // Reset cave counter
- caveCounter = 0;
- var connectionCounter = 0;
- foreach(int n in connectionList)
- {
- if (connectionCounter % size == 0)
- {
- connectionCounter = 0;
- caveCounter++;
- }
- if (n == 1)
- {
- var value1 = "";
- var value2 = "";
- double weight = 0;
- if (caveCounter == 7)
- {
- value1 = Convert.ToString(list[caveCounter - 1]);
- break;
- } else {
- value1 = Convert.ToString(list[caveCounter]);
- value2 = Convert.ToString(list[connectionCounter]);
- weight = calc.calculateWeight(value1, value2);
- }
- DirectedEdge dirEdge = new DirectedEdge(caveCounter, connectionCounter, weight);
- System.IO.File.WriteAllText(@"C:\caveroute\output.txt", dirEdge.ToString());
- }
- connectionCounter++;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement