Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

C++ AC代码(纯模拟,为了不超时,数据进行了部分减支)

Posted by freelark at 2017-01-03 22:48:47 on Problem 1017
#include <iostream>
#include <stdlib.h>
#include <numeric>  //加法用 
using namespace std;

int *size = new int[7];
int *box = new int[36];
int total, num=0;

bool insert_flag(int x, int y, int i) 
{
	for (int m = 0; m < i; m++)
	{
		for (int n = 0; n < i; n++)
			if (box[(x + m) * 6 + (y + n)] != 0 || (x + m)>5 || (y + n)>5)
				return false;
	}
	return true;		
}

void pack()
{		
	if (num > 0) total++;
	while (num > 0)
	{
		int last, first = 0;
		for (int i = 1; i <= 6; ++i) 
		{
			if (size[i] != 0)
			{
				if (first == 0) first = i;
				last = i;
			}
		}
		for (int i = last; i >= first; i--)
		{
			while (size[i] != 0)
			{
				int x = 0, y = 0;
				while (!insert_flag(x, y, i) && (x + y) <= 10)
				{
					y++;
					if (y == 6){
						y = 0;
						x++;
					}
				}	
				if (x + i <= 6 && y + i <= 6)
				{
					for (int m = 0; m < i; m++)
					for (int n = 0; n < i; n++)
						box[(x + m) * 6 + (y + n)] = i;
					size[i]--;
					num--;
				}
				else break;
			}
			if (i == first && size[i]!=0 ) 
			{
				total++;
				memset(box, 0, 36 * sizeof(int));
				break;
			}
		}
	}
}

int main()
{	
	while (1)
	{
		total = 0;
		memset(box, 0, 36 * sizeof(int));	
		for (int i = 1; i <= 6; ++i)		
			cin >> size[i];		
		num = accumulate(size + 1, size + 7, 0);
		if (!num) break;
                //以下减少运算量
		if (size[6] > 0){ total += size[6]; size[6] = 0; }
		if (size[3] / 4 > 0) { total += size[3] / 4; size[3] = size[3] % 4; }			
		if (size[1] <= 11 * size[5]) size[1] = 0; else size[1] -= 11 * size[5];
		if (size[5] > 0){ total += size[5]; size[5] = 0; }

		num = accumulate(size + 1, size + 7, 0);
		pack();
		cout << total << endl;
	}
	delete[]size;
	delete[]box;
	system("pause");
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator