USACO Section 3.2 Magic Squares - 我承认我是瞎混过去的..
2011-12-14 19:43
363 查看
首先题目看清楚.是从1,2,3,4,5,6,7,8到目前状态..然后..我就是很恶心的Hash+暴力BFS了..
我的Hash很恶心...并且是在WA了几次后修改对的..好邪恶..我觉得好的方法应该是Astar或者更加科学的Hash吧..其实这道题的每个状态是可以明确表示出来的..因为总的状态数也就8!=40320种~感觉好麻烦..我就恶心的Hash了..
Program:
我的Hash很恶心...并且是在WA了几次后修改对的..好邪恶..我觉得好的方法应该是Astar或者更加科学的Hash吧..其实这道题的每个状态是可以明确表示出来的..因为总的状态数也就8!=40320种~感觉好麻烦..我就恶心的Hash了..
Program:
/* ID: zzyzzy12 LANG: C++ TASK: msquare */ #include<iostream> #include<istream> #include<stdio.h> #include<string.h> #include<math.h> #include<stack> #include<algorithm> #include<queue> #define oo 2000000000 #define ll long long using namespace std; struct node { string ans; int a[8]; }h,k; queue<node> myqueue; int i,p,t,goal,g[3][8]={{7,6,5,4,3,2,1,0},{3,0,1,2,5,6,7,4},{0,6,1,3,4,2,5,7}}; bool used[5000001]; int getdata(node p) { int i,m=0,k=1; for (i=0;i<8;i++) { m+=k*p.a[i]; k=(k*17)%387723; } return m; } int main() { freopen("msquare.in","r",stdin); freopen("msquare.out","w",stdout); while (!myqueue.empty()) myqueue.pop(); memset(used,false,sizeof(used)); for (i=0;i<8;i++) scanf("%d",&h.a[i]); goal=getdata(h); for (i=0;i<=8;i++) h.a[i]=i+1; h.ans=""; t=getdata(h); myqueue.push(h); used[t]=true; if (t==goal) k=h; else while (1) { h=myqueue.front(); myqueue.pop(); for (p=0;p<3;p++) { k.ans=h.ans+char('A'+p); for (i=0;i<8;i++) k.a[i]=h.a[g[p][i]]; t=getdata(k); if (t==goal) break; if (!used[t]) myqueue.push(k); used[t]=true; } if (t==goal) break; } p=k.ans.length(); printf("%d\n",p);; for (i=0;i<p;i++) { printf("%c",k.ans[i]); if (i%60==59) printf("\n"); } printf("\n"); return 0; }
相关文章推荐
- USACO Section 3.2 Feed Ratios (ratios)
- USACO Section 3.2 Stringsobits(dp)
- USACO Section 3.2: Stringsobits
- USACO Section 3.2 PROB Spinning Wheels
- USACO section 3.2 Feed Ratios(高斯定理)
- USACO 3.2 Magic Squares 魔板 (BFS-HASH)
- 【USACO 3.2】Magic Squares
- USACO Section 3.2 Spinning Wheels(模拟)
- USACO section 3.2 Magic Squares(STL+广搜)
- USACO 3.2 Magic Squares 魔板 (BFS-HASH)
- USACO 3.2 Magic Squares 魔板 (BFS-HASH)
- USACO section 3.2 Magic Squares(STL+广搜)
- USACO 3.2 Magic Squares 魔板 ssl 1692 BFS-HASH
- USACO section 3.2 Sweet Butter(SPFA)
- USACO 3.2 Magic Squares 魔板 (BFS-HASH)
- usaco Chapter 3 section 3.2 Magic Squares
- USACO_3.2_Magic Squares 魔板_BFS_HASH
- USACO section 3.2 Sweet Butter(SPFA)
- USACO-Section 3.2 Magic Squares(BFS)
- USACO 3.2 Magic Squares 魔板