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

周杰辉

设当前每杯的容量为x, y, z, 因为x + y + z = A 则可以枚举i, j,将第i杯中的咖啡倒入第j杯 如果满足条件,设倒入后的状态为x′,y′,z′ 判断其中是否有D,并记录其前驱 为保证操作最少,用BFS扩展即可
#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的更多相关文章

  1. 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 ...

  2. 【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 ...

  3. CSU - 2059 Water Problem(Z线分割平面)

    一条‘Z’形线可以将平面分为两个区域,那么由N条Z形线所定义的区域的最大个数是多少呢?每条Z形线由两条平行的无限半直线和一条直线段组成 Input 首先输入一个数字T(T<100),代表有T次询 ...

  4. CSU 1116 Kingdoms(枚举最小生成树)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1116 解题报告:一个国家有n个城市,有m条路可以修,修每条路要一定的金币,现在这个国家只 ...

  5. CSU 1113 Updating a Dictionary(map容器应用)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1113 解题报告:输入两个字符串,第一个是原来的字典,第二个是新字典,字典中的元素的格式为 ...

  6. CSU 1328 近似回文词(2013湖南省程序设计竞赛A题)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1328 解题报告:中文题题意就不说了.还好数据不大,只有1000,枚举回文串的中心位置,然 ...

  7. 字符串 - 近似回文词 --- csu 1328

    近似回文词 Problem's Link:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1328 analyse: 直接暴力枚举每一个终点,然后枚举 ...

  8. CSU 1507 超大型LED显示屏 第十届湖南省赛题

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1507 解题思路:这是一道模拟题,看了那么多人的代码,我觉得我的代码是最简的,哈哈,其实就 ...

  9. 【最短路】【数学】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), ...

随机推荐

  1. Web Uploader

    Github上的例子没看太明白,在网上找了些资料自己写了个demo,基本上就是用create方法初始化,然后on一堆事件,上传的进度条用的是swf格式的动画,感觉不是很先进的样子.不过我暂时也没搞明白 ...

  2. JQuery的选择器对控件ID含有特殊字符的解决方法-涨姿势了!

    1.jquery类库在我们实际项目中用的很多,大家经常需要根据控件的id,获取对应的html元素. 但是:当id含有特殊字符的时候,是不能选中的. 2. 自己简单的测试了下,jquery的id选择器只 ...

  3. 【CodeForces】741 D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on tree)

    [题意]给定n个点的树,每条边有一个小写字母a~v,求每棵子树内的最长回文路径,回文路径定义为路径上所有字母存在一种排列为回文串.n<=5*10^5. [算法]dsu on tree [题解]这 ...

  4. 20155217 2016-2017-2 《Java程序设计》第8周学习总结

    20155217 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 15.1日志 15.1.1日志API简介 java.util.logging包提供了日志功能 ...

  5. ashx误删后,未能创建类型

    描述 今天,因为临时有事儿,需要去一趟其他城市,项目比较赶.所以只能在车上继续敲代码,倒霉的触摸板让我误删一个ashx一般处理程序.好死不死的这个文件的代码还很长. 我的做法是[垃圾桶]→[还原]→V ...

  6. input输出类型

    http://www.w3school.com.cn/html5/html_5_form_input_types.asp

  7. 字符加密 cipher

    评测传送门 Description: Valentino 函数的定义: 对于一个由数字和小写字母组成的字符串 S,两个整数 K,M,将 S 视为一个 P 进制数,定义: Valentino(S, K, ...

  8. Linux下ssh的使用

    更多内容推荐微信公众号,欢迎关注: 摘抄自:https://www.cnblogs.com/kevingrace/p/6110842.html 对于linux运维工作者而言,使用ssh远程远程服务器是 ...

  9. html5 构造网页的新方式

    从 html 诞生至今,我们构建 html 页面的使用 html 元素好像并没有太多的进步.在构建 html 页面中,用的最多的是 div 标签.但是应用 div 标签构建 html 页面有一个问题, ...

  10. Dream------spark--spark集群的环境搭建

    1.下载安装scala http://www.scala-lang.org/download/2.11.6.html   2.解压下载后的文件,配置环境变量:编辑/etc/profile文件,添加如下 ...