【搜索】 HDOJ 5012 Dice
2014-09-15 09:46
323 查看
水题。。。爆搜即可。。。
#include <iostream> #include <queue> #include <stack> #include <map> #include <set> #include <bitset> #include <cstdio> #include <algorithm> #include <cstring> #include <climits> #include <cstdlib> //#include <cmath> #include <time.h> #define maxn 500005 #define maxm 100005 #define eps 1e-10 #define mod 1000000007 #define INF 999999999 #define lowbit(x) (x&(-x)) #define mp mark_pair #define ls o<<1 #define rs o<<1 | 1 #define lson o<<1, L, mid #define rson o<<1 | 1, mid+1, R //typedef long long LL; typedef int LL; using namespace std; LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;} void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();} LL gcd(LL _a, LL _b){if(!_b) return _a;else return gcd(_b, _a%_b);} // head struct node { int x[10]; int step; }tmp, now; int vis[8][8][8][8][8][8]; int a[10], b[10]; queue<node> q; void read(void) { for(int i = 2; i <= 6; i++) scanf("%d", &a[i]); for(int i = 1; i <= 6; i++) scanf("%d", &b[i]); } void bfs(void) { int ans = INF, ok; for(int i = 1; i <= 6; i++) tmp.x[i] = a[i], tmp.step = 0; q.push(tmp); while(!q.empty()) { now = q.front(); q.pop(); if(vis[now.x[1]][now.x[2]][now.x[3]][now.x[4]][now.x[5]][now.x[6]]) continue; vis[now.x[1]][now.x[2]][now.x[3]][now.x[4]][now.x[5]][now.x[6]] = 1; ok = 1; for(int i = 1; i <= 6; i++) if(now.x[i] != b[i]) { ok = 0; break; } if(ok) { ans = min(now.step, ans); continue; } for(int i = 0; i < 4; i++) { if(i == 0) { tmp.x[1] = now.x[6]; tmp.x[2] = now.x[5]; tmp.x[3] = now.x[3]; tmp.x[4] = now.x[4]; tmp.x[5] = now.x[1]; tmp.x[6] = now.x[2]; } if(i == 1) { tmp.x[1] = now.x[5]; tmp.x[2] = now.x[6]; tmp.x[3] = now.x[3]; tmp.x[4] = now.x[4]; tmp.x[5] = now.x[2]; tmp.x[6] = now.x[1]; } if(i == 2) { tmp.x[1] = now.x[4]; tmp.x[2] = now.x[3]; tmp.x[3] = now.x[1]; tmp.x[4] = now.x[2]; tmp.x[5] = now.x[5]; tmp.x[6] = now.x[6]; } if(i == 3) { tmp.x[1] = now.x[3]; tmp.x[2] = now.x[4]; tmp.x[3] = now.x[2]; tmp.x[4] = now.x[1]; tmp.x[5] = now.x[5]; tmp.x[6] = now.x[6]; } tmp.step = now.step + 1; q.push(tmp); } } if(ans == INF) printf("-1\n"); else printf("%d\n", ans); } void work(void) { memset(vis, 0, sizeof vis); bfs(); } int main(void) { while(scanf("%d", &a[1])!=EOF) { read(); work(); } return 0; }
相关文章推荐
- hdu 5012 Dice 记忆化搜索
- HDOJ-5012-Dice 解题报告
- HDOJ 5012 Dice--2014网络赛西安赛区F题
- HDU 5012 Dice 隐式图的搜索
- HDOJ 5012 Dice
- 【搜索】 HDU 5012 Dice
- 【搜索】 HDU 5012 Dice
- hdu 5012 Dice(隐式图搜索)
- hud 5012 Dice(隐式图的搜索 )
- Dice - HDU 5012 搜索
- [HDOJ 4876] ZCC loves cards [搜索]
- hdu5012 Dice
- HDOJ搜索专题之胜利大逃亡
- hdoj 3345 War Chess (搜索,优先队列)
- UVA - 810 (A Dice problem 搜索)
- HDOJ 1428 漫步校园 (spfa+记忆搜索)
- HDOJ2141(map在二分搜索中的应用)
- HDOJ 1078 记忆搜索
- HDOJ搜索题(总计100题)
- HDOJ-1547 Bubble Shooter 简单搜索