zoukankan      html  css  js  c++  java
  • 三个水杯

    三个水杯

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:4
     
    描述
    给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子。三个水杯之间相互倒水,并且水杯没有标识,只能根据给出的水杯体积来计算。现在要求你写出一个程序,使其输出使初始状态到达目标状态的最少次数。
     
    输入
    第一行一个整数N(0<N<50)表示N组测试数据
    接下来每组测试数据有两行,第一行给出三个整数V1 V2 V3 (V1>V2>V3 V1<100 V3>0)表示三个水杯的体积。
    第二行给出三个整数E1 E2 E3 (体积小于等于相应水杯体积)表示我们需要的最终状态
    输出
    每行输出相应测试数据最少的倒水次数。如果达不到目标状态输出-1
    样例输入
    2
    6 3 1
    4 1 1
    9 3 2
    7 1 1
    样例输出
    3
    -1
    来源
    经典题目

    广度优先搜索,貌似可以用A*,可惜不怎么会,以后试试。

    # include <stdio.h>  
    # include <stdlib.h>  
    # include <string.h>  
    # include <string>  
      
    void check (int queue[], int x, int &tail) {  
        for (int i = 0; i <= tail; ++ i) {  
            if (queue[i] == x) {  
                return;  
            }  
        }  
        ++ tail;  
        //printf ("%d -> ", x);  
        queue[tail] = x;  
        return;  
    }  
      
    int getans(int to[], int i) {  
        if (i == to[1] * 10000 + to[2] * 100 + to[3]) return 1;  
        return 0;  
    }  
      
    int daoshui(int queue[], int &a, int &b, int to, int from[]) {  
        if (a) {  
            int t = from[to] - b;  
            if (t > a) {  
                b += a;  
                a = 0;  
                return 1;  
            }  
            else if (t) {  
                a -= t;  
                b += t;  
                return 1;  
            }  
        }  
        return 0;  
    }  
      
    int main () {  
        int n;  
        scanf ("%d", &n);  
        while (n --) {  
            int from[4];  
            int to[4];  
            scanf ("%d %d %d %d %d %d", &from[1], &from[2], &from[3], &to[1], &to[2], &to[3]);  
            int head = 0;  
            int tail = 0;  
            int step = 0;   
            int queue[100000];  
            queue[0] = from[1] * 10000;  
            int flag = 0;  
            if (queue[0] == to[1] * 10000 + to[2] * 100 + to[3]) {printf("0
    ");continue;}  
            while (head <= tail) {  
                ++ step;  
                //printf("******************************%d************************************
    ", step);  
                int size = tail - head;  
                for (int i = head; i <= head + size; ++ i) {  
                    int a, b, c;  
                    //printf ("|%d|", queue[i]);  
                    a = queue[i] / 10000; b = queue[i] % 10000 / 100; c = queue[i] % 100;  
                    if (daoshui(queue, a, b, 2, from)) {  
                        //printf ("(a -> b)");  
                        check(queue, a * 10000 + b * 100 + c, tail);  
                    }  
                    a = queue[i] / 10000; b = queue[i] % 10000 / 100; c = queue[i] % 100;  
                    if (daoshui(queue, a, c, 3, from)) {  
                        //printf ("(a -> c)");  
                        check(queue, a * 10000 + b * 100 + c, tail);  
                    }  
                    a = queue[i] / 10000; b = queue[i] % 10000 / 100; c = queue[i] % 100;  
                    if (daoshui(queue, b, a, 1, from)) {  
                        //printf ("(b -> a)");  
                        check(queue, a * 10000 + b * 100 + c, tail);  
                    }  
                    a = queue[i] / 10000; b = queue[i] % 10000 / 100; c = queue[i] % 100;  
                    if (daoshui(queue, b, c, 3, from)) {  
                        //printf ("(b -> c)");  
                        check(queue, a * 10000 + b * 100 + c, tail);  
                    }  
                    a = queue[i] / 10000; b = queue[i] % 10000 / 100; c = queue[i] % 100;  
                    if (daoshui(queue, c, a, 1, from)) {  
                        //printf ("(c -> a)");  
                        check(queue, a * 10000 + b * 100 + c, tail);  
                    }  
                    a = queue[i] / 10000; b = queue[i] % 10000 / 100; c = queue[i] % 100;  
                    if (daoshui(queue, c, b, 2, from)) {  
                        //printf ("(c -> b)");  
                        check(queue, a * 10000 + b * 100 + c, tail);  
                    }  
                }  
                //printf ("
    ****************************************************************");  
                head += size + 1;  
                for (int i = head; i <= tail; ++ i) {  
                    if (getans(to, queue[i])) {  
                        printf ("%d
    ", step);  
                        flag = 1;  
                        break;  
                    }  
                }  
                if (flag) break;  
            }  
            //printf("
    ");  
            if (!flag) printf ("-1
    ");  
        }  
        return 0;  
    }  
     
  • 相关阅读:
    【原】从/dev/null重新打开标准输出
    Go 接口转换的一个例子
    关于软件编译安装的出错处理
    【原】GO 语言常见错误
    HP平台由于变量声明冲突导致程序退出时的core
    动态链接库加载出错:cannot restore segment prot after reloc: Permission denied
    Windows VC++常见问题汇总
    .net:System.Web.Mail vs System.Net.Mail应该用哪个
    网络管理的功能
    Hello World! — 用 Groovy 编写的 Java 程序
  • 原文地址:https://www.cnblogs.com/panlangen/p/7955749.html
Copyright © 2011-2022 走看看