SoRoMan

人若无名,便可专心练剑.

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  12 随笔 :: 1 文章 :: 41 评论 :: 0 Trackbacks

今天看到一个网友在谈论一个关于The Tower of Babylon的, 见http://www.cppblog.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 阅读(2566) 评论(2)  编辑 收藏 引用

评论

# re: 思考:关于The Tower of Babylon 2006-08-09 19:14 sicheng
非常感谢您对这道题的关注,甚至还为此写出了完整的程序。
程序写的很漂亮,非常感谢。
由于本人的疏忽 题目描述地不是很清楚,所以特此也把整个原题贴出来http://www.cppblog.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  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理