• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            SoRoMan

            人若無名,便可專心練劍.

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              12 隨筆 :: 1 文章 :: 41 評論 :: 0 Trackbacks

            今天看到一個網友在談論一個關于The Tower of Babylon的, 見http://www.shnenglu.com/sicheng/archive/2006/08/09/11024.html,感覺其實就是個按關鍵字面積插入排序的問題。于是用插入排序算法實現如下。

            簡單測試了下,應該沒問題。
            We have 4 kinds of blocks, they are:
            (1,1,1)
            (1,2,3)
            (2,3,4)
            (2,3,2)
            Building the tower of babylon...
            The max height of tower is: 18.
            Totally 7 blocks are used, they are:
            (1) block (1,1,1), area = 1
            (2) block (1,2,3), area = 2
            (3) block (1,2,3), area = 3
            (4) block (2,3,2), area = 4
            (5) block (2,3,4), area = 6
            (6) block (2,3,4), area = 8
            (7) block (2,3,4), area = 12

            ------------------------------------
            TowerOfBabylon.cpp:
            ------------------------------------
            //
            // File: [TowerOfBabylon.cpp]
            // Description:[This program illustrates how to get the Tower Of Babylon. the Tower Of Babylon: Given N kinds of blocks,
            // you need to buid the tower under the condition that every upper block's area should less than the lower one.]
            // Author:[SoRoMan]
            // Date:[2006-08-08]
            // Version:[2.0]
            // History: [1.0: initial draft.
            // 2.0: add a new type TOWER
            // 3.0: chnage the sequence data structure to list structure to avoid memory access violation.]
            //

            // INCLUDES //////////////////////////////////////////////////////////////////////////
            #include "stdio.h"
            #include "malloc.h"

            // DEFINES //////////////////////////////////////////////////////////////////////////
            #define N 4

            // TYPES //////////////////////////////////////////////////////////////////////////
            typedef struct BLOCK_TYP
            {
            ?int x,y,z;
            ?int area;
            ?int height;
            ?
            ?BLOCK_TYP *pNext;
            } BLOCK, *BLOCK_PTR;

            typedef struct TOWER_TYP
            {
            ?int height;
            ?int num_block;

            ?BLOCK_PTR pTowerTop;
            } TOWER, *TOWER_PTR;

            // PROTOTYPES //////////////////////////////////////////////////////////////////////////
            TOWER_PTR BuildTower(BLOCK_PTR pBlock, int n);

            // FUNCTIONS //////////////////////////////////////////////////////////////////////////
            int main(int argc, wchar_t* argv[])
            {
            ?BLOCK blocks[N] = {{1,1,1},{2,2,2},{3,3,3},{4,4,4}};

            ?printf("We have %d kinds of blocks, they are:\n", N);
            ?int i;
            ?for(i = 0; i < N; i++)
            ?{
            ? printf("(%d,%d,%d)\n", blocks[i].x, blocks[i].y, blocks[i].z);
            ?}
            ?printf("Building the tower of babylon...\n");

            ?TOWER_PTR pT = BuildTower(blocks, N);
            ?printf("The max height of tower is: %d.\nTotally %d blocks are used, they are:\n", pT->height, pT->num_block );

            ?BLOCK_PTR pBlock = pT->pTowerTop;
            ?for(i = 0; i < pT->num_block; i++)
            ?{
            ??????? printf("(%d) block (%d,%d,%d), area = %d, height = %d\n", i+1, pBlock->x, pBlock->y, pBlock->z, pBlock->area, pBlock->height);?
            ??pBlock = pBlock->pNext;
            ?}

            ?getchar();

            ?return 0;
            }

            ////////////////////////////////////////////////////////////////////////////////
            // Algorithm of building the Tower Of Babylon.
            // Input Parameters: pBlock: pointer variable that identifies blocks sequence.
            // n: int variable that identifies how many kinds of blocks.
            // Output Parameters: None.
            // Return: pointer variable that identifies the built tower.
            ////////////////////////////////////////////////////////////////////////////////
            TOWER_PTR BuildTower(BLOCK_PTR pBlock, int n)
            {
            ?int index_block = 0;
            ?TOWER_PTR pTower = new TOWER();
            ?BLOCK_PTR pTop = new BLOCK();?
            ?pTower->pTowerTop = pTop;

            ?// initialize tower
            ?pTower->pTowerTop->x = pBlock->x;
            ?pTower->pTowerTop->y = pBlock->y;
            ?pTower->pTowerTop->z = pBlock->z;

            ?pTower->pTowerTop->area = (pBlock->x)*(pBlock->y);
            ?pTower->pTowerTop->height = pBlock->z;
            ?pTower->height = pTower->pTowerTop->height;
            ?pTower->num_block = 1;
            ???
            ?for(int i = 1; i < 3*n; i++)
            ?{
            ? index_block = i/3;
            ? if (i%3 == 0) // condition 1, area = x*y, height = z.
            ? {
            ?? (pBlock+index_block)->area = ((pBlock+index_block)->x)*((pBlock+index_block)->y);
            ?? (pBlock+index_block)->height = (pBlock+index_block)->z;
            ? }
            ? else if (i%3 == 1) // condition 2, area = x*z, height = y.
            ? {
            ?? (pBlock+index_block)->area = ((pBlock+index_block)->x)*((pBlock+index_block)->z);
            ?? (pBlock+index_block)->height = (pBlock+index_block)->y;
            ? }
            ? else // condition 3, area = y*z, height = z.
            ? {
            ?? (pBlock+index_block)->area = ((pBlock+index_block)->y)*((pBlock+index_block)->z);
            ?? (pBlock+index_block)->height = (pBlock+index_block)->x;
            ? }
            ?
            ? bool bNeedToBeAdded = true;?

            ? BLOCK_PTR pB = pTower->pTowerTop;
            ? BLOCK_PTR pPrev = pTower->pTowerTop;
            ? while(pB != NULL)
            ? {
            ?? if ((pBlock+index_block)->area < (pB->area))
            ?? {
            ?// insert new block
            ?BLOCK_PTR pNewBlock = new BLOCK();?
            ??? *pNewBlock = *(pBlock+index_block);

            ?if(pB == pPrev)
            ?{
            ??pNewBlock->pNext = pB;
            ??pTower->pTowerTop = pNewBlock;
            ?}
            ?else
            ?{
            ??pNewBlock->pNext = pPrev->pNext;
            ??pPrev->pNext = pNewBlock;
            ?}
            ?
            ??? // increase number of blocks
            ??? pTower->num_block++;
            ??? // increase height of tower
            ??? pTower->height += (pBlock+index_block)->height;
            ???
            ??? bNeedToBeAdded = false;
            ??? break;
            ?? }
            ?? else if ((pBlock+index_block)->area == (pB->area))
            ?? {
            ??? if (pB->height < ((pBlock+index_block)->height))
            ??? {
            ???? // increase height of tower
            ???? pTower->height += (pBlock+index_block)->height - pB->height;
            ???? // replace blocks
            ? BLOCK_PTR pNewBlock = new BLOCK();?
            ? *pNewBlock = *(pBlock+index_block);

            ? if (pB == pPrev)
            ? {
            ??pNewBlock->pNext = pB->pNext;
            ??pTower->pTowerTop = pNewBlock;
            ? }
            ? else
            ? {
            ?? pNewBlock->pNext = pB->pNext;
            ?? pPrev->pNext = pNewBlock;
            ? }
            ??? }

            ??? bNeedToBeAdded = false;
            ??? break;
            ?? }

            ?? pPrev = pB;
            ?? pB = pB->pNext;
            ? }
            ?
            ? if(bNeedToBeAdded)
            ? {
            ?? // add new block at the end
            ?? BLOCK_PTR pNewBlock = new BLOCK();?
            ?? *pNewBlock = *(pBlock+index_block);
            ??
            ?? pPrev->pNext = pNewBlock;
            ?? pNewBlock->pNext = NULL;

            ?? // increase number of blocks
            ?? pTower->num_block++;
            ?? // increase height of tower
            ?? pTower->height += (pBlock+index_block)->height;
            ? }
            ?}

            ?return pTower;
            }

            ?

            ?

            ?

            ?

            posted on 2006-08-09 17:32 SoRoMan 閱讀(2596) 評論(2)  編輯 收藏 引用

            評論

            # re: 思考:關于The Tower of Babylon 2006-08-09 19:14 sicheng
            非常感謝您對這道題的關注,甚至還為此寫出了完整的程序。
            程序寫的很漂亮,非常感謝。
            由于本人的疏忽 題目描述地不是很清楚,所以特此也把整個原題貼出來http://www.shnenglu.com/sicheng/archive/2006/08/09/11024.html
            我給您留了信息 希望能交個朋友  回復  更多評論
              

            # re: 思考:關于The Tower of Babylon 2011-07-25 14:12 Sleepy
            表示直接CRUSH了。
            僅用面積來算的話是不對的,你想想1X100的石頭和50X50的石頭該如何放。

            互相探討學習一下,我的代碼(用到VECTOR VC6.0):

            // The Tower of Babylon.cpp : Defines the entry point for the console application.
            //

            #include "stdafx.h"
            #include <stdio.h>

            #include <vector>
            using namespace std;

            class Block
            {
            public:
            int x;
            int y;
            int z;

            public:
            Block(int X, int Y, int Z) : x(X) , y(Y), z(Z) { };

            inline int GetHeight() const { return z; }

            bool CanOverlapped(const Block& block) const
            { return (x < block.x && y < block.y) || (x < block.y && y < block.x); }
            };

            typedef vector< Block > BLOCKVEC;

            void InserByOverlap(BLOCKVEC& vecBlock, const Block& block)
            {
            BLOCKVEC::iterator pos;
            for (pos = vecBlock.begin(); pos != vecBlock.end(); pos++)
            {
            if ((*pos).CanOverlapped(block))
            {
            break;
            }
            }

            vecBlock.insert(pos, block);
            }

            void AddItem(BLOCKVEC& vecBlock, int x, int y, int z)
            {
            InserByOverlap(vecBlock, Block(x, y, z));
            InserByOverlap(vecBlock, Block(y, z, x));
            InserByOverlap(vecBlock, Block(z, x, y));
            }

            void RecursiveCalcHegiht(const BLOCKVEC& Blocks, const Block& BlockPre, int nIndex,
            int nHeghtPre, int& nHeightest, BLOCKVEC BlockStack, BLOCKVEC& BlockOverlap)
            {
            for (int i = nIndex; i < Blocks.size(); i++)
            {
            if (Blocks[i].CanOverlapped(BlockPre))
            {
            BlockStack.push_back(Blocks[i]);
            RecursiveCalcHegiht(Blocks, Blocks[i], i + 1, nHeghtPre + Blocks[i].GetHeight(), nHeightest, BlockStack, BlockOverlap);
            BlockStack.pop_back();
            }
            }

            if (nHeghtPre > nHeightest)
            {
            nHeightest = nHeghtPre;
            BlockOverlap = BlockStack;
            }
            }

            int main(int argc, char* argv[])
            {
            int nCount;
            int x, y, z;
            BLOCKVEC vecBlock;

            scanf("%d", &nCount);

            while(nCount--)
            {
            scanf("%d %d %d", &x, &y, &z);

            AddItem(vecBlock, x, y, z);
            }

            int nHeightest = 0;
            BLOCKVEC BlockOverlap;
            Block BlockBase(INT_MAX, INT_MAX, INT_MAX);

            RecursiveCalcHegiht(vecBlock, BlockBase, 0, 0, nHeightest, BlockOverlap, BlockOverlap);

            printf("Heightst : %d\n", nHeightest);

            BLOCKVEC::reverse_iterator pos;
            for (pos = BlockOverlap.rbegin(); pos != BlockOverlap.rend(); pos++)
            {
            printf("%d, %d, %d \n", (*pos).x, (*pos).y, (*pos).z);
            }

            return 0;
            }


            可以考慮用這里的數據來測試:
            http://poj.org/problem?id=2241  回復  更多評論
              

            国产精品免费久久| 欧美精品一区二区精品久久| 久久无码国产| 久久亚洲中文字幕精品有坂深雪 | 日批日出水久久亚洲精品tv| 一97日本道伊人久久综合影院| 久久se精品一区二区| 久久亚洲AV成人无码电影| 久久se精品一区精品二区| 色综合久久88色综合天天 | 欧美丰满熟妇BBB久久久| 久久99精品九九九久久婷婷| 亚洲综合婷婷久久| 国产精自产拍久久久久久蜜| 久久久国产打桩机| 国产成人精品久久| 国产A级毛片久久久精品毛片| 91精品婷婷国产综合久久| 亚洲欧洲日产国码无码久久99| 久久亚洲精品国产亚洲老地址| 亚洲国产精品嫩草影院久久| 久久精品国产99国产电影网| 欧美亚洲国产精品久久| 国产A级毛片久久久精品毛片| 精品无码久久久久久国产| 国产成人久久精品一区二区三区| 精品免费tv久久久久久久| 国产69精品久久久久99| 久久99热只有频精品8| 国产69精品久久久久APP下载| 九九99精品久久久久久| 欧美黑人又粗又大久久久| 久久99精品久久久大学生| 久久精品国产欧美日韩99热| 久久久久久毛片免费看| 2021最新久久久视精品爱 | 久久精品国产99国产电影网| 久久综合亚洲欧美成人| 99久久er这里只有精品18| 久久午夜伦鲁片免费无码| 久久人人爽人人人人爽AV|