public bool Insert(SquareUnit unit)
 {
     if (!IsInsertValid(this, unit))
     {
         return(false);
     }
     units.Add(unit);
     return(true);
 }
        private static bool IsInsertValid(SquareGrid grid, SquareUnit unit)
        {
            List <(int, int)> existCells = grid.units.SelectMany(n => n.GetCells()).ToList();
            List <(int, int)> newCells   = unit.GetCells();

            // 중첩 존재
            var intersect = existCells.Intersect(newCells);

            if (intersect.Count() != 0)
            {
                return(false);
            }

            // 범위 초과
            if (unit.GetCells().Exists(n => grid.XCount <= n.Item1) ||
                unit.GetCells().Exists(n => grid.YCount <= n.Item2))
            {
                return(false);
            }

            return(true);
        }
Пример #3
0
        /// <summary>
        /// 4 ,2, 1 mag의 유닛이 각각 1/3의 면적을 확보하도록 논리전개
        /// </summary>
        /// <param name="grid"></param>
        public void SimplePack1(ref SquareGrid grid)
        {
            var r = new Random();

            var mags          = new int[] { 4, 3, 2 };
            int wholeArea     = grid.XCount * grid.YCount;
            int evenDivedArea = wholeArea / (mags.Length + 1);
            var iters         = mags.Select(n => (int)(evenDivedArea / Math.Pow(n, 2))).ToArray();

            for (int i = 0; i < mags.Length; i++)
            {
                int mag  = mags[i];
                int iter = iters[i];
                for (int j = 0; j < iter; j++)
                {
                    while (true)
                    {
                        int        x  = r.Next(grid.XCount);
                        int        y  = r.Next(grid.YCount);
                        SquareUnit ug = new SquareUnit(x, y, mag);
                        if (grid.Insert(ug))
                        {
                            break;
                        }
                    }
                }
            }
            foreach (var item in grid.GetVoidCellList())
            {
                int x = item.Item1;
                int y = item.Item2;
                if (!grid.Insert(new SquareUnit(x, y, 1)))
                {
                    throw new Exception();
                }
            }
        }