盖房子问题,请大家踊跃发表意见

arden1019 2006-02-09 10:29:43
这是google编程大赛入围赛题目之一,在http://www.blogjava.net/emu/articles/10191.html上作了一些讨论,可是我感觉都不全面。我总感觉可以提炼出一个比较好的数学模型,下面是题目:
请大家一起讨论啊。

Problem Statement

We are building a rectangular house using manufactured sections for the walls. Each section is 4 feet long. There are three types of sections: window

sections, door sections, and regular sections. We have a fixed assortment of these sections, and want to design the house to have a maximum area, subject

to the following rules:
1) A house side must contain no more than one door.
2) The house must have at least one door.
3) A door section may not be at the end of a wall.
4) Each window section must be adjacent to two regular sections, one on each side of it in its wall.
Create a class HouseParty that contains a method maxArea that is given numReg, numWin, and numDoor (the available quantity of each type of section) and

that returns the maximum area of a house built from those sections. You are not required to use all the sections. If no house can be built your method should

return 0.
Definition

Class:
HouseParty
Method:
maxArea
Parameters:
int, int, int
Returns:
int
Method signature:
int maxArea(int numReg, int numWin, int numDoor)
(be sure your method is public)


Constraints
-
numReg, numWin, and numDoor will each be between 0 and 50 inclusive.
Examples
0)


8
0
0
Returns: 0
No house can be built since you have no door sections.
1)


8
0
1
Returns: 48
One way is to use 3 regular sections for the north wall, one regular section for the east wall and one for the west wall, and to use a door section between 2

regular sections for the south wall. This gives a house that is 12' x 4'. Below is a picture (with door and window sections shown as D and W, and regular

sections shown as - or | )

---
| |
-D-
2)


9
8
2
Returns: 144
One design is:
-D-
| |
D W
| |
-W-
3)


6
23
13
Returns: 48
We are very short of regular sections; one design is:
-
| |
D W
| |
-

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior

written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

...全文
372 22 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
arden1019 2006-02-25
  • 打赏
  • 举报
回复
夭折了...
arden1019 2006-02-21
  • 打赏
  • 举报
回复
最后一次提上来,真的没人愿意看看?
arden1019 2006-02-14
  • 打赏
  • 举报
回复
再次顶上来,是不是嫌问题文字太多啊...
arden1019 2006-02-14
  • 打赏
  • 举报
回复
>>你的那个连接里面有人说“时间复杂度超过题目要求,失败! ”

>>看到那里面有人给出了时间复杂度 O(1) 的算法,都O(1)了偶就没继续看了....
-------------------------------------

我只看题目,上面没有要求复杂度,当然复杂度越小越好啦。虽然链接里面的说是O(1)了,但是,我的目的是想找到一个比较完整的数学抽象来表示它。链接中的算法,我不知道能不能够被证明是正确的。
RiderOnStorm 2006-02-14
  • 打赏
  • 举报
回复
强贴,mark一下
ox_thedarkness 2006-02-14
  • 打赏
  • 举报
回复
你的那个连接里面有人说“时间复杂度超过题目要求,失败! ”

看到那里面有人给出了时间复杂度 O(1) 的算法,都O(1)了偶就没继续看了....
arden1019 2006-02-14
  • 打赏
  • 举报
回复
题目没有要求复杂度吧
ox_thedarkness 2006-02-14
  • 打赏
  • 举报
回复
那天看了一会儿没继续看了
要求的时间复杂度是多少阿?
arden1019 2006-02-10
  • 打赏
  • 举报
回复
ps:上面代码为秋水无恨同志所写,感谢。
arden1019 2006-02-10
  • 打赏
  • 举报
回复
下面是一个js版本的算法,使用了构造法,为了保持最大化面积,尽量使房子接近正方形。可是,这并不是好的、完整的数据抽象。而且其中的构造规则,没有被证明是正确的:

<script>
function Int(n){return Math.floor(n)};
function min(n,m){return Math.min(n,m)};
function maxArea(numReg,numWin,numDoor){
if(numDoor<1 || numReg<4 ) //我认为这里应该是numReg<6
return 0;
var l,x=1,y=1;//x,y表示长和宽
if(numDoor>4)numDoor=4;numReg-=4;
var nUsed=min(numReg,numDoor+numWin);//用的门窗的总合

if(nUsed%2)//如果是1或3,则补足2的倍数
if(numReg-1>nUsed){//如果墙够用来代替门窗
if((0==(numReg%2))&&(1==(nUsed%4))&&(nUsed>1))--nUsed;else//(12,4,1)bug
{numReg-=1;++nUsed;}//则多用一个门窗
}else --nUsed;//若不够,则少用一个门窗

if(nUsed==0)return 0;//(5,5,5)bug

if(l=Int(nUsed/4))//先添加到四面
{
x+=l*2;y+=l*2;//2=一个门窗加一个墙
numReg-=l*4;nUsed%=4;//每面都要用墙,故*4
}
if(nUsed==2)
{
numReg-=2;//减去和门窗对应的墙
x+=2;//先添加给一边,因为目前两边还一样长
if(numReg>=2){numReg-=2;y+=1;}//如果墙有多余的,先给另一边加1,尽量相同
}

if(l=Int(numReg/4))//先添加到四面
{
x+=l;y+=l;//这时只加了一个墙
numReg-=l*4;//但一样每面都要用墙,故*4
}
if(numReg>1)//多出来的分两边
{
numReg-=2;//只有一面加墙
if(x>y)y+=1;else x+=1;
}
return x*y*16;
}

document.write("8,0,0=",maxArea(8,0,0), "<br>");
document.write("8,0,1=",maxArea(8,0,1), "<br>");
document.write("9,8,2=",maxArea(9,8,2), "<br>");
document.write("6,23,13=",maxArea(6,23,13), "<br>");
</script>
抛砖引玉喽...
zsf81 2006-02-10
  • 打赏
  • 举报
回复
学习
laolaoliu2002 2006-02-10
  • 打赏
  • 举报
回复
不是的,可是提供了一种思路
arden1019 2006-02-10
  • 打赏
  • 举报
回复
顶上来
ox_thedarkness 2006-02-10
  • 打赏
  • 举报
回复
= = 哈...诡异阿... 根据规则做的房子可以让他的一个面除了左右两块以外都是门:

_DDDDDDDDDDDD_

研究ing...
ox_thedarkness 2006-02-10
  • 打赏
  • 举报
回复
laolaoliu2002(老刘----狂热的热爱人民币)
这个问题只要围一个长方形的盒子... 里面没有子房间的
Mr_Yang 2006-02-09
  • 打赏
  • 举报
回复
mark
arden1019 2006-02-09
  • 打赏
  • 举报
回复
最主要的四条规则,用汉语说就是:
1) A house side must contain no more than one door.
一间房子的一面墙最多一个门
2) The house must have at least one door.
一间房子最少有一个门
3) A door section may not be at the end of a wall.
门不能在一面墙的边缘
4) Each window section must be adjacent to two regular sections, one on each side of it in its wall.
窗户必须两边都是regular sections
arden1019 2006-02-09
  • 打赏
  • 举报
回复
...楼上的解法是本题的解法么?我没有看懂...
laolaoliu2002 2006-02-09
  • 打赏
  • 举报
回复
有一个建筑物平面示意图,可分解为 m × n 个边长面积均相等的小块,成为“格”
相邻格之间可能有墙, wall ( x , y )表示横坐标为 x 、纵坐标为 y 的格,其四个方向墙取值的加和:左面墙取值 1 ,上面墙取值 2 ,右面墙取值 4 ,下面墙取值 8 。

举例: wall ( 1 , 1 )取值为 3 ,则说明上面和左面有墙,其它方向没有。所有的墙将建筑物分割为若干个互相不连通的区域,每个区域称为一个“房间”。
对于格,及 wall ( x , y )的描述,采用数据文件 input.txt 描述

本题算法有点复杂,涉及了十字链表.
我得解法如下.
setlink(){setsublink()}建立十字链表把处于统一房间的块串起来.
setsumlink()将房间两两组合,建立一个结构数组,设房间总数为headnum,该数组长度为headnum*(headnum-1)/2,改结构的元素为两房间快数之和以及指向两个房间头的
指针.
sortsumlink()将该结构数组排序,从大到小.
然后以次从该结构数组中seekdoor()寻找连接两个房间的关键之门,并返回一个
结构food,即为所求结果,opendoor(),打开关键之门.
laolaoliu2002 2006-02-09
  • 打赏
  • 举报
回复
经典的问题

70,021

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧