PKU 3185 The Water Bowls

不喜欢模拟,直接BFS+位压缩保存状态~300+ms还是可以接受的

虽然模拟是0ms...囧~

  1. #include <iostream>
  2. #include <list>
  3. #include <map>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <bitset>
  7. #include <queue>
  8. #include <stack>
  9. #include <vector>
  10. using namespace std;
  11. bool hash[1048576];
  12. int c[21]={0,1};
  13. struct state
  14. {
  15.     int water,t;
  16. }tmp,p;
  17. queue<state> q;
  18. int main()
  19. {
  20.     int i,hash_tmp=0,t,res=0;
  21.     for(i=2;i<=20;i++)c[i]=c[i-1]*2;
  22.     for(i=1;i<=20;i++)scanf("%d",&t),hash_tmp=2*hash_tmp+t;
  23.     memset(hash,false,sizeof(hash));
  24.     hash[hash_tmp]=true;
  25.     tmp.water=hash_tmp;tmp.t=0;
  26.     if(tmp.water==0)cout<<0<<endl;
  27.     else{
  28.     q.push(tmp);
  29.     while(!q.empty())
  30.         {
  31.             tmp=q.front();tmp.t++;
  32.             for(i=1;i<=20;i++)
  33.                 {
  34.                     p=tmp;
  35.                             p.water^=c[i];
  36.                             if(i-1>=1)p.water^=c[i-1];
  37.                             if(i+1<=20)p.water^=c[i+1];
  38.                             if(hash[p.water])continue;
  39.                             q.push(p);
  40.                             hash[p.water]=true;
  41.                             if(p.water==0)break;
  42.                 }
  43.             q.pop();
  44.             if(p.water==0)break;
  45.         }   
  46.     printf("%d/n",p.t);
  47.     }
  48.     return 0;
  49. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值