Ejemplo n.º 1
0
 public void Sequential_Solve()
 {
     sw.Reset();
     sw.Start();
     if (startPoint == new Point(-1, -1) || endPoint == new Point(-1, -1))
     {
         MessageBox.Show("Start Point or End Point isn't set", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
     }
     else
     {
         queue         = new Queue <CarNode>();
         node          = new CarNode(startPoint, 0, 0);
         visitedNodes  = new List <Tuple <Point, int> >();
         PathReference = new Dictionary <Tuple <Point, int>, CarNode>();
         queue.Enqueue(node);
         bool status = false;
         while (queue.Count > 0)
         {
             node = queue.Dequeue();
             if (node.Point == endPoint)
             {
                 status = true;
                 List <CarNode> PATH = new List <CarNode>();
                 PATH.Add(node);
                 while (node.path != 0)
                 {
                     Tuple <Point, int> tuple = new Tuple <Point, int>(node.Point, node.direction);
                     node = PathReference[tuple];
                     PATH.Add(node);
                 }
                 ts = sw.Elapsed;
                 String Time = string.Format("Timer:{0,2}.{1:00}", ts.Seconds, ts.Milliseconds);
                 sw.Stop();
                 ResultForm resultForm = new ResultForm(MazeBoard_x, MazeBoard_y, startPoint, endPoint, BlockedPoints, PATH, Time);
                 resultForm.ShowDialog();
                 break;
             }
             for (int i = 0; i < 2; ++i)
             {
                 int   new_direction = (node.direction + i) % 4;
                 Point new_point     = new Point(node.Point.X + direction_x[new_direction], node.Point.Y + direction_y[new_direction]);
                 Tuple <Point, int> new_reference = new Tuple <Point, int>(new_point, new_direction);
                 if (is_valid(new_point) && !visitedNodes.Contains(new_reference))
                 {
                     queue.Enqueue(new CarNode(new_point, new_direction, node.path + 1));
                     PathReference[new_reference] = new CarNode(node.Point, node.direction, node.path);
                     visitedNodes.Add(new_reference);
                 }
             }
         }
         if (status == false)
         {
             MessageBox.Show("No Path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
         }
     }
 }
Ejemplo n.º 2
0
        private void NodesThread(List <CarNode> nodes, CancellationToken cantoks)
        {
            List <CarNode> nodes_lst = new List <CarNode>();
            CarNode        node      = nodes.Last();

            mutex_lock.WaitOne();
            if (cantoks.IsCancellationRequested)
            {
                mutex_lock.ReleaseMutex();
                return;
            }
            if (node.Point == endPoint)
            {
                this.cantoks.Cancel();
                Resultnodes = nodes;
                this.finish = true;
                mutex_lock.ReleaseMutex();
                return;
            }
            mutex_lock.ReleaseMutex();
            for (int i = 0; i < 2; ++i)
            {
                int   new_direction = (node.direction + i) % 4;
                Point new_point     = new Point(node.Point.X + direction_x[new_direction], node.Point.Y + direction_y[new_direction]);
                if (is_valid(new_point) && !nodes.Exists(n => n.Point == new_point && n.direction == new_direction))
                {
                    nodes_lst.Add(new CarNode(new_point, new_direction, node.path + 1));
                }
            }
            if (nodes_lst.Count == 2)
            {
                List <CarNode> right_node = new List <CarNode>(nodes);
                List <CarNode> left_node  = new List <CarNode>(nodes);
                right_node.Add(nodes_lst.Last());
                left_node.Add(nodes_lst.First());
                Task task = Task.Run(() => NodesThread(right_node, cantoks));
                Tasks.Add(task);
                NodesThread(left_node, cantoks);
            }
            else if (nodes_lst.Count == 1)
            {
                nodes.Add(nodes_lst.Last());
                NodesThread(nodes, cantoks);
            }
            return;
        }