ZCMU - 1978: 调酒壶里的酸奶

简介: ZCMU - 1978: 调酒壶里的酸奶

题目链接:点击打开链接


题目大意:略。

解题思路:记忆化搜索,对每个状态都有六种情况:

  1. 把A倒满
  2. 把B倒满
  3. 把A倒光
  4. 把B倒光
  5. 把A倒进B
  6. 把B倒进A

六个情况直接搜索就可以了...当然dfs是会超时的,需要用记忆化搜索。


AC 代码


#include<bits/stdc++.h>
#include<cmath>
#define mem(a,b) memset(a,b,sizeof a);
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
queue<pii> q;
int a,b,c;
int vis[110][110], dis[110][110];
void init()
{
    while(!q.empty()) q.pop();
    mem(vis,0); mem(dis,0);
}
int bfs()
{
    dis[0][0]=0; vis[0][0]=1;
    q.push(make_pair(0,0));
    int x,y,A,B;
    while(!q.empty())
    {
        x=q.front().first, y=q.front().second; q.pop();
        for(int i=1;i<=6;i++)
        {
            if(i==1) A=a, B=y;
            else if(i==2) A=x, B=b;
            else if(i==3) A=0, B=y;
            else if(i==4) A=x, B=0;
            else if(i==5) A=x+y>=b?x+y-b:0, B=x+y>=b?b:x+y;
            else if(i==6) B=x+y>=a?x+y-a:0, A=x+y>=a?a:x+y;
            if(!vis[A][B])
            {
                vis[A][B]=1;
                dis[A][B]=1+dis[x][y];
                if(A==c || B==c) return dis[A][B];
                q.push(make_pair(A,B));
            }
        }
    }
    return 0;
}
int main()
{
    while(~scanf("%d%d%d",&a,&b,&c))
    {
        init();
        int rs=bfs();
        if(rs) printf("%d\n",rs);
        else puts("impossible");
    }
    return 0;
}
目录
相关文章
|
10月前
|
Dubbo 应用服务中间件
|
存储 前端开发 安全
【面试题】: bs架构与cs架构的区别以及各自优缺点
bs架构与cs架构的区别以及各自优缺点
9853 0
|
测试技术 容器
开门小例子学习十种用例图
早晨上班米老师需要何老师打开水麒麟的门
|
索引 Python
离线安装Python依赖:以six和websocket-client为例
离线安装Python依赖:以six和websocket-client为例
1626 0
|
移动开发 前端开发 JavaScript
我修复的印象最深的一个bug
作为一个程序猿,几乎天天都要和bug斗智斗勇,就像那句话说的,程序猿每天不是在改bug就是在去改bug的路上。每天都在和bug较劲,但是我修复的印象最深的一个bug是关于浏览器对js Date数据格式的兼容性造成的
1507 155
|
前端开发 C++
|
Web App开发 JavaScript 前端开发
ECMAScript 双月报告:阿里巴巴提案 Error Cause 进入 Stage3
从TC39 3月会议带来的ECMAScript最新进展。
ECMAScript 双月报告:阿里巴巴提案 Error Cause 进入 Stage3
|
Kotlin
My favorite examples of functional programming in Kotlin
My favorite examples of functional programming in Kotlin One of the great things about Kotlin is that it supports functional programming.
1133 0

热门文章

最新文章