Exemplo n.º 1
0
Arquivo: Dinic.cs Projeto: nojima/GMF
        public static double MaximumFlow(
            DirectedGraph graph, double[] cap, int s, int t, out double[] flow)
        {
            if (s == t) {
                throw new ArgumentException("始点と終点が同じ頂点です.");
            }

            int n = graph.Vertices.Count;
            int m = graph.Edges.Count;
            flow = new double[m];

            double total = 0;
            for (bool cont = true; cont; ) {
                cont = false;

                var level = new int[n];
                for (int i = 0; i < n; ++i)
                    level[i] = -1;
                level[s] = 0;

                var Q = new Queue<int>();
                Q.Enqueue(s);

                for (int d = n; Q.Count > 0 && level[Q.Peek()] < d; ) {
                    int u = Q.Dequeue();
                    if (u == t) d = level[u];
                    foreach (var e in graph.OutEdges[u]) {
                        if (cap[e.Index] - flow[e.Index] >= SMALL && level[e.Dst] == -1) {
                            Q.Enqueue(e.Dst);
                            level[e.Dst] = level[u] + 1;
                        }
                    }
                    foreach (var e in graph.InEdges[u]) {
                        if (flow[e.Index] >= SMALL && level[e.Dst] == -1) {
                            Q.Enqueue(e.Dst);
                            level[e.Dst] = level[u] + 1;
                        }
                    }
                }

                var finished = new bool[n];
                for (double f = 1; f > 0; ) {
                    f = Augment(graph, cap, flow, level, finished, s, t, double.PositiveInfinity);
                    if (f < SMALL) break;
                    total += f;
                    cont = true;
                }
            }
            return total;
        }
Exemplo n.º 2
0
    public TurnManager(PathFinder p, List<Unit> ally, List<Unit> enemy)
    {
        pathFinder = p;
        ai = new UnitAI(pathFinder);

        playerUnits = ally;
        aiUnits = enemy;

        NextTurn = UIManager.getUIManager().getNextTurnButton();
        Cancel = UIManager.getUIManager().getCancelButton();
        Wait = UIManager.getUIManager().getWaitButton();

        selectionState = State.OurTurnNoSelection;
        UIManager.getUIManager().ChangeButtonState(selectionState);

        toActAI = new Queue<Unit>();
    }
Exemplo n.º 3
0
    private KeyValuePair<Result, UnitAction> FindUnit()
    {
        Predicate<Node> findEn = (Node q) =>
        {
            bool isOccupied = q.Occupied;
            if (!isOccupied)
                return false;
            if (q.Occupier.isEnemy() != subjectRef.isEnemy())
                return true;
            return false;
        };

        Node closestEnemyNode = pathManager.getClosestNode(subjectRef, findEn);
        if (closestEnemyNode == null)
            return new KeyValuePair<Result, UnitAction>(Result.NothingFound, null);

        Queue<Node> path = new Queue<Node>();
        pathFinderRef.AStar(path, subjectRef.getNode(), closestEnemyNode, closestEnemyNode);
        
        Node goTo = subjectRef.getNode();

        // go to farthest path.
        while (path.Count > 0 && pathManager.canWalkTo(subjectRef, path.Peek()))
            goTo = path.Dequeue();

        UnitAction moveCloserToClosestEnemy = new UnitAction(subjectRef, null, goTo);

        return new KeyValuePair<Result, UnitAction>(Result.Success, moveCloserToClosestEnemy);
    }