POJ-3185-The Water Bowls
来源:互联网 发布:audition mac 破解 编辑:程序博客网 时间:2024/05/18 03:50
这个题最开始用了错误的想法,后来用BFS写的,果断超时,结果看到用的STL的queue,然后就改为数组,一交就AC了。思路那些都比较简单。
代码:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<queue>using namespace std;const int inf=1<<29;const int maxn=1<<20;struct Node{ int s; int ans; Node(){} Node(int ss,int sans):s(ss),ans(sans){}}q[maxn];bool vis[maxn];int s,a[30];bool check(int val){ int ss=s; for(int i=0;i<20;i++) if((val>>i)&1) { ss^=1<<i; if(i>0) ss^=1<<(i-1); if(i<19) ss^=1<<(i+1); } if(ss==0) return true; return false;}int BFS(){ memset(vis,0,sizeof(vis)); int pre=0,last=1; q[last++]=(Node(0,0)); vis[0]=1; while(pre<last) { Node u=q[pre++]; for(int i=0;i<20;i++) { if(!vis[u.s|(1<<i)]) { if(check(u.s|(1<<i))) return u.ans+1; vis[u.s|(1<<i)]=1; q[last++]=Node(u.s|(1<<i),u.ans+1); } } }}int main(){ while(scanf("%d",&a[0])!=EOF) { if(a[0]) s=1; else s=0; for(int i=1;i<20;i++) { scanf("%d",&a[i]); if(a[i]) s|=(1<<i); } if(s==0) { printf("0\n"); continue; } printf("%d\n",BFS()); } return 0;}
0 0
- poj 3185 The Water Bowls
- POJ 3185 (The Water Bowls)
- POJ 3185 The Water Bowls
- poj 3185 The Water Bowls
- POJ 3185 The Water Bowls
- POJ 3185 The Water Bowls
- POJ-3185-The Water Bowls
- poj-3185 The Water Bowls
- The Water Bowls POJ 3185
- poj 3185 The Water Bowls 高斯消元
- poj -- 3185 The Water Bowls(高斯消元)
- poj 3185 The Water Bowls(高斯消元)
- poj 3185 The Water Bowls(高斯消元)
- POJ 3185 The Water Bowls(高斯消元)
- poj 3185 The Water Bowls (高斯消元)
- POJ 3185 The Water Bowls(高斯消元)
- POJ - 3185 The Water Bowls (反转)
- POJ 3185 The Water Bowls (高斯消元)
- ARC指南1 - strong和weak指针
- lamp 环境搭建的心得与体会
- 变量的作用域
- 今天自己的CSDN博客终于开通了
- http协议学习转载
- POJ-3185-The Water Bowls
- HDU 2059 龟兔赛跑(DP和多阶段策略问题,基础题)
- Linux 设备驱动
- jsp开发学生信息管理系统,实现了基本的增删改查的功能
- 类型转换
- servlet 中使用 quartz2.0.2
- HTML中的object、param标记
- js中2个等号与3个等号的区别
- 25XML解析--DOM