コード例 #1
0
        public void solve(Graph g, int start, int hide, List <int> listvisit, int option, List <int>[] listsolver)

        /* Fungsi DFS untuk menyelesaikan permasalahan,
         * memiliki parameter sebuah graf, sebuah integer dimana simpul awal berada
         * sebuah integer dimana simpul terakhir berada, list of integer simpul yang dikunjungi
         * sebuah integer option dari pengguna, dan array of list untuk jalan tercepat menuju simpul
         * yang ingin dikunjungi */
        {
            listvisit.Add(start);

            if (start == hide)
            {
                isFound = true;
            }
            else
            {
                isFound = false;
            }

            foreach (int i in g.getListHouse(start))
            {
                if (!isVisited(listvisit, i) && !isFound)
                {
                    if (option == 0)
                    {
                        if (g.getWeight(start) >= g.getWeight(i))
                        {
                            listsolver[i].Add(start);
                            solve(g, i, hide, listvisit, 0, listsolver);
                        }
                    }
                    else
                    {
                        if (g.getWeight(start) <= g.getWeight(i))
                        {
                            listsolver[i].Add(start);
                            solve(g, i, hide, listvisit, 1, listsolver);
                        }
                    }
                }
            }
        }
コード例 #2
0
ファイル: HnS.cs プロジェクト: pandyakaa/HideAndSeek
        public void solve(Graph g, int start, int hide, List <int> listvisit, int option, List <int>[] listsolver)
        /* Fungsi DFS untuk menyelesaikan permasalahan */
        {
            listvisit.Add(start);

            if (start == hide)
            {
                isFound = true;
            }
            else
            {
                isFound = false;
            }

            foreach (int i in g.getListHouse(start))
            {
                if (!isVisited(listvisit, i) && !isFound)
                {
                    if (option == 0)
                    {
                        if (g.getWeight(start) >= g.getWeight(i))
                        {
                            listsolver[i].Add(start);
                            solve(g, i, hide, listvisit, 0, listsolver);
                        }
                    }
                    else
                    {
                        if (g.getWeight(start) <= g.getWeight(i))
                        {
                            listsolver[i].Add(start);
                            solve(g, i, hide, listvisit, 1, listsolver);
                        }
                    }
                }
            }
        }
コード例 #3
0
        private void drawMap(HnS hideAndSeek)
        {
            Graph h          = hideAndSeek.getGraphHnS();
            int   houseCount = h.getHouseCount();

            //Making a List of house per Weight
            int maxWeight = h.getMaxWeight();

            List <int>[] listPerWeight = new List <int> [maxWeight + 1];
            for (int i = 0; i <= maxWeight; i++)
            {
                listPerWeight[i] = new List <int>();
            }
            for (int house = 1; house <= houseCount; house++)
            {
                int idx = h.getWeight(house);
                listPerWeight[idx].Add(house);
            }

            //2D Array of point
            //use   : storing left and right point of ellipse(s) horizontal radius
            int[,] matrixOfPoints = new int[houseCount + 1, 5];

            //colors
            Brush ellipseCol = (Brush)(new BrushConverter().ConvertFrom("#C70039"));
            Brush lineCol    = (Brush)(new BrushConverter().ConvertFrom("#787878"));

            //Relative position of grid (circle and label)
            int x   = 50;
            int y   = 50;
            int rad = 50;

            //drawing houses
            for (int weight = 0; weight <= maxWeight; weight++)
            {
                y = 50;
                foreach (int house in listPerWeight[weight])
                {
                    //x1,y1,x2,y2
                    matrixOfPoints[house, 1] = x;
                    matrixOfPoints[house, 2] = y + (rad / 2);
                    matrixOfPoints[house, 3] = x + rad;
                    matrixOfPoints[house, 4] = y + (rad / 2);

                    string label       = house.ToString();
                    Grid   ellipseGrid = new Grid();

                    //Create ellipse and textboxt as house representation
                    ellipseGrid.Children.Add(new Ellipse()
                    {
                        Name   = "ellipse" + house,
                        Width  = rad,
                        Height = rad,
                        Fill   = ellipseCol
                    });

                    ellipseGrid.Children.Add(new TextBlock()
                    {
                        HorizontalAlignment = HorizontalAlignment.Center,
                        VerticalAlignment   = VerticalAlignment.Center,
                        Text       = label,
                        Foreground = Brushes.White
                    });


                    mainCanvas.Children.Add(ellipseGrid);
                    //Grid Location
                    Canvas.SetTop(ellipseGrid, y);
                    Canvas.SetLeft(ellipseGrid, x);
                    Canvas.SetZIndex(ellipseGrid, 2);

                    y += 100;
                }
                x += 100;
            }

            //drawing lines
            for (int house = 1; house <= houseCount; house++)
            {
                //get adjacency list
                List <int> listHouse = h.getListHouse(house);
                int        xgap      = 20;

                Grid lineGrid = new Grid();

                foreach (int neighbor in listHouse)
                {
                    if (house < neighbor)
                    {
                        lineGrid.Children.Add(new Line()
                        {
                            //create line with starting point in house and end point in neighbor
                            StrokeThickness = 4,
                            Stroke          = lineCol,
                            X1 = matrixOfPoints[house, 3] - xgap,
                            Y1 = matrixOfPoints[house, 4],
                            X2 = matrixOfPoints[neighbor, 1] + xgap,
                            Y2 = matrixOfPoints[neighbor, 2]
                        });
                    }
                }

                //Bring to back
                Canvas.SetZIndex(lineGrid, 1);
                mainCanvas.Children.Add(lineGrid);
            }
        }
コード例 #4
0
        private void drawMap(Graph h)
        {
            int houseCount = h.getHouseCount();

            //Making a List of house per Weight
            int maxWeight = h.getMaxWeight();

            List <int>[] listPerWeight = new List <int> [maxWeight + 1];
            for (int i = 0; i <= maxWeight; i++)
            {
                listPerWeight[i] = new List <int>();
            }
            for (int house = 1; house <= houseCount; house++)
            {
                int idx = h.getWeight(house);
                listPerWeight[idx].Add(house);
            }

            //2D Array of point
            //use   : storing left and right point of ellipse(s) horizontal radius
            int[,] matrixOfPoints = new int[houseCount + 1, 5];

            //colors
            Brush ellipseCol = (Brush)(new BrushConverter().ConvertFrom("#C70039"));
            Brush lineCol    = (Brush)(new BrushConverter().ConvertFrom("#787878"));

            //Relative position of grid (circle and label)
            int x   = 50;
            int y   = 50;
            int rad = 50;

            //drawing houses
            for (int weight = 0; weight <= maxWeight; weight++)
            {
                y = 50;
                foreach (int house in listPerWeight[weight])
                {
                    //x1,y1,x2,y2
                    matrixOfPoints[house, 1] = x;
                    matrixOfPoints[house, 2] = y + (rad / 2);
                    matrixOfPoints[house, 3] = x + rad;
                    matrixOfPoints[house, 4] = y + (rad / 2);

                    string label       = house.ToString();
                    Grid   ellipseGrid = new Grid();

                    ellipseGrid.Children.Add(new Ellipse()
                    {
                        Width  = rad,
                        Height = rad,
                        Fill   = ellipseCol
                    });

                    ellipseGrid.Children.Add(new TextBlock()
                    {
                        HorizontalAlignment = HorizontalAlignment.Center,
                        VerticalAlignment   = VerticalAlignment.Center,
                        Text       = label,
                        Foreground = Brushes.White
                    });

                    //draw coordinates, erase if not used
                    //tempGrid.Children.Add(new TextBlock()
                    //{
                    //HorizontalAlignment = HorizontalAlignment.Right,
                    //VerticalAlignment = VerticalAlignment.Top,
                    //Text = matrixOfPoints[item,1] +","+ matrixOfPoints[item, 2]+"-"+ matrixOfPoints[item, 3] + "," + matrixOfPoints[item, 4],
                    //Foreground = Brushes.Black
                    //});

                    //adding Grid to canvas
                    mainCanvas.Children.Add(ellipseGrid);
                    //Grid Location
                    Canvas.SetTop(ellipseGrid, y);
                    Canvas.SetLeft(ellipseGrid, x);
                    Canvas.SetZIndex(ellipseGrid, 2);

                    y += 100;
                }
                x += 100;
            }

            //drawing lines
            for (int house = 1; house <= houseCount; house++)
            {
                //get adjacency list
                List <int> listHouse = h.getListHouse(house);
                int        xgap      = 20;

                Grid lineGrid = new Grid();

                foreach (int neighbor in listHouse)
                {
                    if (house < neighbor)
                    {
                        lineGrid.Children.Add(new Line()
                        {
                            StrokeThickness = 4,
                            Stroke          = lineCol,
                            X1 = matrixOfPoints[house, 3],
                            Y1 = matrixOfPoints[house, 4],
                            X2 = matrixOfPoints[neighbor, 1] + xgap,
                            Y2 = matrixOfPoints[neighbor, 2]
                        });
                    }
                }

                //Bring to back
                Canvas.SetZIndex(lineGrid, 1);
                mainCanvas.Children.Add(lineGrid);
            }
        }