Gap bfs()+hash判重
2013-11-16 14:07
169 查看
[align=left]Problem Description[/align]
Let's play a card game called Gap.
You have 28 cards labeled with two-digit numbers. The first digit (from 1 to 4) represents the suit of the card, and the second digit (from 1 to 7) represents the value of the card.
First, you shu2e the cards and lay them face up on the table in four rows of seven cards, leaving a space of one card at the extreme left of each row. The following shows an example of initial layout.
View Code
Let's play a card game called Gap.
You have 28 cards labeled with two-digit numbers. The first digit (from 1 to 4) represents the suit of the card, and the second digit (from 1 to 7) represents the value of the card.
First, you shu2e the cards and lay them face up on the table in four rows of seven cards, leaving a space of one card at the extreme left of each row. The following shows an example of initial layout.
#include<iostream> #include<string> #include<cstring> #include<cmath> #include<queue> #include<cstdio> using namespace std; #define LL __int64 #define maxn 1000000+7 LL hash[maxn]; LL bin[34]; LL endval,n,m,flag; int ans,cas; int tmp[4][8]; int i,j,k; int faval[4][8]= { 11,12,13,14,15,16,17,0, 21,22,23,24,25,26,27,0, 31,32,33,34,35,36,37,0, 41,42,43,44,45,46,47,0 }; struct node { LL val; int mp[4][8]; int cnt; int blankx[4],blanky[4]; }cur,now; queue<node>Q; void premap()//第一步操作都相同 { int it,jt; for(it=0;it<4;it++) for(jt=1;jt<8;jt++) { if(cur.mp[it][jt]==11) { swap(cur.mp[it][jt],cur.mp[0][0]); cur.blankx[0]=it; cur.blanky[0]=jt; } else if(cur.mp[it][jt]==21) { swap(cur.mp[it][jt],cur.mp[1][0]); cur.blankx[1]=it; cur.blanky[1]=jt; } else if(cur.mp[it][jt]==31) { swap(cur.mp[it][jt],cur.mp[2][0]); cur.blankx[2]=it; cur.blanky[2]=jt; } else if(cur.mp[it][jt]==41) { swap(cur.mp[it][jt],cur.mp[3][0]); cur.blankx[3]=it; cur.blanky[3]=jt; } } } bool Is_can()//判断状态是否符合结果 { int it,jt; LL s=0; for(it=0;it<4;it++) for(jt=0;jt<8;jt++) { s+=cur.mp[it][jt]*bin[it*8+jt]; } if(s==endval)return true; return false; } bool hash_add()//hash判重 { int it,jt; if(Is_can())flag=1; int ss=(int)(cur.val%maxn); while(hash[ss]!=-1&&hash[ss]!=cur.val) { ss+=10; if(ss>=maxn) ss=ss%maxn; } if(hash[ss]==-1) { hash[ss]=cur.val; return true; } return false; } bool bfs()//bfs()判断最符合的状态 { int it,jt,kt; LL tval; while(!Q.empty()) Q.pop(); Q.push(cur); while(!Q.empty()) { now=Q.front(); Q.pop(); for(kt=0;kt<4;kt++) { int xx=now.blankx[kt]; int yy=now.blanky[kt]; memcpy(tmp,now.mp,sizeof(now.mp)); for(it=0;it<4;it++) for(jt=0;jt<8;jt++) { if(tmp[it][jt]==0)continue; if(tmp[xx][yy-1]+1==tmp[it][jt]) { tval=now.val; tval-=tmp[it][jt]*bin[it*8+jt]; tval+=tmp[it][jt]*bin[xx*8+yy]; swap(tmp[xx][yy],tmp[it][jt]); cur.val=tval; memcpy(cur.mp,tmp,sizeof(tmp)); if(hash_add()) { if(flag) { ans=now.cnt+1; return true; } memcpy(cur.blankx,now.blankx,sizeof(cur.blankx)); memcpy(cur.blanky,now.blanky,sizeof(cur.blanky)); cur.cnt=now.cnt+1; cur.blankx[kt]=it; cur.blanky[kt]=jt; Q.push(cur); } } } } } return false; } int main() { bin[0]=1; for(i=1;i<=33;i++) bin[i]=bin[i-1]<<1; endval=0; for(i=0;i<4;i++) for(j=0;j<8;j++) endval+=faval[i][j]*bin[i*8+j]; //cout<<"endval:: "<<endval<<endl; scanf("%d",&cas); while(cas--) { flag=0; LL s=0; memset(hash,-1,sizeof(hash)); for(i=0;i<4;i++) { cur.blankx[i]=i; cur.blanky[i]=0; cur.mp[i][0]=0; for(j=1;j<8;j++) { scanf("%d",&cur.mp[i][j]); s+=cur.mp[i][j]*bin[i*8+j]; } } cur.val=s; cur.cnt=0; premap(); hash_add(); if(flag) printf("0\n"); else { if(bfs()) printf("%d\n",ans); else printf("-1\n"); } } return 0; }
View Code
相关文章推荐
- HDU 1067 Gap bfs+hash
- HDU 1067 Gap BFS+Hash
- 杭电1067 Gap(BFS+map)
- hdu 1067 && poj 2046 Gap (bfs+hash)
- hdoj1067 Gap(bfs+hash)
- POJ-2046---Gap (bfs+hash)
- HDU1067 Gap 【BFS+MAP】
- BFS+Hash(储存,判重) HDOJ 1067 Gap
- (*)[kuangbin带你飞]专题二 搜索进阶 H-Gap(bfs+hash)
- HDU - 1067 Gap (bfs + hash) [kuangbin带你飞]专题二
- hdu 1067 Gap+BFS+hash
- 八数码问题(bfs,hash判重)
- HDU-1067-Gap(BFS+HASH)
- hdu 1067 Gap bfs+hash
- HDU 1067 Gap(BFS+hash)
- hdu 1067 Gap bfs+hash
- HDU 1067 GAP (BFS + HASH)
- HDU1067 Gap( BFS+ HASH 剪枝,矩阵用一个数表示)
- poj 2046 Gap(bfs+hash)
- hdu1067 Gap (bfs+hash)