[USACO] The Clocks [位操作 BFS]
来源:互联网 发布:淘宝开店怎样上传宝贝 编辑:程序博客网 时间:2024/06/03 22:58
思路:
位操作+广搜。
1到9个时钟,每个时钟状态用两位表示 ,共18位。即每个状态用一个int表示即可。
然后直接广搜,每个状态都是一个int型,每次某状态到达用一个hash表记录。
一次AC。
位操作写得很蛋疼,事后查了一下网上报告,好像没这么做的,就记录一下把。
/*ID:wuyanyi1PROG:clocksLANG:C++*/#include<iostream>#include<fstream>#include<map>#include<vector>#include<string>#include<memory.h>#include<algorithm>#include<queue>#define Min(a,b) (a<b?a:b)#define Max(a,b) (a>b?a:b)#define llong long long intusing namespace std;const int N=10,inf=(1<<30);const int M=(1<<18)-1;int n;int mp[N][N]={{},{4,1,2,4,5},{3,1,2,3},{4,2,3,5,6},{3,1,4,7},{5,2,4,5,6,8},{3,3,6,9},{4,4,5,7,8},{3,7,8,9},{4,5,6,8,9}};int a[N];int vis[M+10];int val[M+10];int chbit(int state,int pos,int bit){if(bit){unsigned int now=1;now<<=pos;state|=now;}else{unsigned int now=M-1;now=(now<<pos)|(now>>(18-pos));state&=now;}return state&M;}int oper(int state,int k){for(int i=1;i<=mp[k][0];i++){int pos=(9-mp[k][i])*2;int now=(state>>pos)&1;//printf("%d ",now);now+=((state>>(pos+1))&1)*2;//printf("%d %d\n",now,pos);if(now==3){state=chbit(state,pos,0);state=chbit(state,pos+1,0);}else{now++;state=chbit(state,pos,now&1);state=chbit(state,pos+1,(now>>1)&1);}}return state&M;}void bfs(int s){queue<int> que;que.push(s);vis[s]=-1;while(!que.empty()){int now=que.front();que.pop();for(int i=1;i<=9;i++){int t=oper(now,i);if(!vis[t]){que.push(t);vis[t]=now;val[t]=i;if(!t){return;}}}}}void print(int i){if(vis[i]!=-1){print(vis[i]);printf("%d",val[i]);if(i) printf(" ");else printf("\n");}/*else{printf("",val[i]);}*/}int main(){int s=0;freopen("clocks.in","r",stdin);freopen("clocks.out","w",stdout);for(int i=1;i<=9;i++){scanf("%d",a+i);a[i]/=3;a[i]%=4;//printf("%d",a[i]);s<<=2;s|=a[i];}bfs(s);print(0);//printf("\n");return 0;}
- [USACO] The Clocks [位操作 BFS]
- USACO 1.4 The Clocks (位操作+枚举+BFS)
- USACO 1.4 The Clocks (位操作+枚举+DFS)
- USACO/The Clocks(DFS,BFS,枚举)
- USACO 6.5.4 The Clocks bfs
- USACO 1.4 The Clocks (clocks)
- USACO 1.4-The Clocks
- the clocks(usaco)
- USACO The clocks, ariprog
- USACO the clocks
- usaco The clocks
- USACO:The Clocks
- USACO The Clocks
- USACO 1.4 The Clocks
- USACO Section 1.4 The Clocks (clocks)
- USACO S1.4 The Clocks
- [USACO 1.4.2] The Clocks
- usaco 1.5.2 The Clocks
- 蛇形数组编程题
- 10款jquery弹出层 动画效果应有尽有
- Unity的网络功能
- 推荐免费黑色UI工具包下载
- 关于记事本[另存为]时,提示“共用对话框错误”的问题
- [USACO] The Clocks [位操作 BFS]
- poj 2299 Ultra-QuickSort(利用树状数组变形,求一个数左边比它大的数的个数)
- printf到printk的转变
- 带给你设计灵感的30个超棒的暗色系网站设计
- Android上常见度量单位【xdpi、hdpi、mdpi、ldpi】解读
- 一个基于jQuery Mobile的移动设备实时幻灯javascript类库 - taciónJS
- USB 3G网卡驱动流程
- 谈C++泛型
- Makefile学习之使用条件判断