Guest User

Untitled

a guest
Dec 2nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Collections;
  7. using System.Collections.ObjectModel;
  8.  
  9. namespace ConsoleApp1
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             //TODO - PARSE INPUTS AND CREATE INPUT LIST
  16.             List<Domino> input = new List<Domino>();
  17.             input.Add(new Domino(3, 3));
  18.             Console.Write(input[0].leftFace);
  19.             Console.ReadLine();
  20.             NodeList pregraph = new NodeList();
  21.             for (int i = 0; i < input.Count; i++)
  22.             {
  23.                 pregraph.Add(new Node(input[i]));
  24.             }
  25.             Console.Write(pregraph[0].Value.key);
  26.             Console.ReadLine();
  27.             Graph dominograph = new Graph(pregraph);
  28.             dominograph.Remove(dominograph.Nodes[0]);
  29.             Console.WriteLine(dominograph.Nodes.Count);
  30.             Console.ReadLine();
  31.         }
  32.     }
  33.     public class Domino
  34.     {
  35.         Random randm = new Random();
  36.         public int leftFace { get; set; }
  37.         public int rightFace { get; set; }
  38.         public int key;
  39.         public Domino(int left, int right)
  40.         {
  41.             this.leftFace = left;
  42.             this.rightFace = right;
  43.             key = randm.Next();
  44.         }
  45.  
  46.     }
  47.     public class Node
  48.     {
  49.         public Domino data;
  50.         protected NodeList leftNeighbors = new NodeList
  51.         { };
  52.         protected NodeList rightNeighbors = new NodeList
  53.         { };
  54.         public Node() { }
  55.         public Node(Domino input)
  56.         {
  57.             data = input;
  58.         }
  59.         public Node(Domino data, NodeList leftNeighbors, NodeList rightNeighbors)
  60.         {
  61.             this.data = data;
  62.             this.leftNeighbors = leftNeighbors;
  63.             this.rightNeighbors = rightNeighbors;
  64.  
  65.         }
  66.         public Domino Value
  67.         {
  68.             get
  69.             {
  70.                 return this.data;
  71.             }
  72.             set
  73.             {
  74.                 data = Value;
  75.             }
  76.         }
  77.         new public NodeList RightNeighbors
  78.         {
  79.             get
  80.             {
  81.                 if (rightNeighbors == null)
  82.                     rightNeighbors = new NodeList();
  83.  
  84.                 return rightNeighbors;
  85.             }
  86.         }
  87.         new public NodeList LeftNeighbors
  88.         {
  89.             get
  90.             {
  91.                 if (leftNeighbors == null)
  92.                     leftNeighbors = new NodeList();
  93.  
  94.                 return rightNeighbors;
  95.             }
  96.         }
  97.  
  98.  
  99.     }
  100.     public class NodeList : Collection<Node>
  101.     {
  102.         public NodeList() : base() { }
  103.  
  104.         public NodeList(int initialSize)
  105.         {
  106.             // Add the specified number of items
  107.             for (int i = 0; i < initialSize; i++)
  108.                 base.Items.Add(default(Node));
  109.         }
  110.  
  111.         public Node FindByValue(Domino value)
  112.         {
  113.             // search the list for the value
  114.             foreach (Node node in Items)
  115.                 if (node.Value.Equals(value))
  116.                     return node;
  117.  
  118.             // if we reached here, we didn't find a matching node
  119.             return null;
  120.         }
  121.     }
  122.     public class GraphNode : Node
  123.     {
  124.         private List<int> costs;
  125.         public Node Data { get; set; }
  126.         public GraphNode() : base() { }
  127.         public GraphNode(Node blah)
  128.         {
  129.             Data = blah;
  130.         }
  131.         new public NodeList RightNeighbors
  132.         {
  133.             get
  134.             {
  135.                 if (base.rightNeighbors == null)
  136.                     base.rightNeighbors = new NodeList();
  137.  
  138.                 return base.rightNeighbors;
  139.             }
  140.         }
  141.         new public NodeList LeftNeighbors
  142.         {
  143.             get
  144.             {
  145.                 if (base.leftNeighbors == null)
  146.                     base.leftNeighbors = new NodeList();
  147.  
  148.                 return base.rightNeighbors;
  149.             }
  150.         }
  151.  
  152.         public List<int> Costs
  153.         {
  154.             get
  155.             {
  156.                 if (costs == null)
  157.                     costs = new List<int>();
  158.  
  159.                 return costs;
  160.             }
  161.         }
  162.     }
  163.     public class Graph : IEnumerable<GraphNode>
  164.     {
  165.         private NodeList nodeSet;
  166.         public Graph() : this(null) { }
  167.         public Graph(NodeList nodeSet)
  168.         {
  169.             if (nodeSet == null)
  170.                 this.nodeSet = new NodeList();
  171.             else
  172.                 this.nodeSet = nodeSet;
  173.         }
  174.  
  175.  
  176.         public void AddNode(GraphNode node)
  177.         {
  178.             // adds a node to the graph
  179.             nodeSet.Add(node.Data);
  180.         }
  181.  
  182.         public void AddNode(Node value)
  183.         {
  184.             // adds a node to the graph
  185.             nodeSet.Add(new GraphNode(value));
  186.         }
  187.         public void AddUndirectedRightEdge(Node from, Node to, int cost)
  188.         {
  189.             from.RightNeighbors.Add(to);
  190.  
  191.             if((from.Value.rightFace == to.Value.rightFace))
  192.                 to.RightNeighbors.Add(from);
  193.             else if ((from.Value.rightFace == to.Value.leftFace))
  194.                 to.LeftNeighbors.Add(from);
  195.         }
  196.         public void AddUndirectedLeftEdge(GraphNode from, GraphNode to, int cost)
  197.         {
  198.             from.LeftNeighbors.Add(to);
  199.  
  200.             if ((from.Value.leftFace == to.Value.rightFace))
  201.                 to.RightNeighbors.Add(from);
  202.             else if ((from.Value.leftFace == to.Value.leftFace))
  203.                 to.LeftNeighbors.Add(from);
  204.         }
  205.  
  206.         public bool Contains(Domino value)
  207.         {
  208.             return nodeSet.FindByValue(value) != null;
  209.         }
  210.  
  211.         public bool Remove(Node node)
  212.         {
  213.             // first remove the node from the nodeset
  214.             Node nodeToRemove = nodeSet.FindByValue(node.Value);
  215.             if (nodeToRemove == null)
  216.                 // node wasn't found
  217.                 return false;
  218.  
  219.             // otherwise, the node was found
  220.             nodeSet.Remove(nodeToRemove);
  221.  
  222.             // enumerate through each node in the nodeSet, removing edges to this node
  223.             foreach (Node gnode in nodeSet)
  224.             {
  225.                 int index = gnode.RightNeighbors.IndexOf(nodeToRemove);
  226.                 if (index != -1)
  227.                 {
  228.                     // remove the reference to the node and associated cost
  229.                     gnode.RightNeighbors.RemoveAt(index);
  230.                 }
  231.             }
  232.             foreach (Node gnode in nodeSet)
  233.             {
  234.                 int index = gnode.RightNeighbors.IndexOf(nodeToRemove);
  235.                 if (index != -1)
  236.                 {
  237.                     // remove the reference to the node and associated cost
  238.                     gnode.RightNeighbors.RemoveAt(index);
  239.                 }
  240.             }
  241.  
  242.             return true;
  243.         }
  244.  
  245.         public IEnumerator<GraphNode> GetEnumerator()
  246.         {
  247.             throw new NotImplementedException();
  248.         }
  249.  
  250.         IEnumerator IEnumerable.GetEnumerator()
  251.         {
  252.             throw new NotImplementedException();
  253.         }
  254.  
  255.         public NodeList Nodes
  256.         {
  257.             get
  258.             {
  259.                 return nodeSet;
  260.             }
  261.         }
  262.  
  263.         public int Count
  264.         {
  265.             get { return nodeSet.Count; }
  266.         }
  267.     }
  268. }
Advertisement
Add Comment
Please, Sign In to add comment