Advertisement
Tomhass

Untitled

Nov 18th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.82 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. namespace AI_Coursework
  9. {
  10.     public class ListWithDuplicates : List<KeyValuePair<int, int>>
  11.     {
  12.         public void Add(int key, int value)
  13.         {
  14.             var element = new KeyValuePair<int, int>(key, value);
  15.             this.Add(element);
  16.         }
  17.     }
  18.  
  19.     class Graph
  20.     {
  21.         private int vertices;
  22.         private List<Int32>[] adjacent;
  23.         private double weight;
  24.  
  25.         public Graph(int v)
  26.         {
  27.             // THE AMOUNT OF VERTICES IN THE GRAPH
  28.             vertices = v;
  29.             // Create a list of size v to store edges
  30.             adjacent = new List<Int32>[v];
  31.             for (int i = 0; i < v; i++)
  32.             {
  33.                 adjacent[i] = new List<int>();
  34.             }
  35.         }
  36.         public double FindWeight(int a, int b, int x, int y)      
  37.         {
  38.             int wA = a - b;
  39.             int wX = x - y;
  40.             weight = Math.Sqrt(wA * wA + wX * wX);
  41.  
  42.             Console.WriteLine("The Weight from: " + a + ", " + b + " too " + x + ", " + y + " is: " + weight);
  43.             return weight;
  44.         }
  45.         public void AddEdge(int c, int v)
  46.         {
  47.             adjacent[c].Add(v);
  48.         }
  49.         public void DFS(int v)
  50.         {
  51.             // Create an array of bools that is the size of the amount of vertices
  52.             bool[] visited = new bool[vertices];
  53.             // Create a stack
  54.             Stack<Int32> stack = new Stack<int>();
  55.             // Set the first visited vertice in visited list to true
  56.             visited[v] = true;
  57.             stack.Push(v);
  58.  
  59.             // while the stack is not empty
  60.             while(stack.Count != 0)
  61.             {
  62.                 // v is set to the first value out of the stack and is then removed
  63.                 v = stack.Pop();
  64.                 // Prints the stack
  65.                 Console.WriteLine("next->" + v);
  66.  
  67.                 foreach(int i in adjacent[v])
  68.                 {
  69.                     Console.WriteLine(i);
  70.                     // If the value at visited[i] is false
  71.                     if(!visited[i])
  72.                     {
  73.                         // Set it to true
  74.                         visited[i] = true;
  75.                         // Push the value in i onto the stack
  76.                         stack.Push(i);
  77.                     }
  78.                 }
  79.             }
  80.         }
  81.     }
  82.  
  83.  
  84.     public class Program
  85.     {
  86.  
  87.         static void Main(string[] arg)
  88.         {
  89.             // VARIABLES
  90.             char[] seperator = { ',' };
  91.             string[] cavArray;
  92.             int caveCounter = 1;          
  93.             // VALUES FOR CALCULATING WEIGHT OF EDGES
  94.             int xFrom = 0, yFrom = 0, xToo = 0, yToo = 0;
  95.  
  96.  
  97.             // CAVES IN FILE
  98.             int size = 0;            
  99.  
  100.             // READING CAV FILE INTO STRING
  101.             string cav = System.IO.File.ReadAllText(@"C:\Caverns\input1.cav");          
  102.             if (cav != null)
  103.             {
  104.                 Console.WriteLine("File Loaded.");
  105.             }
  106.             else
  107.             {
  108.                 Console.WriteLine("File Failed to Load");
  109.             }
  110.  
  111.             // SPLITTING CAV STRING INTO ARRAY
  112.             cavArray = cav.Split(seperator, StringSplitOptions.RemoveEmptyEntries);
  113.             // MOVING CAV ARRAY INTO A LIST
  114.             List<int> cavList = new List<int>();
  115.             for (int i = 0; i < cavArray.Length; i++)
  116.             {
  117.                 cavList.Add(Convert.ToInt32(cavArray[i]));
  118.             }
  119.             // FINDING THE AMOUNT OF CAVES IN THE FILE THEN REMOVING FROM LIST
  120.             size = Convert.ToInt32(cavList[0]);
  121.             // INITIALISE GRAPH
  122.             Graph graph = new Graph(size * 2);
  123.  
  124.             cavList.RemoveAt(0);
  125.  
  126.             // CREATING LINKED LIST FOR COORDINATES
  127.             List<int> coordList = new List<int>();
  128.             // ADDING ALL COORDINATES TO THE LIST
  129.             for (int i = 0; i < size * 2; i++)
  130.             {
  131.                 coordList.Add(cavList[0]);
  132.                 cavList.RemoveAt(0);
  133.             }
  134.             // CREATING LINKED LIST FOR CONNECTIONS
  135.             List<int> connectionsList = new List<int>();
  136.             // ADDING ALL CONNECTIONS TO THE LIST
  137.             for (int i = 0; i < size * size; i++)
  138.             {
  139.                 connectionsList.Add(cavList[0]);
  140.                 cavList.RemoveAt(0);
  141.             }
  142.             // REMOVING CAVLIST AS UNNECESSARY
  143.             cavList.Clear();
  144.  
  145.             // SPLITTING THE LIST OF COORDINATES INTO A SEPERATE COORDINATE LIST
  146.             var list = new ListWithDuplicates();
  147.             for(int i = 0; i < size * 2; i++)
  148.             {
  149.                 if(caveCounter % 2 == 0)
  150.                 {
  151.                     // SETTING APPROPRIATE X Y COORDINATES
  152.                     int x = coordList[0];
  153.                     coordList.RemoveAt(0);
  154.                     int y = coordList[0];
  155.                     coordList.RemoveAt(0);
  156.                     // ADDING THEM TO A LIST
  157.                     list.Add(x, y);
  158.                 }
  159.                 caveCounter++;
  160.             }
  161.             // RESETTING CAVE COUNTER
  162.             caveCounter = 1;
  163.             var connectionCounter = 0;
  164.  
  165.  
  166.             foreach (int n in connectionsList)
  167.             {
  168.                 connectionCounter++;
  169.                 if (n == 1)
  170.                 {
  171.                     // ADDING THE CONNECTIONS TO THE CURRENT CAVE
  172.                     graph.AddEdge(caveCounter, connectionCounter);
  173.                 }
  174.                 if (connectionCounter % size == 0)
  175.                 {
  176.                     connectionCounter = 0;
  177.                     caveCounter++;
  178.                 }
  179.             }
  180.             graph.DFS(1);
  181.         }
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement