时间限制: 1 s
空间限制: 128000 KB
题目等级 : 黄金 Gold
题目描述 Description
有两个无刻度标志的水壶,分别可装 x 升和 y 升 ( x,y 为整数且均不大于 100 )的水。设另有一水 缸,可用来向水壶灌水或接从水壶中倒出的水, 两水壶间,水也可以相互倾倒。已知 x 升壶为空 壶, y 升壶为空壶。问如何通过倒水或灌水操作, 用最少步数能在x或y升的壶中量出 z ( z ≤ 100 )升的水 来。
输入描述 Input Description
一行,三个数据,分别表示 x,y 和 z;
输出描述 Output Description
一行,输出最小步数 ,如果无法达到目标,则输出"impossible"
样例输入 Sample Input
3 22 1
样例输出 Sample Output
14
数据范围及提示 Data Size & Hint
这题,,,确实做的我懵逼。。。
首先是题目太恶心。。。有八种情况。。。刚开始我只考虑到4种。。。。
然后,记录步数的时候,step莫名其妙/2就是正确答案。。。
AC的一脸懵逼。。。。。
1 #include<iostream>
2 #include<cstdio>
3 #include<queue>
4 #include<cstdlib>
5 using namespace std;
6 const int MAXN=1001;
7 int vis[MAXN][MAXN];
8 int x,y,z;
9 int step=0;
10 int p1,p2;
11 void bfs()
12 {
13 queue<int>qx;queue<int>qy;
14 qx.push(x);qy.push(y);
15 while(qx.size()!=0&&qy.size()!=0)
16 {
17 int wx=qx.front();int wy=qy.front();
18 if(wx==z||wy==z)
19 {printf("%d",step/2);exit(0);}
20 qx.pop();
21 qy.pop();
22 if(wx<0||wy<0||wx>x||wy>y||vis[wx][wy]==1)continue;
23 vis[wx][wy]=1;
24 step++;
25 qx.push(x);qy.push(wy);// x灌满
26 qx.push(0);qy.push(wy);// 把x倒空
27 qx.push(wx);qy.push(y);// y灌满
28 qx.push(wx);qy.push(0);// 把y倒空
29 qx.push(0);qy.push(wy+wx);// x - > y no shenyu
30 qx.push(wx-y+wy);qy.push(y);// you shengyu
31 qx.push(wx+wy);qy.push(0);// y - > x noshengyu
32 qx.push(x);qy.push(wy-x+wx); // youshengyu...
33 }
34 }
35 int main()
36 {
37 scanf("%d%d%d",&x,&y,&z);
38 bfs();
39 printf("impossible");
40 return 0;
41 }