hihoCoder 1233 Boxes(状态压缩 + bfs)——ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛

题目7 : 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<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<ctime>
#define eps 1e-6
#define LL long long
#define pii pair<int, int>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

const int MAXN = 5000000;
int n;
int a[10], b[10];
int vis[MAXN];
bool fuck[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(fuck, 0, sizeof(fuck));
		for(int i = 0; i < n; i++) { //i+1在哪个位置 
			int pos =  (s >> (3*i)) % 8;//取第i+1个位置的盒子 
			if(fuck[pos]) continue;//判断该盒子是否已经移动过
			fuck[pos] = 1;
			if(pos>1 && !fuck[pos-1]) {
				int t = s - (1<<(3*i));//盒子左移
				if(vis[t]==-1) {
					q.push(t);
					vis[t] = vis[s] + 1;
				} 
			}
			if(pos<n && !fuck[pos+1]) {
				int t = s + (1<<(3*i));//盒子右移
				if(vis[t]==-1) {
					q.push(t);
					vis[t] = vis[s] + 1;
				}
			}
		}
	}
}
int main() {
    //freopen("input.txt", "r", stdin);
	int T; cin >> T;
	memset(vis, -1, sizeof(vis));
	for(int i = 1; i <= 7; 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;
}

菜鸟成长记

 
 


你可能感兴趣的:(算法,ACM,bfs,状态压缩)