关闭

uva 10123 No Tipping

标签: acmuva
365人阅读 评论(0) 收藏 举报
分类:

这道题目有点变态,首先一个优化的重点是应该首先放坐标夹在-1.5和1.5之间的物品,因为根据物理进行推导,先放这类物品只会使得整个系统不容易倾斜,增加稳定性,而不会因为放了这类物品而导致倾斜,一开始就只优化了这个,然后进行dfs暴力搜索,结果超时了,后来参考了网上的一些解决方案,其实还可以进行策略的优化,在放物品的时候,左侧和右侧的物品交替放置,即先放一边的物品,直到放不下去为止,然后开始放另外一边的,当然一开始的时候可以从左边开始放,也可以从右边开始放,两种情况都要进行dfs搜索,如果两边都没有办法继续放了,那么肯定找不到解决方案了,其实经过分析可以发现这样只需要对所有的物品进行一次遍历就可以解决问题了,效率的直接从平方级降成线性的复杂度了,优化的效果是十分明显的,这真是一道思路上很锻炼人的题目。

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;

struct obj
{
	double pos;
	double weight;
};

struct state
{
	int step;
	double left_val_p1, right_val_p1, left_val_p2, right_val_p2;
};

struct obj arr1[30];
struct obj arr2[30];
struct obj solution[30];
bool visited[60];

double _abs(double x)
{
	return x>0 ? x:x*(-1);
}

int cmp(const void* a, const void *b)
{
	double sub_a, sub_b;

	struct obj *pa = (struct obj*)a;
	struct obj *pb = (struct obj*)b;
	
	sub_a = _abs(pa->pos*pa->weight);
	sub_b = _abs(pb->pos*pb->weight);

	if(sub_a > sub_b)
		return 1;
	else if(sub_a < sub_b)
		return -1;
	else
		return 0;
}


void put_on_board(const struct obj &obj_ins, struct state &sta)
{
	if(obj_ins.pos<-1.5)
	{
		sta.left_val_p1 += (-1.5-obj_ins.pos)*obj_ins.weight;
		sta.left_val_p2 += (1.5-obj_ins.pos)*obj_ins.weight;
	}
	else if(obj_ins.pos > 1.f)
	{
		sta.right_val_p1 += (obj_ins.pos-(-1.5)) * obj_ins.weight;
		sta.left_val_p2 += (1.5-obj_ins.pos) * obj_ins.weight;
	}
	else
	{
		sta.right_val_p1 += (obj_ins.pos-(-1.5)) * obj_ins.weight;
		sta.right_val_p2 += (obj_ins.pos-1.5) * obj_ins.weight;
	}
	sta.step++;
	solution[sta.step].pos = obj_ins.pos;
	solution[sta.step].weight = obj_ins.weight;
}

bool is_banlanced(const struct state &sta)
{
	if(sta.left_val_p1 > sta.right_val_p1)
		return false;
	if(sta.right_val_p2 > sta.left_val_p2)
		return false;
	return true;
}

bool find_solution;
#define		LEFT	0
#define		RIGHT	1
void dfs(const struct state &sta, int obj_num, int count1, int count2, int direction)
{
	struct state cur_sta, backup_sta;
	int i;
	bool f; //表示是否已经放了物品

	if(find_solution)
		return;

	if(sta.step == obj_num)
		return;

	if(direction == LEFT)
	{
		memcpy(&(cur_sta), &(sta), sizeof(struct state));
		f = false;
		for(i=1; i<=count1; i++)
		{
			if(visited[i])
				continue;

			memcpy(&(backup_sta), &(cur_sta), sizeof(struct state));
			put_on_board(arr1[i], cur_sta);
			if(is_banlanced(cur_sta))
			{
				if(cur_sta.step == obj_num)
				{
					find_solution = true;
					return;
				}

				f = true;
				visited[i] = true;
				continue;
			}
			else
			{
				break;
			}
		}
		if(f)
		{
			if(i == count1+1)
				dfs(cur_sta, obj_num, count1, count2, RIGHT);
			else
				dfs(backup_sta, obj_num, count1, count2, RIGHT);
			if(find_solution)
				return;
		}
	}
	else
	{
		memcpy(&(cur_sta), &(sta), sizeof(struct state));
		f = false;
		for(i=1; i<=count2; i++)
		{
			if(visited[i+30])
				continue;

			memcpy(&(backup_sta), &(cur_sta), sizeof(struct state));
			put_on_board(arr2[i], cur_sta);
			if(is_banlanced(cur_sta))
			{
				if(cur_sta.step == obj_num)
				{
					find_solution = true;
					return;
				}

				f = true;
				visited[i+30] = true;
				continue;
			}
			else
			{
				break;
			}	
		}
		if(f)
		{	
			if(i == count2+1) //右边的放完了
				dfs(cur_sta, obj_num, count1, count2, LEFT);
			else
				dfs(backup_sta, obj_num, count1, count2, LEFT);
			if(find_solution)
				return;
		}
	}
}

void func(int obj_num, int count1, int count2, const struct state &sta)
{
	struct state cur_sta;
	int i;

	memcpy(&(cur_sta), &(sta), sizeof(struct state));
	
	
	find_solution = false;
	//从左边开始放
	memset(visited, false, 60*sizeof(bool));
	dfs(cur_sta, obj_num, count1, count2, LEFT);
	//从右边开始放
	memset(visited, false, 60*sizeof(bool));
	dfs(cur_sta, obj_num, count1, count2, RIGHT);
	if(!find_solution)
	{
		printf("Impossible\n");
	}
	else
	{
		for(i=obj_num; i>=1; i--)
			printf("%.0lf %.0lf\n", solution[i].pos, solution[i].weight);
	}
}

int main(void)
{
	double board_length, board_weight;
	int obj_num, i, count1, count2;
	double pos, weight;
	struct obj obj_temp;
	struct state sta;
	double density;
	int case_count;

	//freopen("input.dat", "r", stdin);

	case_count = 0;
	while(1)
	{
		scanf("%lf %lf %d", &board_length, &board_weight, &obj_num);
		if(board_length==0 && board_weight==0 && obj_num==0)
			break;
		density = board_weight/board_length;
		sta.step = 0;
		sta.left_val_p1 = sta.right_val_p2 = pow(board_length/2-1.5, 2.0) * (density/2.0);
		sta.left_val_p2 = sta.right_val_p1 = pow(board_length/2+1.5, 2.0) * (density/2.0);
		
		for(i=1, count1=count2=1; i<=obj_num; i++)
		{
			scanf("%lf %lf", &pos, &weight);
			if(pos>=-1.5 && pos<=1.5)
			{
				obj_temp.weight = weight;
				obj_temp.pos = pos;
				put_on_board(obj_temp, sta);
			}
			else
			{
				if(pos < -1.5)
				{
					arr1[count1].pos = pos;
					arr1[count1].weight = weight;
					count1 ++;
				}
				else
				{
					arr2[count2].pos = pos;
					arr2[count2].weight = weight;
					count2++;
				}
			}
		}
		qsort(arr1+1, count1-1, sizeof(struct obj), cmp);
		qsort(arr2+1, count2-1, sizeof(struct obj), cmp);
		case_count ++;
		printf("Case %d:\n", case_count);
		func(obj_num, count1-1, count2-1, sta);
	}

	return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:89511次
    • 积分:3385
    • 等级:
    • 排名:第10318名
    • 原创:263篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类