CF581D三个矩形能否构成一个无空隙的正方形
2015-09-29 15:53
134 查看
这题又呵呵一次,自己脑子笨就讨论了所有情况,自己讨论了十种情况,貌似讨论的有点多,
然后就乱了,讨论完就交了,结果终测挂了,仔细想了一下貌似把-1的情况的位置放错了,
然后加了几个continue,把-1情况输出的位置换了一下就过了,还是做的少,WA的离谱。
这题自己的代码写的即麻烦,又繁琐,应该是讨论的情况多了,毕竟怕错,就小心了一点,
然而还是跪了,十种情况,三个长方向一致的情况,然后否则最长的边做正方形的边长,
俩外俩个,放在这个矩形上面,俩个全倒着,或者全立着,或者左倒,右立,或者左立右倒。
另外我没有开数组,加大了代码的繁琐度,查了一下题解,发现了很简单的代码:
他的思路是:首先能构成矩形,只要三个矩形的面积和等于最长边的平方就一定能构成,我当时
做的时候有想过,但是感觉不是很严谨,没有举出反例去验证,就没有想了,有些题还是需要猜想
然后验证的。然后选出剩下俩个中的一个,如果较长边等于len或者较短边加上上面的矩形的宽
等于正方形的边长就不用旋转,否者旋转,即是把俩边swap一下就行了,然后最后一个矩形补齐就行了。
这里先贴上我的暴力代码,后面贴上简洁的代码:
然后就乱了,讨论完就交了,结果终测挂了,仔细想了一下貌似把-1的情况的位置放错了,
然后加了几个continue,把-1情况输出的位置换了一下就过了,还是做的少,WA的离谱。
这题自己的代码写的即麻烦,又繁琐,应该是讨论的情况多了,毕竟怕错,就小心了一点,
然而还是跪了,十种情况,三个长方向一致的情况,然后否则最长的边做正方形的边长,
俩外俩个,放在这个矩形上面,俩个全倒着,或者全立着,或者左倒,右立,或者左立右倒。
另外我没有开数组,加大了代码的繁琐度,查了一下题解,发现了很简单的代码:
他的思路是:首先能构成矩形,只要三个矩形的面积和等于最长边的平方就一定能构成,我当时
做的时候有想过,但是感觉不是很严谨,没有举出反例去验证,就没有想了,有些题还是需要猜想
然后验证的。然后选出剩下俩个中的一个,如果较长边等于len或者较短边加上上面的矩形的宽
等于正方形的边长就不用旋转,否者旋转,即是把俩边swap一下就行了,然后最后一个矩形补齐就行了。
这里先贴上我的暴力代码,后面贴上简洁的代码:
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<string> #include<algorithm> #include<vector> #include<set> #include<map> #include<cmath> #include<cctype> #include<queue> #define LL long long using namespace std; const double eps=1e-10; const double pi=acos(-1.0); const int N=1e5+10; int main() { int x1,y1,x2,y2,x3,y3; while(cin>>x1) { cin>>y1; if(x1<y1) swap(x1,y1); cin>>x2>>y2; if(x2<y2) swap(x2,y2); cin>>x3>>y3; if(x3<y3) swap(x3,y3); if(x1==x2&&x2==x3&&(x1==y1+y2+y3)) { cout<<x1<<endl; for(int i=0;i<y1;i++) { for(int j=0;j<x1;j++) cout<<'A'; cout<<endl; } for(int i=0;i<y2;i++) { for(int j=0;j<x1;j++) cout<<'B'; cout<<endl; } for(int i=0;i<y3;i++) { for(int j=0;j<x1;j++) cout<<'C'; cout<<endl; } continue; } else { int ans=max(max(x1,x2),x3); if(ans==x1) { if(x2+x3==x1&&y2==y3&&x1==y2+y1) { cout<<ans<<endl; for(int i=0;i<y2;i++) { for(int j=0;j<x2;j++) cout<<'B'; for(int j=0;j<x3;j++) cout<<'C'; cout<<endl; } for(int i=0;i<y1;i++) { for(int j=0;j<x1;j++) cout<<'A'; cout<<endl; } continue; } else if(y2+y3==x1&&x2==x3&&x2+y1==x1) { cout<<ans<<endl; for(int i=0;i<x2;i++) { for(int j=0;j<y2;j++) cout<<'B'; for(int j=0;j<y3;j++) cout<<'C'; cout<<endl; } for(int i=0;i<y1;i++) { for(int j=0;j<x1;j++) cout<<'A'; cout<<endl; } continue; } else if(x2==y3&&y2+x3==x1&&x1==x2+y1) { cout<<ans<<endl; for(int i=0;i<x2;i++) { for(int j=0;j<y2;j++) cout<<'B'; for(int j=0;j<x3;j++) cout<<'C'; cout<<endl; } for(int i=0;i<y1;i++) { for(int j=0;j<x1;j++) cout<<'A'; cout<<endl; } continue; } else if(y2==x3&&x2+y3==x1&&y2+y1==x1) { cout<<ans<<endl; for(int i=0;i<y2;i++) { for(int j=0;j<x2;j++) cout<<'B'; for(int j=0;j<y3;j++) cout<<'C'; cout<<endl; } for(int i=0;i<y1;i++) { for(int j=0;j<x1;j++) cout<<'A'; cout<<endl; } continue; } } else if(ans==x2) { if(x1+x3==x2&&y1==y3&&x2==y2+y1) { cout<<ans<<endl; for(int i=0;i<y1;i++) { for(int j=0;j<x1;j++) cout<<'A'; for(int j=0;j<x3;j++) cout<<'C'; cout<<endl; } for(int i=0;i<y2;i++) { for(int j=0;j<x2;j++) cout<<'B'; cout<<endl; } continue; } else if(y1+y3==x2&&x1==x3&&x1+y2==x2) { cout<<ans<<endl; for(int i=0;i<x1;i++) { for(int j=0;j<y1;j++) cout<<'A'; for(int j=0;j<y3;j++) cout<<'C'; cout<<endl; } for(int i=0;i<y2;i++) { for(int j=0;j<x2;j++) cout<<'B'; cout<<endl; } continue; } else if(x1==y3&&y1+x3==x2&&x2==x1+y2) { cout<<ans<<endl; for(int i=0;i<x1;i++) { for(int j=0;j<y1;j++) cout<<'A'; for(int j=0;j<x3;j++) cout<<'C'; cout<<endl; } for(int i=0;i<y2;i++) { for(int j=0;j<x2;j++) cout<<'B'; cout<<endl; } continue; } else if(y1==x3&&x1+y3==x2&&y2+y1==x2) { cout<<ans<<endl; for(int i=0;i<y1;i++) { for(int j=0;j<x1;j++) cout<<'A'; for(int j=0;j<y3;j++) cout<<'C'; cout<<endl; } for(int i=0;i<y2;i++) { for(int j=0;j<x2;j++) cout<<'B'; cout<<endl; } continue; } } else if(ans==x3) { if(x1+x2==x3&&y1==y2&&x3==y1+y3) { cout<<ans<<endl; for(int i=0;i<y2;i++) { for(int j=0;j<x1;j++) cout<<'A'; for(int j=0;j<x2;j++) cout<<'B'; cout<<endl; } for(int i=0;i<y3;i++) { for(int j=0;j<x3;j++) cout<<'C'; cout<<endl; } continue; } else if(y1+y2==x3&&x1==x2&&x1+y3==x3) { cout<<ans<<endl; for(int i=0;i<x2;i++) { for(int j=0;j<y1;j++) cout<<'A'; for(int j=0;j<y2;j++) cout<<'B'; cout<<endl; } for(int i=0;i<y3;i++) { for(int j=0;j<x3;j++) cout<<'C'; cout<<endl; } continue; } else if(x1==y2&&y1+x2==x3&&x3==x1+y3) { cout<<ans<<endl; for(int i=0;i<x1;i++) { for(int j=0;j<y1;j++) cout<<'A'; for(int j=0;j<x2;j++) cout<<'B'; cout<<endl; } for(int i=0;i<y2;i++) { for(int j=0;j<x3;j++) cout<<'C'; cout<<endl; } continue; } else if(y1==x2&&x1+y2==x3&&y3+y1==x3) { cout<<ans<<endl; for(int i=0;i<y1;i++) { for(int j=0;j<x1;j++) cout<<'A'; for(int j=0;j<y2;j++) cout<<'B'; cout<<endl; } for(int i=0;i<y3;i++) { for(int j=0;j<x3;j++) cout<<'C'; cout<<endl; } continue; } } } cout<<-1<<endl; } return 0; }简洁的代码:
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<string> #include<algorithm> #include<vector> #include<set> #include<map> #include<cmath> #include<cctype> #include<queue> #define LL long long using namespace std; const double eps=1e-10; const double pi=acos(-1.0); const int N=1e5+10; struct Rec { int l,w; char ch; bool operator < (const Rec& rhs) const { return l<rhs.l; } int sq() { return l*w; } }; Rec rec[4]; int main() { for(int i=1;i<=3;i++) { cin>>rec[i].l>>rec[i].w; if(rec[i].l<rec[i].w) swap(rec[i].l,rec[i].w); rec[i].ch='A'-1+i; } sort(rec+1,rec+4); if((rec[3].l*rec[3].l)!=rec[3].sq()+rec[2].sq()+rec[1].sq()) cout<<-1<<endl; else { cout<<rec[3].l<<endl; for(int i=0;i<rec[3].w;i++) { for(int j=0;j<rec[3].l;j++) cout<<rec[3].ch; cout<<endl; } if(rec[2].l!=rec[3].l&&rec[2].w+rec[3].w!=rec[3].l) swap(rec[2].l,rec[2].w); for(int i=0;i<rec[2].w;i++) { for(int j=0;j<rec[2].l;j++) cout<<rec[2].ch; for(int j=0;j<rec[3].l-rec[2].l;j++) cout<<rec[1].ch; cout<<endl; } for(int i=0;i<rec[3].l-rec[2].w-rec[3].w;i++) { for(int j=0;j<rec[3].l;j++) cout<<rec[1].ch; cout<<endl; } } return 0; }
相关文章推荐
- configure: error: Cannot locate header file libintl.h 错误的解决方法
- 使用 xcodebuild 从 archive 导出 ipa
- ListView工作流程
- VC++中调用EXCEL方法
- 2015合肥网络赛 HDU 5492 Find a path 动归
- EasyUI - Tabs
- struts.xml配置文件(二)
- SQL中的一些简单语法总结
- Apache上配置反向代理以及负载均衡配置
- 如何使用手机作为树莓派的显示屏幕
- Android 吸入动画效果实现分解
- 深入浅出 RPC - 深入篇
- js返回顶部
- 开源软件的许可协议 License
- 九月英语学习
- linux应用编程笔记(5)系统调用文件编程方法实现文件复制
- 一个绚丽的loading动效分析与实现!
- Gson的使用 && 使用OKHttp处理Volley的底层HTTP请求
- 基于用户的协同过滤推荐算法原理和实现
- java 通配符解惑