首页 > 其他分享 >hdu-1495

hdu-1495

时间:2023-03-03 13:05:00浏览次数:41  
标签:qq hdu ml vis step qqq 1495 include

bfs 六种状态


 

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<vector>
#include<string.h>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<sstream>
#include<time.h>
#include<utility>
#include<malloc.h>
#include<stdexcept>
#include<iomanip>
#include<iterator>

using namespace std;

int s,n,m;

struct ss
{
    int ml[4];
    int step;
};

int vis[110][110][110];

void bfs ()
{
    memset(vis,0,sizeof(vis));
    queue<ss> q;
    ss qq ,qqq;

    qq.ml[0] = s;
    qq.ml[1] = 0;//初始是0  不是n
    qq.ml[2] = 0;
    vis[qq.ml[0]][qq.ml[1]][qq.ml[2]] = 1;
    qq.step = 0;

    q.push(qq);

    while (!q.empty())
    {
        qq = q.front();
        q.pop();

        if ((qq.ml[0] == s/2 && qq.ml[1] == s/2) || (qq.ml[0] == s/2 && qq.ml[2] == s/2) || (qq.ml[1] == s/2 && qq.ml[2] == s/2) )
        {
            printf("%d\n",qq.step);
            return ;
        }

        //a b ok
        {
            //a b倒不完
            if (qq.ml[0] > n - qq.ml[1] )
            {
                qqq = qq;
                qqq.ml[0] = qq.ml[0] - (n- qq.ml[1]);
                qqq.ml[1] = n;
                qqq.step = qq.step + 1;
                if(!vis[qqq.ml[0]][qqq.ml[1]][qqq.ml[2]])
                {
                    vis[qqq.ml[0]][qqq.ml[1]][qqq.ml[2]] = 1;
                    q.push(qqq);
                }
            }
            else
            {
                //a b倒完
                qqq = qq;
                qqq.ml[0] = 0;
                qqq.ml[1] = qq.ml[1] + qq.ml[0] ;
                qqq.step = qq.step + 1;
                if(!vis[qqq.ml[0]][qqq.ml[1]][qqq.ml[2]])
                {
                    vis[qqq.ml[0]][qqq.ml[1]][qqq.ml[2]] = 1;
                    q.push(qqq);
                }
            }
        }
        //a c ok
        {
            //a c倒不完
            if (qq.ml[0] > m - qq.ml[2] )
            {
                qqq = qq;
                qqq.ml[0] = qq.ml[0] - (m - qq.ml[2]);
                qqq.ml[2] = m;
                qqq.step = qq.step + 1;
                if(!vis[qqq.ml[0]][qqq.ml[1]][qqq.ml[2]])
                {
                    vis[qqq.ml[0]][qqq.ml[1]][qqq.ml[2]] = 1;
                    q.push(qqq);
                }
            }
            else
            {
                //a c倒完
                qqq = qq;
                qqq.ml[0] = 0;
                qqq.ml[2] = qq.ml[2] + qq.ml[0] ;
                qqq.step = qq.step + 1;
                if(!vis[qqq.ml[0]][qqq.ml[1]][qqq.ml[2]])
                {
                    vis[qqq.ml[0]][qqq.ml[1]][qqq.ml[2]] = 1;
                    q.push(qqq);
                }
            }
        }
        //b a  ok
        {

            //b a 倒不完
            if (qq.ml[1] > s - qq.ml[0] )
            {
                qqq = qq;
                qqq.ml[0] = s;
                qqq.ml[1] = qq.ml[1] - (s - qq.ml[0]);
                qqq.step = qq.step + 1;
                if(!vis[qqq.ml[0]][qqq.ml[1]][qqq.ml[2]])
                {
                    vis[qqq.ml[0]][qqq.ml[1]][qqq.ml[2]] = 1;
                    q.push(qqq);
                }
            }
            else
            {
                //b a倒完
                qqq = qq;
                qqq.ml[1] = 0;
                qqq.ml[0] = qq.ml[1] + qq.ml[0] ;
                qqq.step = qq.step + 1;
                if(!vis[qqq.ml[0]][qqq.ml[1]][qqq.ml[2]])
                {
                    vis[qqq.ml[0]][qqq.ml[1]][qqq.ml[2]] = 1;
                    q.push(qqq);
                }
            }

        }
        // b c ok
        {
            //b c 倒不完
            if (qq.ml[1] > m - qq.ml[2] )
            {
                qqq = qq;
                qqq.ml[1] = qq.ml[1] - (m - qq.ml[2] );
                qqq.ml[2] = m;
                qqq.step = qq.step + 1;
                if(!vis[qqq.ml[0]][qqq.ml[1]][qqq.ml[2]])
                {
                    vis[qqq.ml[0]][qqq.ml[1]][qqq.ml[2]] = 1;
                    q.push(qqq);
                }
            }
            else
            {
                //b  c 倒完
                qqq = qq;
                qqq.ml[2]= qq.ml[2] + qq.ml[1];
                qqq.ml[1] = 0;
                qqq.step = qq.step + 1;
                if(!vis[qqq.ml[0]][qqq.ml[1]][qqq.ml[2]])
                {
                    vis[qqq.ml[0]][qqq.ml[1]][qqq.ml[2]] = 1;
                    q.push(qqq);
                }
            }
        }
        //c a ok
        {
            //c a 倒不完
            if (qq.ml[2] > s - qq.ml[0] )
            {
                qqq = qq;
                qqq.ml[0] = s;
                qqq.ml[2] = qq.ml[2] - (s - qq.ml[0]);
                qqq.step = qq.step + 1;
                if(!vis[qqq.ml[0]][qqq.ml[1]][qqq.ml[2]])
                {
                    vis[qqq.ml[0]][qqq.ml[1]][qqq.ml[2]] = 1;
                    q.push(qqq);
                }
            }
            else
            {
                //c a 倒完
                qqq = qq;
                qqq.ml[2] = 0;
                qqq.ml[0] = qq.ml[0] + qq.ml[2] ;
                qqq.step = qq.step + 1;
                if(!vis[qqq.ml[0]][qqq.ml[1]][qqq.ml[2]])
                {
                    vis[qqq.ml[0]][qqq.ml[1]][qqq.ml[2]] = 1;
                    q.push(qqq);
                }
            }

        }
        //c b ok
        {
            //倒不完
            if (qq.ml[2] > n - qq.ml[1] )
            {
                qqq = qq;
                qqq.ml[1] = n;
                qqq.ml[2] = qq.ml[2] - (n - qq.ml[1]);
                qqq.step = qq.step + 1;
                if(!vis[qqq.ml[0]][qqq.ml[1]][qqq.ml[2]])
                {
                    vis[qqq.ml[0]][qqq.ml[1]][qqq.ml[2]] = 1;
                    q.push(qqq);
                }
            }
            else
            {
                //倒完
                qqq = qq;
                qqq.ml[2] = 0;
                qqq.ml[1] = qq.ml[1] + qq.ml[2] ;
                qqq.step = qq.step + 1;
                if(!vis[qqq.ml[0]][qqq.ml[1]][qqq.ml[2]])
                {
                    vis[qqq.ml[0]][qqq.ml[1]][qqq.ml[2]] = 1;
                    q.push(qqq);
                }
            }
        }

    }
    printf("NO\n");
    return ;
}

int main()
{
    while (scanf("%d %d %d",&s,&n,&m) != EOF )
    {
        if (n == 0 && s == 0 && m ==0)
            return 0;

        if (s % 2 != 0 )
        {
            printf("NO\n");
        }
        else
        {
            bfs ();
        }
    }
    return 0;
}





标签:qq,hdu,ml,vis,step,qqq,1495,include
From: https://blog.51cto.com/u_15990681/6098469

相关文章

  • hdu-2614
    取得第一个是第一个任务,时间0,接着进行下一个任务。#include<stdio.h>#include<iostream>#include<math.h>#include<stdlib.h>#include<ctype.h>#include<alg......
  • hdu-1195
    http://acm.hdu.edu.cn/showproblem.php?pid=1195bfs加1减1交换,三个方式#include<stdio.h>#include<iostream>#include<math.h>#include<stdlib.h>#includ......
  • hdu-1016
    约瑟夫换问题http://acm.hdu.edu.cn/showproblem.php?pid=1016#include<stdio.h>#include<stdlib.h>#include<algorithm>#include<string.h>intn,cas=1,vi......
  • hdu-1238
    http://acm.hdu.edu.cn/showproblem.php?pid=1238SubstringsTimeLimit:2000/1000MS(Java/Others)    MemoryLimit:65536/32768K(Java/Others)TotalS......
  • hdu-1515
    dfs 题意:给你两个字符串,问:第一个字符串按入栈出栈规则,能否达到第二个字符串,输出所有的方法,i表示入栈,o表示出栈。用dfs模拟第一个字符串入栈出栈过程:1.当前字符......
  • hdu-1548
    搜索做着做着成最短路径了。。dij本层可以直接到达的层数距离为1否则为无穷大#include<stdio.h>#include<iostream>#include<math.h>#include<stdlib.h>#includ......
  • hdu-1253
    http://acm.hdu.edu.cn/showproblem.php?pid=1253这道水题#include<stdio.h>#include<iostream>#include<math.h>#include<stdlib.h>#include<......
  • hdu-2821
    http://acm.hdu.edu.cn/showproblem.php?pid=2821不要被题目吓到,认真读题还是好理解的#include<stdio.h>#include<iostream>#include<string.h>#include<math.h......
  • HDU-5112-A Curious Matt (2014ACM/ICPC北京赛区现场赛A题!)
    http://acm.hdu.edu.cn/showproblem.php?pid=5112排序之后计算就好开始用cin超时了#include<stdio.h>#include<iostream>#include<math.h>#include<stdlib.h>#......
  • hdu-5122
    http://acm.hdu.edu.cn/showproblem.php?pid=5122简单题#include<stdio.h>#include<iostream>#include<math.h>#include<stdlib.h>#include<ctype.h>#include......