• HDU 1495


    可乐倒来倒去的问题,BFS

      1 #include <iostream>
      2 #include <queue>
      3 #include <cstring>
      4 using namespace std;
      5 struct node{
      6     int s,n,m,t;
      7 }now,nxt;
      8 int s,n,m;
      9 queue<node> q;
     10 int flag[105][105][105];
     11 bool check(node a)
     12 {
     13     return (a.s==a.n&&a.m==0) || (a.s==a.m&&a.n==0) || (a.m==a.n&&a.s==0);
     14 }
     15 int bfs()
     16 {
     17     while(!q.empty()) q.pop();
     18     memset(flag,0,sizeof(flag));
     19     now.s=s; 
     20     now.t=now.n=now.m=0;
     21     q.push(now);    flag[s][0][0]=1;
     22     while(!q.empty())
     23     {
     24         now=q.front(); q.pop();
     25         if(check(now)) return now.t;
     26         nxt.t=now.t+1;
     27         if(now.s<s){//开始强行判断,基本整段复制改一改 
     28             if(now.m>0){
     29                 if(now.m>s-now.s){
     30                     nxt.s=s;
     31                     nxt.m=now.m-(s-now.s);
     32                     nxt.n=now.n;
     33                 } else {
     34                     nxt.s=now.s+now.m;
     35                     nxt.m=0;
     36                     nxt.n=now.n;        
     37                 }
     38                 if(!flag[nxt.s][nxt.m][nxt.n]){
     39                     q.push(nxt); flag[nxt.s][nxt.m][nxt.n]=1;
     40                 } 
     41             }
     42             if(now.n>0){
     43                 if(now.n>s-now.s){
     44                     nxt.s=s;
     45                     nxt.n=now.n-(s-now.s);
     46                     nxt.m=now.m;
     47                 } else {
     48                     nxt.s=now.s+now.n;
     49                     nxt.n=0;
     50                     nxt.m=now.m;        
     51                 }
     52                 if(!flag[nxt.s][nxt.m][nxt.n]){
     53                     q.push(nxt); flag[nxt.s][nxt.m][nxt.n]=1;
     54                 }                 
     55             }
     56         }
     57         if(now.m<m){
     58             if(now.s>0){
     59                 if(now.s>m-now.m){
     60                     nxt.m=m;
     61                     nxt.s=now.s-(m-now.m);
     62                     nxt.n=now.n;
     63                 } else {
     64                     nxt.m=now.m+now.s;
     65                     nxt.s=0;
     66                     nxt.n=now.n;        
     67                 }
     68                 if(!flag[nxt.s][nxt.m][nxt.n]){
     69                     q.push(nxt); flag[nxt.s][nxt.m][nxt.n]=1;
     70                 } 
     71             }
     72             if(now.n>0){
     73                 if(now.n>m-now.m){
     74                     nxt.m=m;
     75                     nxt.n=now.n-(m-now.m);
     76                     nxt.s=now.s;
     77                 } else {
     78                     nxt.m=now.m+now.n;
     79                     nxt.n=0;
     80                     nxt.s=now.s;        
     81                 }
     82                 if(!flag[nxt.s][nxt.m][nxt.n]){
     83                     q.push(nxt); flag[nxt.s][nxt.m][nxt.n]=1;
     84                 }
     85             }            
     86         }
     87         if(now.n<n){
     88             if(now.s>0){
     89                 if(now.s>n-now.n){
     90                     nxt.n=n;
     91                     nxt.s=now.s-(n-now.n);
     92                     nxt.m=now.m;
     93                 } else {
     94                     nxt.n=now.n+now.s;
     95                     nxt.s=0;
     96                     nxt.m=now.m;        
     97                 }
     98                 if(!flag[nxt.s][nxt.m][nxt.n]){
     99                     q.push(nxt); flag[nxt.s][nxt.m][nxt.n]=1;
    100                 } 
    101             }
    102             if(now.m>0){
    103                 if(now.m>n-now.n){
    104                     nxt.n=n;
    105                     nxt.m=now.m-(n-now.n);
    106                     nxt.s=now.s;
    107                 } else {
    108                     nxt.n=now.n+now.m;
    109                     nxt.m=0;
    110                     nxt.s=now.s;        
    111                 }
    112                 if(!flag[nxt.s][nxt.m][nxt.n]){
    113                     q.push(nxt); flag[nxt.s][nxt.m][nxt.n]=1;
    114                 }
    115             }            
    116         }
    117     }
    118     return -1;
    119 }
    120 int main()
    121 {
    122     while(~scanf("%d%d%d",&s,&n,&m),(n+m+s))
    123     {
    124         if(m<n) swap(m,n);
    125         int ans=bfs();
    126         if(ans==-1) puts("NO");
    127         else printf("%d
    ",ans);
    128     }
    129 }
    我自倾杯,君且随意
  • 相关阅读:
    LeetCode 9. Palindrome Number(回文数)
    POJ 1080 Human Gene Functions
    springcloud之gateway
    canal快速入门
    Java8新特性
    vue整合echarts
    vue整合微信支付
    Thymeleaf快速入门
    vue整合阿里云播发器
    vue之vuex
  • 原文地址:https://www.cnblogs.com/nicetomeetu/p/5436317.html
Copyright © 2020-2023  润新知