poj 2046 Gap
2015-08-31 16:01
399 查看
原题链接:http://poj.org/problem?id=2046
爆搜+哈希判重。。
爆搜+哈希判重。。
[code]#include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<vector> #include<queue> #include<set> using std::set; using std::pair; using std::swap; using std::queue; using std::vector; using std::multiset; #define pb(e) push_back(e) #define sz(c) (int)(c).size() #define mp(a, b) make_pair(a, b) #define all(c) (c).begin(), (c).end() #define iter(c) __typeof((c).begin()) #define cls(arr, val) memset(arr, val, sizeof(arr)) #define cpresent(c, e) (find(all(c), (e)) != (c).end()) #define rep(i, n) for(int i = 0; i < (int)n; i++) #define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i) const int N = 1000007; const int MOD = 100000007; const int INF = 0x3f3f3f3f; typedef long long ll; struct Node { int s; int mat[4][8]; inline bool operator==(const Node &x) const { rep(i, 4) { rep(j, 8) { if(mat[i][j] != x.mat[i][j]) return false; } } return true; } inline int hash() const { ll ret = 0, k = 1; rep(i, 4) { rep(j, 8) { ret = (ret + k * mat[i][j]) % MOD; k <<= 1; } } return (int)ret; } inline void read() { rep(i, 4) { mat[i][0] = 0; rep(j, 7) { scanf("%d", &mat[i][j + 1]); } } rep(i, 4) { rep(j, 8) { int v = mat[i][j]; if(1 == v % 10) { swap(mat[i][j], mat[v / 10 -1][0]); } } } } }start, goal; struct Hash_Set { int tot, A , head , next ; inline void init() { tot = 0, cls(head, -1); } inline bool insert(const int val) { int u = val % N; for(int i = head[u]; ~i; i = next[i]) { if(A[i] == val) return false; } A[tot] = val, next[tot] = head[u]; head[u] = tot++; return true; } }hash; void bfs() { hash.init(); queue<Node> q; q.push(start); hash.insert(start.hash()); while(!q.empty()) { Node x = q.front(); q.pop(); rep(i, 4) { rep(j, 8) { if(x.mat[i][j]) continue; Node t = x; int x1 = 0, y1 = 0, val = x.mat[i][j - 1] + 1; if(1 == val || 8 == val % 10) continue; rep(k, 4) { rep(l, 8) { if(val == x.mat[k][l]) { x1 = k, y1 = l; k = 4; break; } } } t.s = x.s + 1; swap(t.mat[i][j], t.mat[x1][y1]); if(t == goal) { printf("%d\n", t.s); return; } val = t.hash(); if(!hash.insert(val)) continue; q.push(t); } } } puts("-1"); } void solve() { start.read(); rep(i, 4) { rep(j, 7) { goal.mat[i][j] = (i + 1) * 10 + (j + 1); } goal.mat[i][7] = 0; } if(start == goal) { puts("0"); return; } bfs(); } int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w+", stdout); #endif int t; scanf("%d", &t); while(t--) { solve(); } return 0; }
相关文章推荐
- 复习数据库
- gobject type system
- 使用Fresco实现简单的显示一张图片
- Linux makefile 教程 非常详细,且易懂
- java普通算法
- Telnet客户端添加、使用
- FindBugs && Lint
- NoSql——redis(高级操作)
- 三目运算符“?:”省略中间操作数
- iOS 的 APP 如何适应 iPhone 5s/6/6Plus 三种屏幕的尺寸?
- Android上积累代码
- 深浅复制
- Java 7之多线程第1篇 - 线程的状态转换
- Socket 通信(基础原理、实时聊天系统雏形)
- 微信开发第七篇发送素材(图文)信息
- 手势密码(三)
- jquery datatable(二)
- 学习日志---机器学习导论
- node.js中util模块介绍
- 计算经纬度