Exemplo n.º 1
0
        private int Solve()
        {
            Square current = new Square(new int[] {0, 0}, 1, 0);
            BinaryTree<Square> store = new BinaryTree<Square>();
            store.Add(current);

            double targetSize = 0;

            while(!store.Empty()) {
                current = store.PopMax();

                int[] curentCoordinates = current.GetCoordinates();
                if(curentCoordinates[0] == coordinates[0] && curentCoordinates[1] == coordinates[1]) {
                    targetSize = current.GetSize();
                }

                Square[] children = current.GetChildren();
                foreach (Square child in children) {
                    int[] childCoordinates = child.GetCoordinates();
                    if(childCoordinates[0] <= coordinates[0] && childCoordinates[1] <= coordinates[1]) {
                        store.Add(child);
                    }
                }
            }

            int count = 0;
            int result = -1;

            current = new Square(new int[] { 0, 0 }, 1, 0);
            store.Add(current);

            while (!store.Empty()) {
                count++;
                current = store.PopMax();

                int[] curentCoordinates = current.GetCoordinates();
                if (curentCoordinates[0] == coordinates[0] && curentCoordinates[1] == coordinates[1]) {
                    result = count;
                }

                Square[] children = current.GetChildren();
                for (int i = 0; i < children.Length; i++) {
                    if (targetSize <= children[i].GetSize()) {
                        store.Add(children[i]);
                    }
                }
            }

            return result;
        }