HLJOJ1021(倒水问题)

描述:设大,中,小三个杯子的容量分别为a,b,c。开始时,A中装满了水,B,C都为空。你的任务是,判断经过一系列操作后,能否使其中一个杯子里出现x升水。(一次操作指把x杯子里的水倒入y杯子中,因为杯子没有刻度 ,所以倒水时只允许将x杯子倒空,或将y杯子倒满。

输入:四个整数a,b,c,x.(1<=a<=b<=c<100,0<x<100)

输出:能的话,输出最短的操作步数,否则输出Impossible.

样例:

6 3 1 4

10 8 7 5

输出:

3

impssbile

第一个样例所经过的步骤:(6,0,0)->(3,3,0)->(3,2,1)->(4,2,0)

只有六种状态,BFS一下

a->b,a->c

b->a,b->c

c->a,c->b

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>

using namespace std;
const double eps = 1e-6;
const int MAXN = 100010;
const double INF = 1e20;
int visit[101][101];
int aa[3];
int x;
struct Node
{
    int cap[3],step;
    Node(int x, int y,int z,int p)
    {
        cap[0] = x,cap[1] = y, cap[2] = z , step = p;
    }
    Node() {}
};

int BFS()
{
    memset(visit,0,sizeof(visit));
    queue<Node> Q;
    Q.push(Node(aa[0],0,0,0));
    visit[aa[0]][0] = 1;
    while(!Q.empty())
    {
        Node temp = Q.front(),temp2;
        Q.pop();
       if(temp.cap[0] == x || temp.cap[1] == x || temp.cap[2] == x) return temp.step;

        for(int i = 0; i < 3; i++)
        {
            for(int j = 0; j < 3; j++)
            {
                if(i == j) continue;
                int cha = aa[j] - temp.cap[j];
                temp2 = temp;

                if(temp2.cap[i] && cha )
                {

                    if(temp2.cap[i] > cha)
                    {
                        temp2.cap[j] = aa[j];
                        temp2.cap[i] -= cha;
                    }
                    else
                    {
                        temp2.cap[j] += temp2.cap[i];
                        temp2.cap[i] = 0;

                    }

                    if(!visit[temp2.cap[0]][temp2.cap[1]])
                    {
                        visit[temp2.cap[0]][temp2.cap[1]] = 1;
                        Q.push(Node(temp2.cap[0],temp2.cap[1],temp2.cap[2],temp2.step+1));
                    }
                }

            }
        }
    }

    return -1;
}

int main()
{
    #ifdef xxz
    freopen("in.txt","r",stdin);
    #endif // xxz
    while(~scanf("%d%d%d%d", &aa[0], &aa[1], &aa[2],&x))
    {
        int ans = BFS();
        if(ans == -1) printf("Impossible\n");
        else printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-10 17:21:39

HLJOJ1021(倒水问题)的相关文章

倒水问题

[题目描述] 有两个无刻度标志的水壶,分别可装x升和y升(x,y为整数且均不大于100)的水.设另有一水缸,可用来向水壶灌水或接从水壶中倒出的水,两水壶间,水也可以相互倾倒.已知x升壶为空壶,y升壶为空壶.问如何通过倒水或灌水操作,用最少步数能在x或y升的壶中量出z(z ≤ 100)升的水来. [输入描述] 一行,三个数据,分别表示x,y和z. [输出描述] 一行,输出最小步数,如果无法达到目标,则输出“impossible”. [样例输入] 3 22 1 [样例输出] 14

Pots BFS(著名倒水问题升级版)

Pots You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap; DROP(i)      empty the pot i to the drain; POUR(i,j)    pour from pot i 

2017广东工业大学程序设计竞赛 E倒水(Water)

原题链接:http://www.gdutcode.sinaapp.com/problem.php?cid=1057&pid=4 Problem E: 倒水(Water) Description 一天,CC买了N个容量可以认为是无限大的瓶子,开始时每个瓶子里有1升水.接着~~CC发现瓶子实在太多了,于是他决定保留不超过K个瓶子.每次他选择两个当前含水量相同的瓶子,把一个瓶子的水全部倒进另一个里,然后把空瓶丢弃.(不能丢弃有水的瓶子) 显然在某些情况下CC无法达到目标,比如N=3,K=1.此时CC会

HDU 1495 非常可乐(BFS倒水问题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 题目大意:只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 (正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,N>0,M>0) .如果能将可乐平分则输出倒可乐的最少的次数,如果不能输出"NO". 解题思路:题意很坑看了半天,就是要有两个杯子里的可乐都为S/2,S为奇数肯

【CodeVS1226】倒水问题

题目描述 Description 有两个无刻度标志的水壶,分别可装 x 升和 y 升 ( x,y 为整数且均不大于 100 )的水.设另有一水 缸,可用来向水壶灌水或接从水壶中倒出的水, 两水壶间,水也可以相互倾倒.已知 x 升壶为空 壶, y 升壶为空壶.问如何通过倒水或灌水操作, 用最少步数能在x或y升的壶中量出 z ( z ≤ 100 )升的水 来. 输入描述 Input Description 一行,三个数据,分别表示 x,y 和 z; 输出描述 Output Description 一

BFS(倒水问题) HDU 1495 非常可乐

题目传送门 1 /* 2 BFS:倒水问题,当C是奇数时无解.一共有六种情况,只要条件符合就入队,我在当该状态vised时写了continue 3 结果找了半天才发现bug,泪流满面....(网上找份好看的题解都难啊) 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-4 10:54:16 8 File Name :HDOJ_1495.cpp

1226 倒水问题

1226 倒水问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 有两个无刻度标志的水壶,分别可装 x 升和 y 升 ( x,y 为整数且均不大于 100 )的水.设另有一水 缸,可用来向水壶灌水或接从水壶中倒出的水, 两水壶间,水也可以相互倾倒.已知 x 升壶为空 壶, y 升壶为空壶.问如何通过倒水或灌水操作, 用最少步数能在x或y升的壶中量出 z ( z ≤ 100 )升的水 来. 输入描述 Input Descript

UVa 10603 倒水问题

https://vjudge.net/problem/UVA-10603 题意:三个杯子,倒水问题.找出最少倒水量. 思路:路径寻找问题.不难,暴力枚举. 1 #include<iostream> 2 #include<queue> 3 #include<string> 4 #include<cstring> 5 using namespace std; 6 7 int a, b, c, d; 8 int vis[205][205]; //访问变量,因为只有

[Wikioi 1226]倒水问题

题目描述 Description 有两个无刻度标志的水壶,分别可装 x 升和 y 升 ( x,y 为整数且均不大于 100 )的水.设另有一水 缸,可用来向水壶灌水或接从水壶中倒出的水, 两水壶间,水也可以相互倾倒.已知 x 升壶为空 壶, y 升壶为空壶.问如何通过倒水或灌水操作, 用最少步数能在x或y升的壶中量出 z ( z ≤ 100 )升的水 来. 输入描述 Input Description 一行,三个数据,分别表示 x,y 和 z; 输出描述 Output Description 一