Exemplo n.º 1
0
        private IDictionary<ushort, ushort> Rearrange(bool selTeam, IDictionary<ushort, Neayer> n1)
        {
            IDictionary<ushort, ushort> uidMap = new Dictionary<ushort, ushort>();
            if (selTeam)
            {
                List<int> range = new List<int>(Enumerable.Range(1, playerCapacity));
                List<int> team1 = range.Where(p => p % 2 == 1).ToList();
                team1.Shuffle();
                List<int> team2 = range.Where(p => p % 2 == 0).ToList();
                team2.Shuffle();
                Random random = new Random();
                if (random.Next() % 2 == 0)
                {
                    List<int> tmp = team1; team1 = team2; team2 = tmp;
                }

                List<ushort> hope1 = new List<ushort>();
                List<ushort> hope2 = new List<ushort>();
                List<ushort> hope0 = new List<ushort>();

                IDictionary<string, List<ushort>> dictCount = new Dictionary<string, List<ushort>>();
                foreach (var pair in n1)
                {
                    Neayer ny = pair.Value;
                    string name = "";
                    if (ny.HopeTeam == Base.Rules.RuleCode.HOPE_AKA)
                        name = "AKA";
                    else if (ny.HopeTeam == Base.Rules.RuleCode.HOPE_AO)
                        name = "AO";
                    else if (ny.HopeTeam == Base.Rules.RuleCode.HOPE_IP)
                        name = (ny.Tunnel.RemoteEndPoint as IPEndPoint).Address.ToString();
                    else
                        hope0.Add(ny.Uid);
                    if (name != "")
                    {
                        if (!dictCount.ContainsKey(name))
                            dictCount.Add(name, new List<ushort>());
                        dictCount[name].Add(ny.Uid);
                    }
                }
                //List<List<ushort>> rests = dictCount.Values.Where(p => p.Count > 1).ToList();
                var pq = new DS.PriorityQueue<List<ushort>>(new DS.ListSizeComparer<ushort>());
                foreach (var list in dictCount.Values)
                {
                    if (list.Count > 1)
                        pq.Push(list);
                    else if (list.Count == 1)
                        hope0.Add(list[0]);
                }

                while (pq.Count > 0)
                {
                    List<ushort> list = pq.Pop();
                    int a = hope1.Count, b = hope2.Count;
                    if (a + list.Count <= b)
                        hope1.AddRange(list);
                    else if (b + list.Count <= a)
                        hope2.AddRange(list);
                    else if (a <= b)
                        hope1.AddRange(list);
                    else
                        hope2.AddRange(list);
                }
                //foreach (var pair in n1)
                //{
                //    Neayer ny = pair.Value;
                //    if (ny.HopeTeam == 1)
                //        hope1.Add(ny.Uid);
                //    else if (ny.HopeTeam == 2)
                //        hope2.Add(ny.Uid);
                //    else
                //        hope0.Add(ny.Uid);
                //}
                hope1.Shuffle();
                hope2.Shuffle();
                hope0.Shuffle();

                int dif1 = hope1.Count - team1.Count;
                if (dif1 > 0)
                {
                    var removes = hope1.Take(dif1).ToList();
                    hope1.RemoveAll(p => removes.Contains(p));
                    hope0.AddRange(removes);
                }
                else if (dif1 < 0)
                {
                    var removes = hope0.Take(-dif1).ToList();
                    hope0.RemoveAll(p => removes.Contains(p));
                    hope1.AddRange(removes);
                }
                for (int i = 0; i < hope1.Count; ++i)
                    uidMap.Add(hope1[i], (ushort)team1[i]);
                int dif2 = hope2.Count - team2.Count;
                if (dif2 > 0)
                {
                    var removes = hope2.Take(dif2).ToList();
                    hope2.RemoveAll(p => removes.Contains(p));
                    hope0.AddRange(removes);
                }
                else if (dif2 < 0)
                {
                    var removes = hope0.Take(-dif2).ToList();
                    hope0.RemoveAll(p => removes.Contains(p));
                    hope2.AddRange(removes);
                }
                for (int i = 0; i < hope2.Count; ++i)
                    uidMap.Add(hope2[i], (ushort)team2[i]);
            }
            else
            {
                List<int> uidList = new List<int>(Enumerable.Range(1, playerCapacity));
                uidList.Shuffle();
                int i = 0;
                foreach (var pair in n1)
                    uidMap.Add(pair.Key, (ushort)uidList[i++]);
            }
            return uidMap;
        }
        public static int TrapRainWater2(int[][] heightMap)
        {
            int res = 0;

            int rl = heightMap.Length;

            if (rl <= 2)
            {
                return(res);
            }

            int cl = heightMap[0].Length;

            if (cl <= 2)
            {
                return(res);
            }

            bool[,] visited = new bool[rl, cl];
            DS.PriorityQueue <Cell> queue = new DS.PriorityQueue <Cell>();

            // Add all boundary cells
            for (int r = 0; r < rl; r++)
            {
                visited[r, 0]      = true;
                visited[r, cl - 1] = true;
                queue.Enqueue(new Cell(r, 0, heightMap[r][0]));
                queue.Enqueue(new Cell(r, cl - 1, heightMap[r][cl - 1]));
            }

            for (int c = 0; c < cl; c++)
            {
                visited[0, c]      = true;
                visited[rl - 1, c] = true;
                queue.Enqueue(new Cell(0, c, heightMap[0][c]));
                queue.Enqueue(new Cell(rl - 1, c, heightMap[rl - 1][c]));
            }

            int[,] dirs = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };

            //do 1 BFS (4 directions)
            while (!queue.IsEmpty())
            {
                Cell c = queue.Dequeue();
                for (int i = 0; i < dirs.GetLength(0); i++)
                {
                    int ri = c.Row + dirs[i, 0];
                    int ci = c.Col + dirs[i, 1];
                    if (ri >= 0 && ri < rl && ci >= 0 && ci < cl && !visited[ri, ci]) //validate
                    {
                        visited[ri, ci] = true;
                        //If (ri,ci) height is lower, it can hold water and the amount of water should be c.Height - heightMap[ri][ci]
                        res += Math.Max(0, c.Height - heightMap[ri][ci]);
                        // take max hieght
                        queue.Enqueue(new Cell(ri, ci, Math.Max(heightMap[ri][ci], c.Height)));
                    }
                }
            }

            return(res);
        }