18.06.27 POJ 3414:Pots
描述
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
- FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
- DROP(i) empty the pot i to the drain;
- POUR(i,j) pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).
Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.
输入
On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).
输出
The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.
样例输入
3 5 4
样例输出
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
1 #include <cstdio> 2 #include <string> 3 #include <memory.h> 4 #include <algorithm> 5 #include <stdlib.h> 6 #include <math.h> 7 #include <iostream> 8 #include<queue> 9 #include <vector> 10 #include <bitset> 11 using namespace std; 12 13 int volume[3]; 14 int c; 15 bool flag = false; 16 int visited[105][105]; 17 18 struct state { 19 int pot[3]; 20 vector<int> oper; 21 int step; 22 state(int _x, int _y) { 23 step = 0; 24 pot[1] = _x, pot[2]=_y; 25 } 26 void fill(int x) { 27 pot[x] = volume[x]; 28 } 29 void drop(int x) { 30 pot[x] = 0; 31 } 32 void pour(int x, int y) { 33 if (pot[x] <= (volume[y]-pot[y])) { 34 pot[y] += pot[x]; 35 pot[x] = 0; 36 } 37 else if (pot[x] >(volume[y]-pot[y])) { 38 pot[x] -= volume[y] - pot[y]; 39 pot[y] = volume[y]; 40 } 41 } 42 }; 43 queue<state> all; 44 45 void bfs() { 46 while (!all.empty()) { 47 state now = all.front(); 48 all.pop(); 49 if (now.pot[1] == c || now.pot[2] == c) { 50 flag = true; 51 cout << now.step << endl; 52 vector<int>::iterator i1 = now.oper.begin(), i2 = now.oper.end(); 53 for (; i1 != i2; i1++) { 54 if (*i1 == 1) 55 printf("FILL(1)\n"); 56 else if (*i1 == 2) 57 printf("FILL(2)\n"); 58 else if (*i1 ==3) 59 printf("DROP(1)\n"); 60 else if (*i1 == 4) 61 printf("DROP(2)\n"); 62 else if (*i1 == 5) 63 printf("POUR(1,2)\n"); 64 else if (*i1 == 6) 65 printf("POUR(2,1)\n"); 66 } 67 return; 68 } 69 state newone(now); 70 if(newone.pot[1]!=volume[1]) 71 newone.fill(1); 72 if (visited[newone.pot[1]][newone.pot[2]] == 0) 73 { 74 newone.step++; 75 newone.oper.push_back(1); 76 visited[newone.pot[1]][newone.pot[2]] = 1; 77 all.push(newone); 78 } 79 newone = now; 80 if (newone.pot[2] != volume[2]) 81 newone.fill(2); 82 if (visited[newone.pot[1]][newone.pot[2]] == 0) 83 { 84 newone.step++; 85 newone.oper.push_back(2); 86 visited[newone.pot[1]][newone.pot[2]] = 1; 87 all.push(newone); 88 } 89 newone = now; 90 if (newone.pot[1] != 0) 91 newone.drop(1); 92 if (visited[newone.pot[1]][newone.pot[2]] == 0) 93 { 94 newone.step++; 95 newone.oper.push_back(3); 96 visited[newone.pot[1]][newone.pot[2]] = 1; 97 all.push(newone); 98 } 99 newone = now; 100 if (newone.pot[2] != 0) 101 newone.drop(2); 102 if (visited[newone.pot[1]][newone.pot[2]] == 0) 103 { 104 newone.step++; 105 newone.oper.push_back(4); 106 visited[newone.pot[1]][newone.pot[2]] = 1; 107 all.push(newone); 108 } 109 newone = now; 110 if (newone.pot[1] != 0) 111 newone.pour(1, 2); 112 if (visited[newone.pot[1]][newone.pot[2]] == 0) 113 { 114 newone.step++; 115 newone.oper.push_back(5); 116 visited[newone.pot[1]][newone.pot[2]] = 1; 117 all.push(newone); 118 } 119 newone = now; 120 if (newone.pot[2] != 0) 121 newone.pour(2, 1); 122 if (visited[newone.pot[1]][newone.pot[2]] == 0) 123 { 124 newone.step++; 125 newone.oper.push_back(6); 126 visited[newone.pot[1]][newone.pot[2]] = 1; 127 all.push(newone); 128 } 129 } 130 } 131 132 int main() 133 { 134 scanf("%d%d%d", &volume[1], &volume[2], &c); 135 state origin(0, 0); 136 all.push(origin); 137 visited[0][0] = 1; 138 bfs(); 139 if (flag == false) 140 printf("impossible\n"); 141 return 0; 142 }
没看见要输出步数……
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥