Advertisement
Tomhass

Untitled

Nov 20th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.49 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8.  
  9. namespace attempt
  10. {
  11.     class CalculateWeight
  12.     {
  13.         double weight;
  14.  
  15.         public double calculateWeight(string s, string y)
  16.         {
  17.             char[] seperator = { ',',']','[',' ' };
  18.             string[] weightArray1;
  19.             string[] weightArray2;
  20.  
  21.             weightArray1 = s.Split(seperator, StringSplitOptions.RemoveEmptyEntries);
  22.             weightArray2 = y.Split(seperator, StringSplitOptions.RemoveEmptyEntries);
  23.  
  24.             int x1 = Convert.ToInt32(weightArray1[0]);
  25.             int y1 = Convert.ToInt32(weightArray1[1]);
  26.             int x2 = Convert.ToInt32(weightArray2[0]);
  27.             int y2 = Convert.ToInt32(weightArray2[1]);
  28.  
  29.             int test1 = y2 - y1;
  30.             int test2 = x2 - y1;
  31.  
  32.             double test3 = test1 * test1 - test2 * test2;
  33.             if(test3 < 0)
  34.             {
  35.                 test3 = test3 * -1;
  36.             }
  37.  
  38.             weight = Math.Sqrt(test3);
  39.            
  40.  
  41.  
  42.             return weight;
  43.         }
  44.     }
  45.  
  46.     class DirectedEdge
  47.     {
  48.         // Starting vertex
  49.         private int _starting;
  50.         // Target vertex
  51.         private int _target;
  52.         // Weight from x to y
  53.         private double _weight;
  54.  
  55.         public DirectedEdge(int s, int t, double weight)
  56.         {
  57.             this._starting = s;
  58.             this._target = t;
  59.             this._weight = weight;
  60.         }
  61.  
  62.         // Return starting vertex
  63.         public int From()
  64.         {
  65.             return _starting;
  66.         }
  67.  
  68.         // Return weight
  69.         public double Weight()
  70.         {
  71.             return _weight;
  72.         }
  73.  
  74.         public override string ToString()
  75.         {
  76.             return String.Format("{0:d}->{1:d} weight is: {2:f}", _starting, _target, _weight);
  77.         }
  78.     }
  79.  
  80.  
  81.     class EdgeWeightedDiGraph
  82.     {
  83.         // Number of vertices
  84.         private int _vertices;
  85.         // Number of edges
  86.         private int _edges;
  87.         // Linked list of adjacent nodes
  88.         private LinkedList<DirectedEdge>[] _adjacent;
  89.  
  90.         public EdgeWeightedDiGraph(int v)
  91.         {
  92.             this._vertices = v;
  93.             this._edges = 0;
  94.  
  95.             // Create linked list from adj[x] with all vertices connected to it contained
  96.             _adjacent = new LinkedList<DirectedEdge>[v];
  97.             for(int x = 0; x < _vertices; x++)
  98.             {
  99.                 _adjacent[x] = new LinkedList<DirectedEdge>();
  100.             }
  101.         }
  102.         // Return the number of vertices
  103.         public int Vertices()
  104.         {
  105.             return _vertices;
  106.         }
  107.  
  108.         // Return the number of edges
  109.         public int Edges()
  110.         {
  111.             return _edges;
  112.         }
  113.  
  114.  
  115.         public void AddEdge(DirectedEdge edge)
  116.         {
  117.             _adjacent[edge.From()].AddFirst(edge);
  118.             _edges++;
  119.         }
  120.  
  121.         // Iterate through vertices linked list
  122.         public IEnumerable<DirectedEdge> Adj(int v)
  123.         {
  124.             return _adjacent[v];
  125.         }
  126.  
  127.         //Iterate through all edges
  128.         public IEnumerable<DirectedEdge> iterateEdges()
  129.         {
  130.             LinkedList<DirectedEdge> linkedList = new LinkedList<DirectedEdge>();
  131.             for (int v = 0; v < _vertices; v++)
  132.             {
  133.                 foreach (DirectedEdge e in _adjacent[v])
  134.                 {
  135.                     linkedList.AddFirst(e);
  136.                 }
  137.             }
  138.             return linkedList;
  139.         }
  140.     }
  141.  
  142.     public class ListWithDuplicates : List<KeyValuePair<int, int>>
  143.     {
  144.         public void Add(int key, int value)
  145.         {
  146.             var element = new KeyValuePair<int, int>(key, value);
  147.             this.Add(element);
  148.         }
  149.     }
  150.  
  151.  
  152.     class Program
  153.     {
  154.         static void Main(string[] args)
  155.         {
  156.             /*
  157.              * User input
  158.              */
  159.             string cav = "";
  160.             string userInput = Convert.ToString(Console.Read());
  161.  
  162.             cav = System.IO.File.ReadAllText(@"C:\Caverns\input1.cav");
  163.            
  164.  
  165.             /*
  166.              * Read in file
  167.              */
  168.  
  169.             char[] seperator = { ',' };
  170.             string[] cavArray; // array for storing cav file
  171.             int caveCounter = 1; // current cave
  172.             int size; // amount of caves
  173.             CalculateWeight calc = new CalculateWeight();
  174.  
  175.  
  176.             // Reading cav file into string
  177.             // Split cav string into array
  178.             cavArray = cav.Split(seperator, StringSplitOptions.RemoveEmptyEntries);
  179.             // Move cav array into list
  180.             List<int> cavList = new List<int>();
  181.             for (int i = 0; i < cavArray.Length; i++)
  182.             {
  183.                 cavList.Add(Convert.ToInt32(cavArray[i]));
  184.             }
  185.  
  186.             // Finding amount of caves
  187.             size = Convert.ToInt32(cavList[0]);
  188.             // Initialised directed weighted graph
  189.             EdgeWeightedDiGraph graph = new EdgeWeightedDiGraph(size);
  190.             cavList.RemoveAt(0);
  191.  
  192.             // Creating list for coordinates
  193.             List<int> coordList = new List<int>();
  194.             // Add coords to that list then remove them
  195.             for(int i = 0; i < size * 2; i++)
  196.             {
  197.                 coordList.Add(cavList[0]);
  198.                 cavList.RemoveAt(0);
  199.             }
  200.             // Creating list for connections
  201.             List<int> connectionList = new List<int>();
  202.             // Add connections to list
  203.             for(int i  = 0; i< size * size; i++)
  204.             {
  205.                 connectionList.Add(cavList[0]);
  206.                 cavList.RemoveAt(0);
  207.             }
  208.             // Splitting list of coordinates into seperate coordinate list
  209.             var list = new ListWithDuplicates();
  210.             for(int i  = 0; i < size * 2; i++)
  211.             {
  212.                 if(caveCounter % 2 == 0)
  213.                 {
  214.                     int x = coordList[0];
  215.                     coordList.RemoveAt(0);
  216.                     int y = coordList[0];
  217.                     coordList.RemoveAt(0);
  218.                     // Add to list
  219.                     list.Add(x, y);
  220.                 }
  221.                 caveCounter++;
  222.             }
  223.             // Reset cave counter
  224.             caveCounter = 0;
  225.             var connectionCounter = 0;
  226.  
  227.             foreach(int n in connectionList)
  228.             {
  229.                 if (connectionCounter % size == 0)
  230.                 {
  231.                     connectionCounter = 0;
  232.                     caveCounter++;
  233.                 }
  234.                 if (n == 1)
  235.                 {
  236.                     var value1 = "";
  237.                     var value2 = "";
  238.                     double weight = 0;
  239.  
  240.                     if (caveCounter == 7)
  241.                     {
  242.                         value1 = Convert.ToString(list[caveCounter - 1]);
  243.                         break;
  244.                     } else {
  245.                         value1 = Convert.ToString(list[caveCounter]);
  246.                         value2 = Convert.ToString(list[connectionCounter]);
  247.                         weight = calc.calculateWeight(value1, value2);
  248.                     }          
  249.                     DirectedEdge dirEdge = new DirectedEdge(caveCounter, connectionCounter, weight);
  250.                     System.IO.File.WriteAllText(@"C:\caveroute\output.txt", dirEdge.ToString());
  251.                 }
  252.  
  253.                 connectionCounter++;
  254.  
  255.  
  256.             }
  257.  
  258.  
  259.         }
  260.     }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement