示例#1
0
 private void InitializeGrid()
 {
     gridDrawer = new GridDrawer.GridDrawer(mainGrid);
     gridDrawer.Draw();
 }
示例#2
0
 private void InitializeGrid(int verticalCells, int horizontalCells)
 {
     gridDrawer = new GridDrawer.GridDrawer(mainGrid, verticalCells, horizontalCells);
     gridDrawer.Draw();
 }
示例#3
0
        private void runAlgoButton_Click(object sender, EventArgs e)
        {
            if (languageComboBox.SelectedItem == null)
            {
                MessageBox.Show("No selected algorithm", "Select algorithm", MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);
                return;
            }

            if (gridDrawer.startCell == null || gridDrawer.startCell.type != CellType.A)
            {
                MessageBox.Show("There is no starting point selected", "Select start point", MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);
                return;
            }

            if (gridDrawer.endCell == null || gridDrawer.endCell.type != CellType.B)
            {
                MessageBox.Show("There is no target point selected", "Select target point", MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);
                return;
            }

            var width  = gridDrawer.Grid.CellObjects.GetLength(0);
            var height = gridDrawer.Grid.CellObjects.GetLength(1);


            int number_of_nodes = height * width;


            printLog($"Converting Maze to Graph");

            int[,] graph = convertGridToGraph(gridDrawer.Grid.CellObjects);


            int startNode = gridDrawer.startCell.coords.y * gridDrawer.HorizontalCells + gridDrawer.startCell.coords.x;
            int endNode   = gridDrawer.endCell.coords.y * gridDrawer.HorizontalCells + gridDrawer.endCell.coords.x;


            int[,] cost = new int[number_of_nodes, number_of_nodes];
            int[] distance        = new int[number_of_nodes];
            int[] predecessor     = new int[number_of_nodes];
            int[] visitedVertices = new int[number_of_nodes];


            int result;

            clearSolBtn_Click(null, null);

            printLog($"Run the algorithm: {languageComboBox.SelectedItem.ToString()}");

            watch.Reset();
            watch.Start();

            if (languageComboBox.SelectedIndex == 0)
            {
                result = asmProxy.executeDijkstraAsm(graph, number_of_nodes, startNode, cost, distance, predecessor, visitedVertices);
            }
            else
            {
                result = asmProxy.executeDijkstraC(graph, number_of_nodes, startNode, cost, distance, predecessor, visitedVertices);
            }

            watch.Stop();

            printLog($"Execution Time: {watch.ElapsedMilliseconds} ms");

            watch.Reset();

            if (endNode != startNode)
            {
                var colStartNode = (startNode / width) + 1;
                var rowStartNode = (startNode % width) + 1;

                var colEndNode = (endNode / width) + 1;
                var rowEndNode = (endNode % width) + 1;

                if (distance[endNode] != INFINITY)
                {
                    printLog($"Distance form start node: {startNode} ({rowStartNode}, {colStartNode}) to target node: {endNode} ({rowEndNode}, {colEndNode}) is equal {distance[endNode]}");
                }
                else
                {
                    printLog($"There is no path between the selected points");
                }

                var j = endNode;
                do
                {
                    j = predecessor[j];
                    var col = (j / width);
                    var row = (j % width);
                    if (j != startNode)
                    {
                        gridDrawer.setCellWithoutUpdate(row, col, CellType.Path);
                    }
                } while (j != startNode);
                gridDrawer.Draw();
            }
        }