USACO 1.4.1解题报告
来源:互联网 发布:php public static 编辑:程序博客网 时间:2024/05/16 12:44
IOI 95
The six basic layouts of four rectangles
Four rectangles are given. Find the smallest enclosing (new) rectangle into which these four may be fitted without overlapping. By smallest rectangle, we mean the one with the smallest area.
All four rectangles should have their sides parallel to the corresponding sides of the enclosing rectangle. Figure 1 shows six ways to fit four rectangles together. These six are the only possible basic layouts, since any other layout can be obtained from a basic layout by rotation or reflection. Rectangles may be rotated 90 degrees during packing.
There may exist several different enclosing rectangles fulfilling the requirements, all with the same area. You must produce all such enclosing rectangles.
PROGRAM NAME: packrec
INPUT FORMAT
Four lines, each containing two positive space-separated integers that represent the lengths of a rectangle's two sides. Each side of a rectangle is at least 1 and at most 50.
SAMPLE INPUT (file packrec.in)
1 22 33 44 5
OUTPUT FORMAT
The output file contains one line more than the number of solutions. The first line contains a single integer: the minimum area of the enclosing rectangles. Each of the following lines contains one solution described by two numbers p and q with p<=q. These lines must be sorted in ascending order of p, and must all be different.SAMPLE OUTPUT (file packrec.out)
404 105 8
由题意可知,四个矩形只有6种排列情况,其实,四和五可以合并为一种情况,那么只剩五种情况,前四种直接按数学方法计算即可求得面积,后一种要分三种情况,讨论是否重合,只有在不重合的前提下,才能计算其面积,四个矩形前排列只有24种情况,由深搜即可得到其全排列,然后每种情况计算其面积就可以,代码比较长,注意要细心。
代码如下:
/*
ID:jinyuxu2
LANG:C++
PROG:packrec
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
int p,minarea;
int str[4][2];
struct node
{
int area,row,col;
}rect[3000];
void Push(int x,int y)
{
int s=x*y;
if(x>y)
{
int t=x;
x=y;
y=t;
}
if(s<minarea)
{
rect[0].area=s;
rect[0].row=x;
rect[0].col=y;
minarea=s;
p=1;
}
else if(s==minarea)
{
rect[p].area=s;
rect[p].row=x;
rect[p].col=y;
p++;
}
else
return;
}
int cmp(const void *a,const void *b)
{
return(((struct node *)a)->row-((struct node *)b)->row);
}
void Select(int t)
{
int x,y;
if(t==1)
{
x=0;
y=0;
for(int i=0;i<4;i++)
{
if(str[i][1]>y)
y=str[i][1];
x+=str[i][0];
}
Push(x,y);
}
if(t==2)
{
x=0;
y=0;
for(int i=0;i<3;i++)
{
if(str[i][1]>y)
y=str[i][1];
x+=str[i][0];
}
if(x<str[3][1])
x=str[3][1];
y+=str[3][0];
Push(x,y);
}
if(t==3)
{
x=0;
y=0;
for(int i=0;i<2;i++)
{
if(str[i][1]>y)
y=str[i][1];
x+=str[i][0];
}
y+=str[2][0];
if(x<str[2][1])
x=str[2][1];
x+=str[3][0];
if(y<str[3][1])
y=str[3][1];
Push(x,y);
}
if(t==4)
{
x=0;
y=0;
for(int i=0;i<2;i++)
{
if(str[i][1]>y)
y=str[i][1];
x+=str[i][0];
}
if(str[2][0]>str[3][0])
x+=str[2][0];
else
x+=str[3][0];
if(y<str[2][1]+str[3][1])
y=str[2][1]+str[3][1];
Push(x,y);
}
if(t==5)
{
if(str[0][0]<=str[1][0])
{
if(str[2][1]<str[1][1]&&str[3][1]<=str[2][0])
{
x=str[1][0]+str[2][0];
y=str[0][1]+str[1][1]>str[2][1]+str[3][0]?str[0][1]+str[1][1]:str[2][1]+str[3][0];
}
if(str[2][1]>=str[1][1]&&str[2][1]<str[0][1]+str[1][1]&&str[0][0]+str[3][1]<=str[1][0]+str[2][0])
{
x=str[1][0]+str[2][0];
y=str[0][1]+str[1][1]>str[2][1]+str[3][0]?str[0][1]+str[1][1]:str[2][1]+str[3][0];
}
if(str[2][1]>=str[0][1]+str[1][1])
{
x=str[3][1]>str[1][0]+str[2][0]?str[3][1]:str[1][0]+str[2][0];
y=str[2][1]+str[3][0];
}
Push(x,y);
}
}
}
void DFS(int t)
{
int a,b,c,d;
if(t>=4)
return;
if(t==3)
for(int i=1;i<=5;i++)
Select(i);
DFS(t+1);
a=str[t][0];
b=str[t][1];
for(int i=t+1;i<4;i++)
{
str[t][0]=str[i][0];
str[t][1]=str[i][1];
str[i][0]=a;
str[i][1]=b;
DFS(t+1);
c=str[t][0];
d=str[t][1];
str[t][0]=str[i][0];
str[t][1]=str[i][1];
str[i][0]=c;
str[i][1]=d;
}
}
void choose(int t)
{
int a;
if(t==4)
{
DFS(0);
return;
}
choose(t+1);
a=str[t][0];
str[t][0]=str[t][1];
str[t][1]=a;
choose(t+1);
a=str[t][0];
str[t][0]=str[t][1];
str[t][1]=a;
}
int main()
{
freopen("packrec.in","r",stdin);
freopen("packrec.out","w",stdout);
p=0;
minarea=0x3fffffff;
int x,y;
for(int i=0;i<4;i++)
{
scanf("%d %d",&x,&y);
str[i][0]=x<y?x:y;
str[i][1]=x>y?x:y;
}
choose(0);
qsort(rect,p,sizeof(rect[0]),cmp);
printf("%d\n",rect[0].area);
printf("%d %d\n",rect[0].row,rect[0].col);
for(int i=1;i<p;i++)
if(rect[i].row!=rect[i-1].row)
printf("%d %d\n",rect[i].row,rect[i].col);
return 0;
}
- USACO 1.4.1解题报告
- USACO:Overfencing解题报告
- usaco Transformations 解题报告
- USACO Stringsobits 解题报告
- USACO Camelot 解题报告
- USACO Cowcycles 解题报告
- USACO Wormholes 解题报告
- USACO Telecowmunication 解题报告
- USACO Picture 解题报告
- USACO Twofive 解题报告
- USACO 3.4解题报告
- USACO 4.1解题报告
- USACO 4.2解题报告
- USACO 4.3解题报告
- USACO 4.4解题报告
- USACO 5.3解题报告
- USACO 6.1解题报告
- USACO :The Castle解题报告
- Handling ORA-1403 ora-12801 on logical standby app
- java.lang.IllegalStateException: getOutputStream() has already been called for t
- 纳税金额
- android 音乐播放器----获取专辑封面图片
- validateJarFile jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending c
- USACO 1.4.1解题报告
- 无线Trace脚本(旧格式)-平均延时
- home键监听
- matlab把图片pgm格式转换成png格式
- 知道生日算日期
- 无线Trace脚本(旧格式)-吞吐量
- Android高手的六大境界 mvc模式
- java环境变量配置
- 5——22