【 UVA - 1587 】Box(盒子)

题目链接

代码:

#include <iostream>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
typedef pair<int,int> pr; //表示长方形
int main()
{
    
    
	int w,h,c=0;
	map<pr,int> m;  //映射:长方形-->出现次数
	map<int,int> cnt; //映射: 边长-->出现次数
	set<pr> s; //记录出现了哪些类型的长方形,方便m查找次数
	while(cin>>w>>h)
	{
    
    
		++c;
		if(w>h) swap(w,h); //按从小到大
		m[pr{
    
    w,h}]++; //增加该种长方形出现次数
		cnt[w]++; //增加该种边出现次数
		cnt[h]++;
		s.insert(pr{
    
    w,h}); //增加该种长方形类型
		
		if(c%6==0) //每输入完六个长方形 判断一次
		{
    
    
			int flag=0;
			for(pr x:s)
			{
    
    
				if(m[x]%2 || cnt[x.first]%4 || cnt[x.second]%4)
				/*相同长方形应该成对出现 && 长方形的任何边必须能整除4(因为最起码
				相面对的两个长方形(相同),一定最少有四条相等边),当等长边的出现次数为12时,为正方形。*/
				{
    
    
					flag=1;
					break;
				}
			}	
			if(flag) cout<<"IMPOSSIBLE"<<endl;
			else cout<<"POSSIBLE"<<endl;
			m.clear(),cnt.clear(),s.clear();
		}
	}
   	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_45260385/article/details/108175785
今日推荐