ZOJ1005 Jug问题 运行结果有误?

prolog waterjugs.pl 两个瓶子,一个四加仑一个3加仑,输入您想要的最终状态,可以智能的搜索出倒法,需要配合A*搜索的prolog程序 立即下载

ZOJ1005 Jug问题

题目

In the movie “Die Hard 3”, Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3-gallon jug and a 5-gallon jug and were asked to fill the 5-gallon jug with exactly 4 gallons. This problem generalizes that puzzle.

You have two jugs, A and B, and an infinite supply of water. There are three types of actions that you can use: (1) you can fill a jug, (2) you can empty a jug, and (3) you can pour from one jug to the other. Pouring from one jug to the other stops when the first jug is empty or the second jug is full, whichever comes first. For example, if A has 5 gallons and B has 6 gallons and a capacity of 8, then pouring from A to B leaves B full and 3 gallons in A.

A problem is given by a triple (Ca,Cb,N), where Ca and Cb are the capacities of the jugs A and B, respectively, and N is the goal. A solution is a sequence of steps that leaves exactly N gallons in jug B. The possible steps are

fill A
fill B
empty A
empty B
pour A B
pour B A
success

where “pour A B” means “pour the contents of jug A into jug B”, and “success” means that the goal has been accomplished.

You may assume that the input you are given does have a solution.

Input

Input to your program consists of a series of input lines each defining one puzzle. Input for each puzzle is a single line of three positive integers: Ca, Cb, and N. Ca and Cb are the capacities of jugs A and B, and N is the goal. You can assume 0 < Ca <= Cb and N <= Cb <=1000 and that A and B are relatively prime to one another.

Output

Output from your program will consist of a series of instructions from the list of the potential output lines which will result in either of the jugs containing exactly N gallons of water. The last line of output for each puzzle should be the line “success”. Output lines start in column 1 and there should be no empty lines nor any trailing spaces.

Sample Input

3 5 4
5 7 3

Sample Output

fill B
pour B A
empty A
pour B A
fill B
pour B A
success
fill A
pour A B
fill A
pour A B
empty B
pour A B
success

主要思想

考虑到 A.ca<B.ca N<=B.ca。
因此首先考虑B.ca=N的情况;不满足则不断满A,倒入B。
但是zoj运行结果显示“Wrong Answer”也是有点悲伤,哪里错了呐?

代码块

#include<stdio.h>

struct jug{
    int ca;
    int water;
};

struct jug A,B;
int N;

int judge(){
    if(A.water==N) return 1;
    if(B.water==N) return 1;
    if(B.water>=B.ca){
        B.water=0;
        printf("empty B\n");
    }
    return 0;
}

void main(){
    int flag=1;
        A.water=0;
        B.water=0;
        scanf("%d%d%d",&A.ca,&B.ca,&N);
        if (B.ca==N){
            printf("fill B\n");
            flag=0;
        }
        while(flag){
            if(A.water==0){
                printf("fill A\n");
                A.water=A.ca;
                if(judge()) break;
            }
            printf("pour A B\n");
            B.water+=A.water;
            A.water=0;
            if(B.water>=B.ca){
                A.water=B.water-B.ca;
            }
            if(judge())break;
        }
        printf("success\n");

}
SolidWorks2020详细步骤安装教程 1、在安装软件前请断开网络连接,需要安装.NETFramework3.5和4.0,最后还要确保把之前安装的旧版本完全卸载,以管理员身份运行SolidWorks_Flexnet_Server\server_remove.bat,然后等到服务“SolidWorksFlexnet服务器”将被删除。12、先不要运行软件,找到_SolidSQUAD_文件夹,将文件夹“ProgramFiles\SOLIDWORKSCorp”中的所有破解文件复制替到“ProgramFiles\SOLIDWORKSCorp”目录下; 阅读详情

相关推荐

DDS(Direct Digital Synthesizer)数字频率合成器

随着数字技术在仪器仪表和通信系统中的广泛使用,可从参考频率源产生多个频率的数 字控制方法诞生了,即直接数字频率合成(DDS)。 直接数字合成(DDS)是一种产生模拟波形(通常是正弦波)的方法,它通过产生数字形式的时变信号,然后进行数模转换。由于DDS设备内的操作主要是数字的,它可以提供输出频率之间的快速切换,良好的频率分辨率,并在广泛的频率频谱上操作。随着设计和工艺技术的进步,今天的DDS设备非常紧凑,功耗很小。 上面是比较书面的语言,其实简单地说就是一个信号发生器模块,主要是产生频率可调的三角波正弦波。

denghuajing的博客 4866

dfs -bfs-记录路径

dfs主要思路: 从一个节点出发,遍历与该节点相连的所有点,有时不给重复访问,标记设置一次即可,动态规划中,有时需要dfs遍历标点的记号;有时又求不同路径总数,需要先标记再撤去标记,恢复现场,不影响其他搜索。当然,和所有递归一样,这是要有边界的,不能无限递归下去,边界一般根据题目获得。dfs是最朴素的暴力解法,找极大连通子图(如果标记的话)个数,等等,暴力选dfs bfs主要思路: 从一个节点出发,一层一层扫,找到的路径就是最短路径,那怎么记录最短路径走的步数, ...

weixin_54010759的博客 4125

微信小程序 实现拼图功能

微信小程序 实现碎片拼图

weixin_45689786的博客 2251

BFS记路径

BFS记路径

yuanxinya_的博客 918

2024最新版Anaconda Navigator安装使用指南

本文简要介绍如何下载安装Anaconda Navigator最新版,以及如何使用Python内核搭载来完成应用程序开发。

jackson_lingua的博客 1万+

告别jom Error 2:深入理解QtCreator、CMake与MSVC工具链的协作机制

本文深入解析QtCreator构建过程中常见的jom Error 2问题,揭示CMake与MSVC工具链的协作机制。从CMake生成器的工作原理到MSVC工具链的组件协作,帮助开发者系统化诊断构建错误,提升开发效率。

weixin_30654419的博客 433

BFS记录路径(学习记录)

前段时间学完BFS很开心地发现oj上有些题可以做了!但是记录路径又把我难住惹,,,赶紧去学习一下,本篇算是复习BFS和它的记录路径方法吧。 还是以题引例:1086 迷宫问题 1220Look for homework 1654Treasure House ...

m0_62289613的博客 2644

三款云电脑实测:ToDesk、青椒云、顺网云谁更适合AIGC新手?附详细配置对比

本文针对AIGC新手,深度实测了ToDesk云电脑、青椒云和顺网云三款云电脑服务。通过对比上手门槛、内置生态、操作引导及成本,发现ToDesk云电脑凭借一键启动、丰富预置模型和直观引导,为新手提供了最接近“开箱即用”的零门槛AIGC创作体验,是快速入门AI绘画的高效选择。

assembly8low的博客 1095

BFS + 路径记忆

19蓝桥杯省赛 迷宫 题目描述:给定一个30行50列的迷宫,从左上角移动到右下角,并输出移动路径。 移动有四种方式“U,D,L,R”,分别表示往上下左右走。‘0’表示可以走,‘1’代表墙不能走。 解题思路:其实参加比赛的时候根本就不会,只学了个语言基础,连bfs都不会,赛后听学长说是bfs,后来学了bfs,还是不知道怎么解决输出路径的问题。过了好久才知道,只需要维护一个队首head和队尾tail指...

qq_43461168的博客 2138

别再硬拔了!用这3个终端命令轻松搞定Mac上移动硬盘‘被程序占用’的问题

本文介绍了3个终端命令解决Mac上移动硬盘‘被程序占用’的问题,包括使用`diskutil list`定位硬盘、`lsof`查找占用进程以及`diskutil unmount`安全卸载。这些方法能有效避免数据风险,提升操作效率,特别适合开发者和技术爱好者。

weixin_33507732的博客 297

Python爬虫实战:构建药监局药品批准信息本地查询系统

在医药信息化时代,药品批准信息的快速查询对于医药从业者、研究人员和普通消费者都具有重要意义。国家药品监督管理局(NMPA)提供了官方的药品查询系统,但有时我们需要建立本地数据库以便批量分析或离线查询。本文将详细介绍如何使用Python爬虫技术,结合最新的异步爬虫框架和技术栈,构建一个完整的药监局药品信息本地查询系统。本系统将实现以下功能:自动化爬取药监局官网的药品批准信息数据清洗和结构化存储建立本地查询接口数据定期更新机制pythonimport os"""药品类型枚举"""

2201_76125261的博客 729

BFS记录路径

19蓝桥杯走迷宫 #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<string> #include<vector>...

lanshan1111的博客 994

Win7/10/11文件拖拽导致资源管理器闪退问题

最近老是遇到把文件从资源管理器拖动到微信或者浏览器时,资源管理器直接闪退的问题,一开始在公司遇到了还以为是电脑配置太低,直到被封闭在家办公,发现自己的高级电脑也有这个问题,那就来了解了解问题是出在什么地方把。

Nicer_feng's blog 1万+

zju 1005 zoj 1005

Jugs Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were gi

sonfree的专栏 612

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

zoj 1005

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1005 In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3

npu2015303181的博客 321

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

ZOJ Problem Set - 1005 Jugs

ZOJ Problem Set - 1005 JugsTime Limit: 2 Seconds Memory Limit: 65536 KB Special Judge In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the followin...

aboc43983的博客 135

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

ZOJ1005Jugs

Jugs Time Limit: 2000 msMemory Limit: 65536 KB In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3-gallon jug and a 5-gallon jug and were asked to fill the 5-gallon jug with exactly

Lycodeboy的博客 248

c语言大小写字母互换1005,1005 Jugs,1005jugs

1005 Jugs,1005jugs辗转相减,新手入门题。两个容量的灌水题,无所谓最优解。1 #include23 intmain(){4 intA,B,T,sA,sB;5 while(scanf("%d %d %d",&A,&B,&T)>=0){6 sA=sB=0;7 while(1){8 ...

weixin_39710462的博客 254

ZOJ Problem Set - 1005 Jugs

Jugs Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were g

acm_1361677193的专栏 427

ZOJ Problem 1005 jugs

题目 In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3-gallon jug and a 5-gallon jug and were asked to fill the 5-gallon jug with exactly 4 gallons. This problem generalizes that puz

JetEcho的博客 283

ZOJ 1005 Jugs

Jugs Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were gi

闲暇时光写代码 641

ZOJ1005解题报告

Jugs Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were gi

卧龙居 2328

zoj 1005 Jugs

最近一直在看算法的知識,也順便刷刷題,不過還是一直被虐。這道題相信每個人都認識的了,就是兩個用兩個水壺弄出指定量的水。一開始完全不知道要怎麼下手,後來嘗試了一下,用了很複雜的邏輯,結果第一個案例就死循環然後完全不知道怎麼調試。後來搜了一下網上的博客,看到一個人說他後來發現題目其實很簡單,因爲並沒有要求是最優解,自己並沒有看代碼,想想還是要自己動腦寫出來的好。然後第一次就accept了。 思維很簡

u014209688的专栏 531

ZOJ Problem Set - 1005 Jugs

ZOJ Problem Set - 1005 Jugs Time Limit: 2 Seconds                                     Memory Limit: 65536 KB                                                     Special Judge                  

Jackwuyongxing的专栏 789

浙江大学zoj 1005 jugs解法

In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3-gallon jug and a 5-gallon jug and were asked to fill the 5-gallon jug with

cch5487614的专栏 970

ZOJ 1005 Jugs (演绎推理)

Jugs Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were gi

ZJUCSqxy的博客 381
上一篇: 在Mac系统上配置opencv 使用Xcode初步应用
下一篇: 机器学习-周志华,习题1.1,答案探究
ThousandsOfWind
博客等级 码龄11年 21粉丝 13原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值