Beispiel #1
0
        public int CollectMaxTax(int[] dist, int[] money, int[] carrots, int maxCarrots, int startingCarrots, out TaxAction[] collectingPlan)
        {
            collectingPlan = new TaxAction[dist.Length];


            int max = -2;

            (int, int)[,] tab = new (int, int)[dist.Length + 1, maxCarrots + 2];
        public int CollectMaxTax(int[] dist, int[] money, int[] carrots, int maxCarrots, int startingCarrots, out TaxAction[] collectingPlan)
        {
            bool ifPrint = false;

            collectingPlan = null;
            int max  = 0;
            int curr = 0;
            int prev = 0;
            var tab  = new int[dist.Length, maxCarrots + 1];

            TaxAction[] plan = new TaxAction[dist.Length];



            for (int i = 0; i < maxCarrots + 1; i++)
            {
                tab[0, i] = -1;
            }



            tab[0, startingCarrots] = money[0];
            tab[0, Math.Min(maxCarrots, startingCarrots + carrots[0])] = 0;



            for (int miasto = 1; miasto < dist.Length; miasto++) // miasta
            {
                for (int j = 0; j < maxCarrots + 1; j++)         // marchewki
                {
                    tab[miasto, j] = -1;
                }

                curr = WhichTab(miasto);
                prev = Math.Abs(curr - 1);

                for (int marchewka = 0; marchewka < maxCarrots + 1; marchewka++) // marchewki
                {
                    if (marchewka + dist[miasto] < maxCarrots + 1)               // bierzemy hajs
                    {
                        if (tab[miasto - 1, marchewka + dist[miasto]] >= 0)
                        {
                            //Array.Copy(plan[prev, j + dist[i]], 0, plan[curr, j], 0, i);
                            tab[miasto, marchewka] = tab[miasto - 1, marchewka + dist[miasto]] + money[miasto];
                            //plan[curr, j][i] = TaxAction.TakeMoney;
                            //plan2[i] = TaxAction.TakeMoney;
                        }
                    }


                    if (marchewka + dist[miasto] - carrots[miasto] < maxCarrots + 1 && marchewka - carrots[miasto] >= 0)
                    {
                        if (tab[miasto - 1, marchewka + dist[miasto] - carrots[miasto]] > tab[miasto, marchewka])
                        {
                            //Array.Copy(plan[prev, j + dist[i] - carrots[i]], 0, plan[curr, j], 0, i);
                            //plan[curr, j][i] = TaxAction.TakeCarrots;
                            tab[miasto, marchewka] = tab[miasto - 1, marchewka + dist[miasto] - carrots[miasto]];
                            //plan2[i] = TaxAction.TakeCarrots;
                        }
                    }


                    if (marchewka == maxCarrots)
                    {
                        for (int k = marchewka - carrots[miasto] + dist[miasto]; k <= maxCarrots; k++)
                        {
                            if (k >= 0 && dist[miasto] <= k)
                            {
                                if (tab[miasto - 1, k] > tab[miasto, marchewka])
                                {
                                    //Array.Copy(plan[prev, k], 0, plan[curr, j], 0, i);
                                    //plan[curr, j][i] = TaxAction.TakeCarrots;
                                    tab[miasto, marchewka] = tab[miasto - 1, k];

                                    //plan2[i] = TaxAction.TakeCarrots;
                                }
                            }
                        }
                    }
                }
            }


            max = -1;
            int indexMax = -1;

            for (int i = startingCarrots; i < maxCarrots + 1; i++)
            {
                if (max < tab[dist.Length - 1, i])
                {
                    max      = tab[dist.Length - 1, i];
                    indexMax = i;
                }
            }


            if (indexMax >= 0)
            {
                for (int miasto = dist.Length - 1; miasto > 0; miasto--)
                {
                    if (indexMax + dist[miasto] <= maxCarrots)
                    {
                        if (tab[miasto - 1, indexMax + dist[miasto]] + money[miasto] == tab[miasto, indexMax] && tab[miasto - 1, indexMax + dist[miasto]] != -1)
                        {
                            plan[miasto] = TaxAction.TakeMoney;
                            indexMax     = indexMax + dist[miasto];
                        }
                        else
                        {
                            if (indexMax < maxCarrots)
                            {
                                indexMax     = indexMax + dist[miasto] - carrots[miasto];
                                plan[miasto] = TaxAction.TakeCarrots;
                            }
                            else
                            {
                                for (int k = indexMax - carrots[miasto] + dist[miasto]; k < indexMax + dist[miasto] - 1; k++)
                                {
                                    if (k >= 0)
                                    {
                                        if (tab[miasto - 1, k] == tab[miasto, indexMax])
                                        {
                                            indexMax     = k;
                                            plan[miasto] = TaxAction.TakeCarrots;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        if (indexMax < maxCarrots)
                        {
                            indexMax     = indexMax + dist[miasto] - carrots[miasto];
                            plan[miasto] = TaxAction.TakeCarrots;
                        }
                        else
                        {
                            for (int k = indexMax + dist[miasto] - 1; k >= indexMax - carrots[miasto] + dist[miasto]; k--)
                            {
                                if (k >= 0 && k <= maxCarrots)
                                {
                                    if (tab[miasto - 1, k] == tab[miasto, indexMax])
                                    {
                                        indexMax     = k;
                                        plan[miasto] = TaxAction.TakeCarrots;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }


                if (tab[0, indexMax] == money[0])
                {
                    plan[0] = TaxAction.TakeMoney;
                }
                else
                {
                    plan[0] = TaxAction.TakeCarrots;
                }
                //PrintPlan(collectingPlan);
            }

            //plan2 = collectingPlan;
            if (ifPrint)
            {
                PrintPlan(plan);
            }
            if (max >= 0)
            {
                collectingPlan = plan;
            }
            return(max);
        }
Beispiel #3
0
        public int CollectMaxTax(int[] dist, int[] money, int[] carrots, int maxCarrots, int startingCarrots, out TaxAction[] collectingPlan)
        {
            int n = money.Length;

            collectingPlan = new TaxAction[n];
            int max  = -1;
            int zas2 = maxCarrots + 1;

            int[,] dynam        = new int[n, zas2];
            TaxAction[,] plany  = new TaxAction[n, zas2];
            int[,] ilemarchewek = new int[n, zas2];



            for (int j = 0; j < n; j++)
            {
                for (int i = 0; i < zas2; i++)
                {
                    dynam[j, i] = -1;
                }
            }
            dynam[0, startingCarrots] = money[0];
            plany[0, startingCarrots] = TaxAction.TakeMoney;
            int oby = startingCarrots + carrots[0];

            if (oby >= zas2)
            {
                oby = maxCarrots;
            }
            dynam[0, oby]        = 0;
            plany[0, oby]        = TaxAction.TakeCarrots;
            ilemarchewek[0, oby] = oby - startingCarrots;
            for (int i = 1; i < n; i++)
            {
                int iii = i - 1;
                for (int j = 1; j < zas2; j++)
                {
                    if (dynam[iii, j] < 0)
                    {
                        continue;
                    }
                    int nac = j - dist[i];

                    if (nac < 0)
                    {
                        continue;
                    }

                    int nac7 = dynam[iii, j] + money[i];


                    int zas = nac + carrots[i];
                    if (dynam[i, nac] < nac7)
                    {
                        plany[i, nac] = TaxAction.TakeMoney;
                        dynam[i, nac] = nac7;
                    }
                    if (zas >= zas2)
                    {
                        zas = maxCarrots;
                    }
                    if (dynam[iii, j] > dynam[i, zas])
                    {
                        plany[i, zas]        = TaxAction.TakeCarrots;
                        dynam[i, zas]        = dynam[iii, j];
                        ilemarchewek[i, zas] = zas - nac;
                    }
                }
            }
            int indMax = -1;

            for (int j = startingCarrots; j < zas2; j++)
            {
                if (dynam[n - 1, j] > max)
                {
                    indMax = j;
                    max    = dynam[n - 1, j];
                }
            }
            if (max == -1)
            {
                collectingPlan = null;
                return(max);
            }
            for (int i = n - 1; i >= 0; i--)
            {
                collectingPlan[i] = plany[i, indMax];
                indMax           += dist[i];
                if (collectingPlan[i] == TaxAction.TakeCarrots)
                {
                    indMax = indMax - ilemarchewek[i, indMax - dist[i]];
                }
            }
            return(max);
        }
        public int CollectMaxTax(int[] dist, int[] money, int[] carrots, int maxCarrots, int startingCarrots, out TaxAction[] collectingPlan)
        {
            collectingPlan = new TaxAction[dist.Length];
            int max = -1;

            int[,] tab = new int[dist.Length + 1, maxCarrots + 1];
            int mar = startingCarrots;

            for (int i = 0; i <= dist.Length; i++)
            {
                for (int j = 0; j <= maxCarrots; j++)
                {
                    tab[i, j] = -1;
                }
            }
            tab[0, startingCarrots] = 0;
            TaxAction[,] tx         = new TaxAction[dist.Length + 1, maxCarrots + 1];
            for (int i = 0; i <= dist.Length; i++)
            {
                for (int j = 0; j <= maxCarrots; j++)
                {
                    tx[i, j] = TaxAction.Empty;
                }
            }
            int[,] cr = new int[dist.Length + 1, maxCarrots + 1];
            for (int i = 0; i <= dist.Length; i++)
            {
                for (int j = 0; j <= maxCarrots; j++)
                {
                    cr[i, j] = -1;
                }
            }
            cr[0, startingCarrots] = startingCarrots;
            for (int i = 0; i < dist.Length; i++)
            {
                if (i == dist.Length - 1)
                {
                    for (int j = 0; j < startingCarrots; j++)
                    {
                        if (tab[i, j] != -1)
                        {
                            mar = j + carrots[i];
                            if (mar > maxCarrots)
                            {
                                mar = maxCarrots;
                            }
                            if (mar >= startingCarrots)
                            {
                                if (tab[i, mar] < tab[i, j])
                                {
                                    tab[i + 1, mar] = tab[i, j];
                                    tx[i + 1, mar]  = TaxAction.TakeCarrots;
                                    cr[i + 1, mar]  = j;
                                }
                                else
                                {
                                    tab[i + 1, mar] = tab[i, mar];
                                    tx[i + 1, mar]  = tx[i, mar];
                                    cr[i + 1, mar]  = mar;
                                }
                            }
                        }
                    }
                    for (int j = startingCarrots; j <= maxCarrots; j++)
                    {
                        if (tab[i, j] != -1)
                        {
                            if (tab[i + 1, j] < tab[i, j] + money[i])
                            {
                                tab[i + 1, j] = tab[i, j] + money[i];
                                tx[i + 1, j]  = TaxAction.TakeMoney;
                                cr[i + 1, j]  = j;
                            }
                        }
                    }
                }
                else
                {
                    for (int j = 0; j <= maxCarrots; j++)
                    {
                        if (tab[i, j] != -1)
                        {
                            mar = j + carrots[i];
                            if (mar > maxCarrots)
                            {
                                mar = maxCarrots;
                            }
                            if (mar - dist[i + 1] < 0)
                            {
                                continue;
                            }
                            if (tab[i + 1, mar - dist[i + 1]] < tab[i, j])
                            {
                                tab[i + 1, mar - dist[i + 1]] = tab[i, j];
                                tx[i + 1, mar - dist[i + 1]]  = TaxAction.TakeCarrots;
                                cr[i + 1, mar - dist[i + 1]]  = j;
                            }
                            if (j - dist[i + 1] < 0)
                            {
                                continue;
                            }
                            if (tab[i + 1, j - dist[i + 1]] < tab[i, j] + money[i])
                            {
                                tab[i + 1, j - dist[i + 1]] = tab[i, j] + money[i];
                                tx[i + 1, j - dist[i + 1]]  = TaxAction.TakeMoney;
                                cr[i + 1, j - dist[i + 1]]  = j;
                            }
                        }
                    }
                }
            }
            mar = startingCarrots;
            for (int i = startingCarrots; i <= maxCarrots; i++)
            {
                if (tab[dist.Length, i] > max)
                {
                    max = tab[dist.Length, i];
                    mar = i;
                }
            }
            if (max == -1)
            {
                collectingPlan = null;
                return(max);
            }
            for (int i = dist.Length; i > 0; i--)
            {
                collectingPlan[i - 1] = tx[i, mar];
                mar = cr[i, mar];
            }
            return(max);
        }