hihocoder #1233 : Boxes 北京赛区(2015)网络赛(状态压缩+bfs)

#1233 : Boxes

时间限制: 1000ms
单点时限: 1000ms
内存限制: 256MB

描述

There is a strange storehouse in PKU. In this storehouse there are n slots for boxes, forming a line. In each slot you can pile up any amount of boxes. The limitation is that you can only pile a smaller one above a bigger one, in order to keep balance. The slots are numbered from 1 to n. The leftmost one is slot 1.

At first there is exactly one box in each slot. The volume of the box in slot i is vi. As a virgo, you decide to sort these boxes by moving some of them. In each move you can choose a slot and move the top box in this slot to an adjacent slot (of course you can only put it on the top). You should ensure that the limitation mentioned above is still satisfied after this move. After the sort operation, there should be exactly one box in each slot, and for each pair of adjacent slots, the box in the left one should be smaller than the box in the right one.

Your task is to calculate the minimum number of moves you need to sort the boxes.

输入

In the first line there’s an integer T(T≤6000), indicating the number of test cases. The following 2T lines describe the test cases.

In each test case, the first line contains an integer n, indicating the number of slots. The second line contains n integers v1,v2…vn, indicating the volume of the boxes. It is guaranteed that all vi in a test case are different.

Please note that n<8,0≤vi≤104

输出

For each test case, print a line containing one integer indicating the answer. If it’s impossible to sort the boxes, print -1.

样例输入
4
3
2 1 3
2
7 8
2
10000 1000
3
97 96 95
样例输出
4
0
-1
20
题意:有n个卡槽,放有体积不同的n个空盒子,每次你可以移动一个空盒子到相邻卡槽,但前提是相邻卡槽若已经有空盒子,那么要移动的空盒子体积必须小于已有的空盒子,问要移动多少步才能使得从左到右,每个卡槽空盒子的体积递增。
现用①表示1号卡槽,②表示2号卡槽,依次类推……
样例1的移动方式:1->③,2->②,1->②,1->①
样例3的移动方式:95->②,95->①,96->③,95->②,95->③,97->②,95->②,95->①,96->②,95->②,95->③,96->①,95->②,95->①,97->③,95->②,95->③,96->②,95->②,95->①
题解:状态压缩,状态表示每个盒子所在的位置,每个盒子的位置用三位二进制表示,总共的状态为2^21,因为每次盒子只能向左放或者向右放,那么我们从结束的位置开始搜索,bfs预处理出目标状态到所有状态所需要的最小步数,然后对于每个询问O(1)回答即可。

#include<bits/stdc++.h>
using namespace std;

const int MAXN = 5000000;
int n;
int a[10], b[10];
int vis[MAXN];
bool flag[10];
void bfs(int n)
{  
	queue<int> q;
	int tmp = 0;
	for(int i = 0; i < n; i++)
	{
		tmp += (i+1) * (1 << (3*i));
	}//比如说n=7时,tem = 7 * 3^6 + 6 *3^5 + 5 * 3^4 + 4 * 3^3 + 3 * 3^2 + 2 * 3^1 + 1 * 3^0
	q.push(tmp);
	vis[tmp] = 0;
	while(!q.empty())
	{
		int s = q.front(); 
		q.pop();
		memset(flag, 0, sizeof(flag));
		for(int i = 0; i < n; i++)  	//i+1在哪个位置 
		{
			int pos =  (s >> (3*i)) % 8;	//取第i+1个位置的盒子 
			if(flag[pos]) continue;	//判断该盒子是否已经移动过
			flag[pos] = 1;
			if(pos>1 && !flag[pos-1])
			{
				int t = s - (1<<(3*i));//盒子左移
				if(vis[t]==-1)
				{
					q.push(t);
					vis[t] = vis[s] + 1;
				} 
			}
			if(pos<n && !flag[pos+1])
			{
				int t = s + (1<<(3*i));//盒子右移
				if(vis[t]==-1)
				{
					q.push(t);
					vis[t] = vis[s] + 1;
				}
			}
		}
	}
}
int main() {
    
	int t; 
	cin >> t;
	memset(vis, -1, sizeof(vis));
	for(int i = 1; i < 8; i++){
		bfs(i);
	} 
	
	while(t--) {
		scanf("%d", &n);
		for(int i = 1; i <= n; i++){
			scanf("%d", &a[i]), b[i] = a[i];
		}
		sort(b+1, b+n+1);
		for(int i = 1; i <= n; i++){
			a[i] = lower_bound(b+1, b+n+1, a[i]) - b - 1;//因为移动的步数仅跟各盒子的大小关系有关,与具体体积大小无关,故记为1~n
		}
		int s = 0;
		for(int i = 1; i <= n; i++)
		{
			s += i*(1<<(3*a[i]));//将各个盒子的位置表示成一个三位二进制表示数
		}
		printf("%d\n", vis[s]);
	}
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值