HDU 1584 bfs
来源:互联网 发布:淘宝信誉 编辑:程序博客网 时间:2024/06/01 10:17
奇葩做法:位压缩+bfs 居然过了 正解应该是dfs+剪枝吧
#include<stdio.h>#include<iostream>#include<cstdio>#include<algorithm>#include<queue>#include<math.h>using namespace std;int posi[10],to[10],mini[10];struct node{ int pos,va,step; node(){}}st;int mi=99999999;int find(int pos,int reg){ int res=to[reg]; while((pos&(1<<res))==0) { res=to[res]; } return res;}void bfs(){ queue<node>q; q.push(st); node k,kk; while(!q.empty()) { k=q.front(); q.pop(); if(k.step==9) { mi=min(mi,k.va); continue; } for(int i=0;i<10;++i) if(k.pos&(1<<i)) { if(posi[i]==10)continue; int res=find(k.pos,i); if(k.pos&(1<<res)){ kk.pos=k.pos-(1<<i); kk.step=k.step+1; kk.va=k.va+abs(i-res); q.push(kk); } } }}int main(){ int t; scanf("%d",&t); while(t--) { memset(to,0,sizeof to); for(int i=0;i<10;++i){scanf("%d",&posi[i]);mini[i]=8888888;} for(int i=0;i<10;++i) for(int j=0;j<10;++j) if(posi[i]==(posi[j]-1)){ to[i]=j; } mi=9999999; st.va=st.step=0; st.pos=(1<<10)-1; bfs(); printf("%d\n",mi); } return 0;}
0 0
- HDU 1584 bfs
- HDU bfs
- hdu 1584 蜘蛛牌(bfs+set)
- hdu 1175 bfs
- HDU 1072(BFS)
- HDU 1429(BFS )
- hdu 1026(BFS)
- HDU 4039 BFS
- hdu 1312 bfs
- hdu 1253 三维bfs
- hdu 1242 bfs
- BFS hdu 1072 Nightmare
- HDU 1072 Nightmare BFS
- bfs hdu 1026
- hdu-2757 BFS
- HDU-1495 BFS
- hdu 1401 bfs
- HDU-2952 BFS
- boost准模板库time_duration类的使用(续)
- Java中的重写与隐藏
- Coder-Strike 2014 - Round 1 B. Network Configuration
- Use MusicBrainz in iOS(三)查询专辑的完整信息
- visual studio 2012中 使用localDB 具体讲解
- HDU 1584 bfs
- Android uevent
- 做图片验证码
- CMOS与TTL比较
- sleep和wait的区别
- 冒泡排序及其改进
- onSaveInstanceState() 和 onRestoreInstanceState()
- Java中关于字符与字符串的处理
- AIX与LINUX