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

由大到小 多多指教

Posted by liyan199311 at 2017-03-24 17:00:31 on Problem 1020
不管由大到小还是由小到大,都要注意一点,遍历的过程中不允许出现中间出现空白,注意这个问题即可,即使不注意这个问题,都能够ac的,数据太弱。即使这样,也应当写出最准确的代码,这才是刷题追求的




#include<iostream>
#include<string>
#include <iomanip>
#include<math.h>
#include <fstream>  
using namespace std;

int input[10] = {};
int data[10] = { 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 };
int a[40] = {};
int size;
int total_times = 0;

//置位
bool set(int x, int length)
{
	if (x + length > size)
	{
		return false;
	}
	int index = x, error = 1;
	for (; index < length + x; index++)
	{
		a[index] += length;
		if (a[index]> size)
		{
			error = 0;
			break;
		}
	}
	if (!error)
	{
		//还原现场
		for (int i = x; i <= index; i++)
		{
			a[i] -= length;
		}
	}
	return error;
}

//选占用最短的x,顺便获取支持最长长度
void maxLeft(int &x, int &maxLength)
{
	x = 0;
	for (int i = 1; i < size; i++)
	{
			if (a[x] > a[i])
				x = i;
	}
	maxLength = 0;
	for (int i = x; i < size; i++)
	{
		if (a[x] == a[i])
		{
			maxLength ++;
		}
		else
		{
			break;
		}
	}
}

//清楚上次操作
void clearLast(int x, int length)
{
	for (int i = x; i < x + length; i++)
	{
		a[i] -= length;
	}
}

//x为选择的列,maxLength为选择该列时最大支持的正方形大小
bool dfs(int x, int maxLength)
{
	total_times++;
	int result = 0, left = 0;
	for (int i = 9; i >= 0; i--)
	{
		if (input[i])
		{
			//正方形放完即认为结束,否则继续dfs
			left = 1;
			if (maxLength >= i && set(x, i + 1))  //maxLength这里用于减枝,不然会tle
			{
				int next_x, next_maxLength;
				maxLeft(next_x, next_maxLength);
				input[i] --;
				result = dfs(next_x, next_maxLength);
				if (result)
				{
					return true;
				}
				else
				{
					//还原现场
					input[i] ++;
					clearLast(x, i + 1);
				}
			}
		}
	}
	if (left == 0)
	{
		return true;
	}
	return result;
}

int main()
{
	//ifstream in("d:\\document\\test.txt");
	int max_case = 0, temp_value, cur_case = 0;
	cin >> max_case;
	getchar();
	while (cur_case < max_case)
	{
		total_times = 0;
		cur_case++;
		int count = 0;
		int max_size = 0, max_value = 0, temp = 0;
		memset(input, 0, 10 * sizeof(int));
		memset(a, 0, 40 * sizeof(int));
		cin >> size >> count;
		for (int i = 0; i < count; i++)
		{
			cin >> temp;
			max_size += data[temp - 1];
			input[temp - 1] ++;
		}
		if (max_size != size * size)
		{
			cout << "HUTUTU!" << endl;
		}
		else
		{
			if (dfs(0, 10))
			{
				cout << "KHOOOOB!" << endl;
			}
			else
			{
				cout << "HUTUTU!" << endl;
			}
		}
	}
	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