bzoj1054: [HAOI2008]移动玩具
2017-10-23 09:19
253 查看
就是把矩阵当成二进制数判断是否访问过,然后就能AC了。。。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; struct data { int a[5][5]; int step; int num; }; queue<data> q; int read() { char ch=getchar();int f=0; while(ch<'0'||ch>'9')ch=getchar(); while(ch>='0'&&ch<='9'){f=(f<<1)+(f<<3)+ch-'0';ch=getchar();} return f; } int vis[100005],pur,s1[5][5],now[5][5]; int movex[5]={0,0,0,1,-1},movey[5]={0,-1,1,0,0}; char s[5][5],t[5][5]; void bfs() { while(!q.empty()) { data x=q.front(); q.pop(); //cout<<x.num<<" "; for(int i=1;i<=4;i++) { for(int j=1;j<=4;j++) { now[i][j]=x.a[i][j]; } } for(int i=1;i<=4;i++) { for(int j=1;j<=4;j++) { if(now[i][j]==1) { for(int k=1;k<=4;k++) { if(!now[i+movex[k]][j+movey[k]]&&i+movex[k]>=1&&i+movex[k]<=4&&j+movey[k]>=1&&j+movey[k]<=4) { now[i+movex[k]][j+movey[k]]=1; now[i][j]=0; int temp=0,now1=1; for(int o=1;o<=4;o++) { for(int p=1;p<=4;p++) { temp+=now1*now[o][p]; now1<<=1; } } //cout<<temp<<" "; if(vis[temp]>vis[x.num]+1) { vis[temp]=vis[x.num]+1; if(temp==pur) return; data x; x.num=temp; x.step=vis[temp]; for(int o=1;o<=4;o++) { for(int p=1;p<=4;p++) { x.a[o][p]=now[o][p]; } } q.push(x); } now[i+movex[k]][j+movey[k]]=0; now[i][j]=1; } } } } } } } int main() { for(int i=1;i<=4;i++) scanf("%s",s[i]+1); for(int i=1;i<=4;i++) scanf("%s",t[i]+1); int now1=1,temp=0; memset(vis,0x7f7f7f7f,sizeof(vis)); for(int i=1;i<=4;i++) { for(int j=1;j<=4;j++) { pur+=now1*(t[i][j]-'0'); temp+=now1*(s[i][j]-'0'); now1<<=1; s1[i][j]=s[i][j]-'0'; } } //cout<<pur<<" "<<temp; data x; for(int i=1;i<=4;i++) { for(int j=1;j<=4;j++) { x.a[i][j]=s1[i][j]; } } x.step=0; x.num=temp; vis[temp]=0; q.push(x); bfs(); cout<<vis[pur]; }
相关文章推荐
- 【BZOJ 1054】 [HAOI2008]移动玩具
- BZOJ 1054 [HAOI2008]移动玩具 BFS
- 【BFS】bzoj1054 [HAOI2008]移动玩具
- Bzoj 1054 [HAOI2008]移动玩具
- bzoj 1054: [HAOI2008]移动玩具.cpp
- [BZOJ 1054][HAOI 2008]移动玩具 状态压缩
- BZOJ 1054 [HAOI2008]移动玩具
- bzoj 1054: [HAOI2008]移动玩具 bfs
- BZOJ1054 [HAOI2008]移动玩具
- BZOJ大视野 1054: [HAOI2008]移动玩具 解题报告
- 【bzoj1054】[HAOI2008]移动玩具 Bfs
- 【BZOJ 1054】 [HAOI2008]移动玩具
- 【BZOJ 1054】 [HAOI2008]移动玩具 bfs
- [BZOJ 1054][HAOI 2008]移动玩具(BFS+判重)
- [BZOJ1054][HAOI2008]移动玩具(bfs+hash)
- BZOJ 1054 [HAOI2008]移动玩具
- 【BZOJ1054】[HAOI2008]移动玩具 BFS
- bzoj1054: [HAOI2008]移动玩具
- 【BZOJ】1054: [HAOI2008]移动玩具(bfs+hash)
- [BZOJ1054][HAOI2008]移动玩具(bfs+hash)