Esempio n. 1
0
        public static FrogNode CreateFrogNodeTree(int numberOfFrogs)
        {
            FrogNode root = new FrogNode(numberOfFrogs);

            root = FrogNode.CreateFrogPossibilities(root);
            return(root);
        }
Esempio n. 2
0
 static void Main(string[] args)
 {
     FrogNode.DFS(0);
     FrogNode.DFS(1);
     FrogNode.DFS(2);
     FrogNode.DFS(3);
     FrogNode.DFS(4);
 }
Esempio n. 3
0
        public override bool Equals(object obj)
        {
            if (!(obj is FrogNode))
            {
                return(false);
            }

            FrogNode node = obj as FrogNode;

            for (int i = 0; i < Frogs.Count; i++)
            {
                if (Frogs[i] != node.Frogs[i])
                {
                    return(false);
                }
            }
            return(true);
        }
Esempio n. 4
0
 static FrogNode CreateFrogPossibilities(FrogNode node)
 {
     if (node.ZeroPos - 1 >= 0 && node.Frogs[node.ZeroPos - 1] == '>')
     {
         FrogNode newNode = new FrogNode(node);
         newNode.ZeroPos                = node.ZeroPos - 1;
         newNode.Frogs[node.ZeroPos]    = '>';
         newNode.Frogs[newNode.ZeroPos] = '_';
         node.Nodes.Add(newNode);
     }
     if (node.ZeroPos - 2 >= 0 && node.Frogs[node.ZeroPos - 2] == '>')
     {
         FrogNode newNode = new FrogNode(node);
         newNode.ZeroPos                = node.ZeroPos - 2;
         newNode.Frogs[node.ZeroPos]    = '>';
         newNode.Frogs[newNode.ZeroPos] = '_';
         node.Nodes.Add(newNode);
     }
     if (node.ZeroPos + 1 < node.Frogs.Count && node.Frogs[node.ZeroPos + 1] == '<')
     {
         FrogNode newNode = new FrogNode(node);
         newNode.ZeroPos                = node.ZeroPos + 1;
         newNode.Frogs[node.ZeroPos]    = '<';
         newNode.Frogs[newNode.ZeroPos] = '_';
         node.Nodes.Add(newNode);
     }
     if (node.ZeroPos + 2 < node.Frogs.Count && node.Frogs[node.ZeroPos + 2] == '<')
     {
         FrogNode newNode = new FrogNode(node);
         newNode.ZeroPos                = node.ZeroPos + 2;
         newNode.Frogs[node.ZeroPos]    = '<';
         newNode.Frogs[newNode.ZeroPos] = '_';
         node.Nodes.Add(newNode);
     }
     foreach (FrogNode child in node.Nodes)
     {
         FrogNode.CreateFrogPossibilities(child);
     }
     return(node);
 }
Esempio n. 5
0
        public static void DFS(int numberOfFrogs)
        {
            if (numberOfFrogs <= 0)
            {
                Console.WriteLine("No frogs to swap!");
                return;
            }
            FrogNode         root     = FrogNode.CreateFrogNodeTree(numberOfFrogs);
            FrogNode         searched = new FrogNode(numberOfFrogs, '<', '>');
            Stack <FrogNode> path     = new Stack <FrogNode>();
            List <FrogNode>  visited  = new List <FrogNode>();

            path.Push(root);
            while (path.Any())
            {
                FrogNode top = path.Peek();
                if (top.Equals(searched))
                {
                    FrogNode.PrintPath(path);
                    break;
                }
                if (top.Nodes.Any() && !CheckIfAllVisited(top.Nodes, visited))
                {
                    foreach (FrogNode node in top.Nodes)
                    {
                        if (!visited.Contains(node))
                        {
                            path.Push(node);
                            break;
                        }
                    }
                }
                else
                {
                    visited.Add(path.Pop());
                }
            }
        }
Esempio n. 6
0
 public FrogNode(FrogNode node)
 {
     Frogs   = new List <char>(node.Frogs);
     Nodes   = new List <FrogNode>();
     ZeroPos = node.ZeroPos;
 }