CSU - 2061 Z‘s Coffee
Description

Z is crazy about coffee. One day he bought three cups of coffee. The first cup has a capacity of A ml, the second has B ml and the third has C ml. At the beginning, only the first cup is full of coffee, that is, Z only has A ml coffee and the other cups is empty. Because the cup has no scale, one operation of pouring coffee only stop until either one cup is full or empty. Z can only choose two cups to do the pouring operation. For some reasons, Z wants to get exactly D ml of coffee, and also he is so lazy that want the number of operations as least as possible. So he ask you for help.
Input
The first line is the case number T.
Each case has one line with four integers A B C D as mentioned above.
1 ≤ A, B, C ≤ 1000
1 ≤ D ≤ max(A, B, C)
1 ≤ T ≤ 100
Output
If he can get the exactly milliliter of coffee as he want, then print the least number of operation in a line.
And print the initial capacity of the three cups and then print the result after each operation line by line.
The print order should be the first cup, the second cup and the third cup.
If there are more than one operation schema, any of them will be accepted.
If he cannot get the exactly milliliter of coffee as he want , print "-1" without quotation.
Sample Input
1
12 8 5 10
Sample Output
5
12 0 0
7 0 5
0 7 5
5 7 0
5 2 5
10 2 0
Hint
Source
Author
周杰辉
#include<stdio.h>
#include<queue>
#include<iostream>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXN = 1e6 + 5;
struct node{
int cup[3],stp;
int hash(){ return cup[0] * 1001 * 1001 + cup[1] * 1001 + cup[2]; }
void print(){ printf("%d %d %d\n", cup[0], cup[1], cup[2]); }
};
queue<node>q;
set<int>se;
vector<node>ve;
node Q[MAXN];
int pre[MAXN];
int cup[3], d;
int bfs()
{
int l = 0,r=0;
node tmp;
tmp.cup[0] = cup[0]; tmp.cup[1] = tmp.cup[2]=tmp.stp = 0;
Q[++r] = tmp;
se.insert(tmp.hash());
while (l<r)
{
node u = Q[++l];
for (int i = 0; i < 3;i++)
if (u.cup[i])//对每个杯子
{
for (int j = 0; j < 3;j++)
if (i != j&&u.cup[j] != cup[j])//不同杯子 也没有满
{
node v = u;
if (v.cup[i] + v.cup[j]>cup[j])
{
v.cup[i] = v.cup[i] - (cup[j] - v.cup[j]);
v.cup[j] = cup[j];
}
else
{
v.cup[j] += v.cup[i];
v.cup[i] = 0;
}
v.stp = u.stp + 1;
int hash = v.hash();
if (!se.count(hash))//记录是否出现过该情况
{
se.insert(hash);
Q[++r] = v;
pre[r] = l;
for (int k = 0; k < 3; k++)
{
if (v.cup[k] == d)return r;
}
}
}
}
}
return -1;
}
int main()
{
int T;
while (~scanf("%d", &T))
{
while (T--)
{
while (!q.empty())
q. pop();
scanf("%d %d %d %d", &cup[0], &cup[1], &cup[2],&d);
se.clear();
int ans = bfs();
if (ans == -1)
{
printf("-1\n");
continue;
}
int cnt = Q[ans].stp;
ve.clear();
for (int i = ans; i; i = pre[i])
ve.push_back(Q[i]);
reverse(ve.begin(), ve.end());
printf("%d\n", cnt);
for (int i = 0; i < ve.size(); i++)
{
printf("%d %d %d\n", ve[i].cup[0], ve[i].cup[1], ve[i].cup[2]);
}
}
} return 0;
}
CSU - 2061 Z‘s Coffee的更多相关文章
- CSU - 2062 Z‘s Array
Description Z likes to play with array. One day his teacher gave him an array of n elements, and ask ...
- 【Python】使用torrentParser1.03对多文件torrent的分析结果
Your environment has been set up for using Node.js 8.5.0 (x64) and npm. C:\Users\horn1>cd C:\User ...
- CSU - 2059 Water Problem(Z线分割平面)
一条‘Z’形线可以将平面分为两个区域,那么由N条Z形线所定义的区域的最大个数是多少呢?每条Z形线由两条平行的无限半直线和一条直线段组成 Input 首先输入一个数字T(T<100),代表有T次询 ...
- CSU 1116 Kingdoms(枚举最小生成树)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1116 解题报告:一个国家有n个城市,有m条路可以修,修每条路要一定的金币,现在这个国家只 ...
- CSU 1113 Updating a Dictionary(map容器应用)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1113 解题报告:输入两个字符串,第一个是原来的字典,第二个是新字典,字典中的元素的格式为 ...
- CSU 1328 近似回文词(2013湖南省程序设计竞赛A题)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1328 解题报告:中文题题意就不说了.还好数据不大,只有1000,枚举回文串的中心位置,然 ...
- 字符串 - 近似回文词 --- csu 1328
近似回文词 Problem's Link:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1328 analyse: 直接暴力枚举每一个终点,然后枚举 ...
- CSU 1507 超大型LED显示屏 第十届湖南省赛题
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1507 解题思路:这是一道模拟题,看了那么多人的代码,我觉得我的代码是最简的,哈哈,其实就 ...
- 【最短路】【数学】CSU 1806 Toll (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1806 题目大意: N个点M条有向边,给一个时间T(2≤n≤10,1≤m≤n(n-1), ...
随机推荐
- [转载]RSA算法详解
原文:http://www.matrix67.com/blog/archives/5100 数论,数学中的皇冠,最纯粹的数学.早在古希腊时代,人们就开始痴迷地研究数字,沉浸于这个几乎没有任何实用价值的 ...
- 微信小程序开发教程(七)逻辑层——.js详解
逻辑层,是事务逻辑处理的地方.对于小程序而言,逻辑层就是.js脚本文件的集合.逻辑层将数据进行处理后发送给视图层,同时接收视图层的事件反馈. 微信小程序开发框架的逻辑层是由JavaScript编写.在 ...
- HDU 4370 0 or 1 (最短路)
[题目链接](http://acm.hdu.edu.cn/showproblem.ph Problem Description Given a n/n matrix Cij (1<=i,j< ...
- 【转换】Bean、List、Map、Array、String与JSON字符串的相互转换
import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.math.BigDecimal; i ...
- 引发类型为“System.OutOfMemoryException”的异常
在运维工作中,经常能接到客户的反馈这个:引发类型为“System.OutOfMemoryException”的异常.客户反馈物理内存都还有富余,怎么报内存不足的错误呢! 什么时候会引发System.O ...
- Go语言知识点笔记
golang的花括号: 在go中,继承了C系的花括号作为一个作用域块的包含范围指示,但不同于C/C++中花括号位置可任意摆放,go要求“ { ”必须在右侧(一行代码尾部),不能单独另起一行.类似Pyt ...
- 20155303狄惟佳预备作业三Linux学习笔记
20155303狄惟佳预备作业三Linux学习笔记 初次接触Ubuntu系统以及Linux内核,了解了其产生的历史,从感性来讲,深深吸引我的是其中蕴含的珍贵的开源精神,以及Stallman等人对&qu ...
- 20165230 《Java程序设计》实验二(Java面向对象程序设计)实验报告
20165230 <Java程序设计>实验二(Java面向对象程序设计)实验报告 一.实验报告封面 课程:Java程序设计 班级:1652班 姓名:田坤烨 学号:20165230 成绩: ...
- 个人对java中对象锁与类锁的一些理解与实例
一 什么是对象锁 对象锁也叫方法锁,是针对一个对象实例的,它只在该对象的某个内存位置声明一个标识该对象是否拥有锁,所有它只会锁住当前的对象,而并不会对其他对象实例的锁产生任何影响,不同对象访问同一个 ...
- 【杂谈】需要mark的一些东西
https://riteme.github.io/blog/2017-10-28/oi-coding-guidelines.html https://www.luogu.org/blog/34238/ ...