POJ3414——Pots
来源:互联网 发布:外国身份证 知乎 编辑:程序博客网 时间:2024/05/17 23:48
Description
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 potj is full (and there may be some water left in the pot i), or the poti 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 exactlyC liters of water in one of the pots.
Input
On the first and only line are the numbers A, B, andC. These are all integers in the range from 1 to 100 and C≤max(A,B).
Output
The first line of the output must contain the length of the sequence of operationsK. 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’.
Sample Input
3 5 4
Sample Output
6FILL(2)POUR(2,1)DROP(1)POUR(2,1)FILL(2)POUR(2,1)
Source
啊哈,和杭电的一题差不多,每个状态拿过来最多可以新产生6个状态,做好剪枝和标记,就可以AC了
#include<queue>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;bool vis[105][105]; struct node{ int A, B; int num; int code; int from, to; int cnt;};node pre[1001000];void change(node &temp2, node &temp1, int t){temp2.A = temp1.A;temp2.B = temp1.B;temp2.cnt = temp1.cnt + 1;temp2.num = temp1.num + t;pre[temp2.num] = temp1;}node bfs(int A, int B, int C){queue< node >qu;memset( vis, 0, sizeof(vis) );while( !qu.empty() )qu.pop(); int cnt = 1;node temp1, temp2, ans;bool flag = false;temp1.A = 0;temp1.B = 0;temp1.num = 0;//????temp1.cnt = 0;temp1.code = -1;//????,//pre[temp1.num] = temp1;qu.push(temp1);while( !qu.empty() ){temp1 = qu.front();qu.pop();if( temp1.A == C || temp1.B == C){flag = true;ans = temp1;break;}if(temp1.A != 0)//??A???{ change(temp2, temp1, cnt ++);temp2.A = 0;temp2.code = 2;temp2.from = 1;if( !vis[temp2.A][temp2.B] ){vis[temp2.A][temp2.B] = 1;qu.push(temp2);}}if(temp1.B != 0){ change(temp2, temp1, cnt ++);temp2.B = 0;temp2.code = 2;temp2.from = 2;if( !vis[temp2.A][temp2.B] ){vis[temp2.A][temp2.B] = 1;qu.push(temp2);}} if(temp1.A != A)//???,????{ change(temp2, temp1, cnt ++);temp2.A = A;temp2.code = 1;temp2.from = 1;if( !vis[temp2.A][temp2.B] ){vis[temp2.A][temp2.B] = 1;qu.push(temp2);}}if(temp1.B != B)//???,????{ change(temp2, temp1, cnt ++);temp2.B = B;temp2.code = 1;temp2.from = 2;if( !vis[temp2.A][temp2.B] ){vis[temp2.A][temp2.B] = 1;qu.push(temp2);}}if( temp1.A != 0 && temp1.B != B)//A??,B??,?A?B{ change(temp2, temp1, cnt ++);temp2.code = 3;temp2.from = 1;temp2.to = 2;if(temp2.A + temp2.B <= B){temp2.B += temp2.A;temp2.A = 0;if( !vis[temp2.A][temp2.B] ){vis[temp2.A][temp2.B] = 1;qu.push(temp2);}}else{temp2.A -= (B - temp2.B);temp2.B = B;if( !vis[temp2.A][temp2.B] ){vis[temp2.A][temp2.B] = 1;qu.push(temp2);}}}if( temp1.B != 0 && temp1.A != A)//B??,A??,?B?A{ change(temp2, temp1, cnt ++);temp2.code = 3;temp2.from = 2;temp2.to = 1;if(temp2.A + temp2.B <= A){temp2.A += temp2.B;temp2.B = 0;if( !vis[temp2.A][temp2.B] ){vis[temp2.A][temp2.B] = 1;qu.push(temp2);}}else{temp2.B -= (A - temp2.A);temp2.A = A;if( !vis[temp2.A][temp2.B] ){vis[temp2.A][temp2.B] = 1;qu.push(temp2);}}}}if(flag)return ans;ans.num = -1;return ans;}void print(node temp){if(temp.num != 0)print( pre[temp.num] );if(temp.code == 1){printf("FILL(%d)\n", temp.from);}else if(temp.code == 2){printf("DROP(%d)\n", temp.from);}else if(temp.code == 3){printf("POUR(%d,%d)\n", temp.from, temp.to);}}int main(){int A, B, C;while(~scanf("%d%d%d", &A, &B, &C)){node ans = bfs(A, B, C);if(ans.num == -1){printf("impossible\n");continue;}printf("%d\n", ans.cnt);print(ans);}return 0;}
- poj3414——Pots
- POJ3414——Pots
- poj3414——Pots
- BFS—POJ3414-Pots
- poj3414——Pots(BFS)
- POJ3414 Pots —— BFS + 模拟
- POJ3414 Pots
- poj3414 Pots
- poj3414 Pots
- poj3414 - Pots
- POJ3414--Pots
- POJ3414 Pots
- poj3414 Pots
- poj3414 Pots
- POj3414 Pots
- poj3414 Pots
- POJ3414---Pots
- POJ3414--- Pots
- c++堆和栈
- HashMap与HashTable区别(Differences between HashMap and Hashtable)
- ArcGIS教程:网络分析工作流
- 64位Win7下安装与配置PHP环境【Apache+PHP+MySQL】
- ifconfig lo的作用
- POJ3414——Pots
- this project is not a myeclipse hibernate project . assuming hibernate 3 cap
- 体验JAVA中的构造函数
- 日志字段提取优化方案
- Cocos2d-x:制作一个动态的精灵
- 多桌面热键消息挂钩
- 黑马程序员---Java多线程(1)
- struct中成员分配大小
- 栈区,堆区,静态存储区,文字常量存储区,代码区的区别--详解