hihocoder 1233 Boxes(bfs+状态压缩+hash表)
来源:互联网 发布:蝴蝶效应 知乎 编辑:程序博客网 时间:2024/06/04 20:04
题意:
有n个卡槽,放有体积不同的n个空盒子,每次你可以移动一个空盒子到相邻卡槽,但前提是相邻卡槽若已经有空盒子,那么要移动的空盒子体积必须小于已有的空盒子,问要移动多少步才能使得从左到右,每个卡槽空盒子的体积递增。
解析:
状态压缩,用一个
long long 的状态表示当前所有的状态,每个盘子所在的位置,每个盘子的位置用7 位二进制表示,总共的状态为247 ,因为每次盘子只能向左放或者向右放,那么我们从结束的位置开始搜索,bfs 预处理出目标状态到所有状态所需要的最小步数插入hash 表中(反方向建表),然后对于每个询问O(1) 回答即可。
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;typedef long long ll;const int MAXSIZE = 1000003;const int N = 7;const int dir[] = {-1, 1};int goal[] = {1, 2, 3, 4, 5, 6, 7};int bit[65];struct Node { ll state; int step; };int v[N], save[N], n;struct HashTable { int head[MAXSIZE], next[MAXSIZE]; ll state[MAXSIZE], dist[MAXSIZE]; int sz; void clear() {sz = 1; memset(head, 0, sizeof(head)); } int getHash(ll state) { return state % MAXSIZE; } bool insert(ll st, ll step) { int h = getHash(st); int u = head[h]; while(u) { if(st == state[u]) return false; u = next[u]; } state[sz] = st; dist[sz] = step; next[sz] = head[h]; head[h] = sz++; return true; } int find(ll st) { int h = getHash(st); int u = head[h]; while(u) { if(st == state[u]) return dist[u]; u = next[u]; } return -1; }} table[7];ll getState(int a[]) { ll state = 0; for(int i = 0; i < n; i++) state = (state << 7) + (1 << (a[i] - 1)); return state;}inline int lowbit(int x) { return x & (-x); };inline ll getSlot(ll state, int pos) { return ( state >> (7 * pos) ) % 128; }inline bool move(ll &state, int cur, int next) { ll x = getSlot(state, cur); ll curTop = bit[lowbit(x)]; x = getSlot(state, next); ll nextTop = bit[lowbit(x)]; if(curTop > nextTop && nextTop != 0) return false; state = state^(1ll << (curTop - 1 + 7 * cur))^(1ll << (curTop - 1 + 7 * next)); return true;}void bfs(int id) { queue<Node> que; ll endState = getState(goal); que.push( (Node){endState, 0} ); table[id].insert(endState, 0); while(!que.empty()) { Node node = que.front(); que.pop(); for(int i = 0; i < n; i++) { if(getSlot(node.state, i) == 0) continue; for(int d = 0; d < 2; d++) { int next = i + dir[d]; if(next < 0 || next >= n) continue; Node newNode = node; if(move(newNode.state, i, next)) { newNode.step = node.step + 1; if(table[id].insert(newNode.state, newNode.step)) que.push(newNode); } } } }}void prepare() { bit[0] = 0; for(int i = 0; i < 7; i++) bit[1 << i] = i+1; for(int i = 0; i < 7; i++) { n = i+1; table[i].clear(); bfs(i); }}void discrete() { for(int i = 0; i < n; i++) save[i] = v[i]; sort(save, save+n); for(int i = 0; i < n; i++) v[i] = lower_bound(save, save+n, v[i]) - save + 1;}int main() { prepare(); int T; scanf("%d", &T); while(T--) { scanf("%d", &n); for(int i = 0; i < n; i++) scanf("%d", &v[i]); discrete(); printf("%d\n", table[n-1].find( getState(v) )); } return 0;}
0 0
- hihocoder 1233 Boxes(bfs+状态压缩+hash表)
- hihoCoder 1233 Boxes(状态压缩)
- hihocoder #1233 : Boxes 北京赛区(2015)网络赛(状态压缩+bfs)
- HihoCoder 1233 Boxes(bfs打表)
- hihoCoder 1233 Boxes(bfs)
- HihoCoder 1233 Boxes BFS
- hiho 1233 Boxes(状态压缩+BFS)
- HihoCoder 1233 Boxes (bfs 状压)
- hihoCoder 1233 Boxes(状态压缩 + bfs)——ACM-ICPC国际大学生程序设计竞赛北京赛区(2015)网络赛
- hihocoder 1233 Boxes
- hihocoder 1233Boxes
- hihoCoder 1233 Boxes
- hdu 4090 bfs+dfs+状态压缩+hash判重
- hihocoder#1044之状态压缩(一)
- hihocoder 状态压缩二
- 【hihocoder】状态压缩dp
- HihoCoder 状态压缩一
- HihoCoder 状态压缩二
- Android开发中怎样调用系统Email发送邮件(多种调用方式)
- hdu 1937 Finding Seats
- Java 装箱和拆箱
- Bluetooth SIG profiles
- Android 自定义 DialogFragment 宽度问题
- hihocoder 1233 Boxes(bfs+状态压缩+hash表)
- linux中对文件名包含空格时处理异常的解决方法
- js的时间戳和php的时间戳转换需要注意的地方
- 注册功能实现
- ZOJ.2829 Beautiful Number【水】 2015/09/22
- 友善之臂第一个裸板驱动LED程序的详细分析(新手向)
- 【学习笔记】系列二:Linux安装配置apache
- document.addEventListener理解
- Android 关闭多个Activity