发表文章

[最新] 103 Stacking Boxes

wbb1395 2月前 4

版权声明:转载请注明出处 http://blog.csdn.net/wbb1395/article/details/81980104

动态规划

首先把所有的长方体按大小先排好序,从里到外一个个找,设置一个数组vecBoxes用来存储找到的长方体,数组vecBoxes初始值设为最里面的那个长方体,每遍历一次输入的长方体,就遍历一次数组vecBoxes,如果这个长方体包含数组vecBoxes里的一组长方体的最后一个,先把这个组新的长方体存储到vecAddBoxes

遍历完数组vecBoxes以后,从vecAddBoxes里找到最长的长方体组,再存到vecBoxes,遍历完输入的长方体,这题的解就出来了

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Box
{
	int iIndex = 0;
	vector<int> vecBox;
};


const bool boxCompare(const Box& stBox1, const Box& stBox2)
{
	for (auto it = stBox1.vecBox.begin(), iter = stBox2.vecBox.begin(); it != stBox1.vecBox.end() && iter != stBox2.vecBox.end(); ++it, ++iter)
	{
		if (*it < *iter)
		{
			return true;
		}
		else if (*it > *iter)
		{
			return false;
		}
	}
	return false;
}

class StackingBoxes
{
public:
	void readBoxes(const int& iBoxes, const int& iDimension);
	void computeBoxes();
	void outputResult();
private:
	vector<Box> m_vecBoxes;
	vector<Box> m_vecBoxesSequence;
};

void StackingBoxes::readBoxes(const int& iBoxes, const int& iDimension)
{
	m_vecBoxes.clear();
	Box stBox;
	int iBox = 0;
	for (int i = 1; i <= iBoxes; ++i)
	{
		stBox.iIndex = i;
		stBox.vecBox.clear();
		for (int j = 0; j < iDimension; ++j)
		{
			cin >> iBox;
			stBox.vecBox.push_back(iBox);
		}
		sort(stBox.vecBox.begin(), stBox.vecBox.end());
		m_vecBoxes.push_back(stBox);
	}
	sort(m_vecBoxes.begin(), m_vecBoxes.end(), boxCompare);
}

void StackingBoxes::computeBoxes()
{
	m_vecBoxesSequence.clear();
	vector<vector<Box>> vecBoxes;
	vector<vector<Box>> vecAddBoxes;
	vector<Box> vecBox;
	vecBox.push_back(*m_vecBoxes.begin());
	vecBoxes.push_back(vecBox);
	for (auto it = m_vecBoxes.begin() + 1; it != m_vecBoxes.end(); ++it)
	{
		vecAddBoxes.clear();
		bool isFind = false;
		for (auto& iter : vecBoxes)
		{
			bool isNest = true;
			for (auto iter1 = iter.rbegin()->vecBox.begin(), iter2 = it->vecBox.begin(); iter1 != iter.rbegin()->vecBox.end() && iter2 != it->vecBox.end(); ++iter1, ++iter2)
			{
				if (*iter1 >= *iter2)
				{
					isNest = false;
					break;
				}
			}
			if (isNest)
			{
				isFind = true;
				vecBox = iter;
				vecBox.push_back(*it);
				vecAddBoxes.push_back(vecBox);
			}
		}
		if (!isFind)
		{
			vecBox.clear();
			vecBox.push_back(*it);
			vecAddBoxes.push_back(vecBox);
		}
		size_t stMax = 0;
		for (auto& iter : vecAddBoxes)
		{
			if (iter.size() >= stMax)
			{
				stMax = iter.size();
			}
		}
		for (auto& iter : vecAddBoxes)
		{
			if (iter.size() >= stMax)
			{
				vecBoxes.push_back(iter);
			}
		}
	}
	size_t iCount = 0;
	for (auto& it : vecBoxes)
	{
		if (it.size() > iCount)
		{
			iCount = it.size();
			m_vecBoxesSequence = it;
		}
	}
}

void StackingBoxes::outputResult()
{
	cout << m_vecBoxesSequence.size() << endl;
	for (auto it = m_vecBoxesSequence.begin(); it != m_vecBoxesSequence.end(); ++it)
	{
		if (it == m_vecBoxesSequence.begin())
		{
			cout << it->iIndex;
		}
		else
		{
			cout << " " << it->iIndex;
		}
	}
	cout << endl;
}

int main()
{
	StackingBoxes objStackingBoxes;
	int iBoxes = 0;
	int iDimension = 0;
	while (cin >> iBoxes >> iDimension)
	{
		objStackingBoxes.readBoxes(iBoxes, iDimension);
		objStackingBoxes.computeBoxes();
		objStackingBoxes.outputResult();
	}
	return 0;
}

 

相关推荐
最新评论 (0)
返回
发表文章
wbb1395
文章数
30
评论数
0
注册排名
645859