POJ 3414
来源:互联网 发布:java常用包及类 编辑:程序博客网 时间:2024/06/12 21:47
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 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.
Input
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).
Output
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’.
Sample Input
3 5 4
Sample Output
6FILL(2)POUR(2,1)DROP(1)POUR(2,1)FILL(2)POUR(2,1)
Source
题意:
就是有4个操作,倒来倒去求获得c的水量。
POINT:
因为数据量小,DFS BFS都可以。感觉DFS写起来比较简单。就写了DFS,后来发现不是这样。因为要输出路径。
我就复制了一下DFS,在跑一次输出路径,所以代码很长。
#include <iostream>#include <stdio.h>#include <math.h>#include <stack>#include <string.h>using namespace std;int mp[111][111];int a,b,c;int ans=9999999;void dfs(int x,int y,int now){ if(x==0&&y==0&&now!=0) return; if(now>=ans) return; if(mp[x][y]<=now&&mp[x][y]!=0) return; mp[x][y]=now; if(x==c||y==c) { ans=min(now,ans); return; } if(x>0) { dfs(0,y,now+1); } if(y>0) { dfs(x,0,now+1); } if(x<a) { dfs(a,y,now+1);//fill a if(y!=0&&a-x>y) { dfs(x+y,0,now+1); } if(y!=0&&a-x<=y) { dfs(a,y-(a-x),now+1); } } if(y<b) { dfs(x,b,now+1);//fill b if(x!=0&&b-y>x) { dfs(0,x+y,now+1); } if(x!=0&&b-y<=x) { dfs(x-(b-y),b,now+1); } }}int ans1;int flag=0;stack<int> q;void dfs1(int x,int y,int now){ if(x==0&&y==0&&now!=0) return; if(now>ans1) return; if(mp[x][y]<=now&&mp[x][y]!=0) return; mp[x][y]=now; if(x==c||y==c) { ans=min(now,ans); if(ans1==ans) { flag=1; return; } return; } if(x>0) { dfs1(0,y,now+1); if(flag==1) { q.push(1); //printf("DROP(%d)\n",1); return; } } if(y>0) { dfs1(x,0,now+1); if(flag==1) { q.push(2); // printf("DROP(%d)\n",2); return; } } if(x<a) { dfs1(a,y,now+1);//fill a if(flag==1) { q.push(3); // printf("FILL(%d)\n",1); return; } if(y!=0&&a-x>y) { dfs1(x+y,0,now+1); if(flag==1) { q.push(4); // printf("POUR(%d,%d)\n",2,1); return; } } if(y!=0&&a-x<=y) { dfs1(a,y-(a-x),now+1); if(flag==1) { q.push(5); // printf("POUR(%d,%d)\n",2,1); return; } } } if(y<b) { dfs1(x,b,now+1);//fill b if(flag==1) { q.push(6); // printf("FILL(%d)\n",2); return; } if(x!=0&&b-y>x) { dfs1(0,x+y,now+1); if(flag==1) { q.push(7); // printf("POUR(%d,%d)\n",1,2); return; } } if(x!=0&&b-y<=x) { dfs1(x-(b-y),b,now+1); if(flag==1) { q.push(8); // printf("POUR(%d,%d)\n",1,2); return; } } }}int main(){ while(~scanf("%d %d %d",&a,&b,&c)) { ans=9999999; memset(mp,0,sizeof mp); dfs(0,0,0); ans1=ans; if(ans1==9999999) { printf("impossible\n"); continue; } flag=0; memset(mp,0,sizeof mp); dfs1(0,0,0); printf("%d\n",ans); while(!q.empty()) { int a=q.top(); q.pop(); if(a==1) { printf("DROP(%d)\n",1); } else if(a==2) { printf("DROP(%d)\n",2); } else if(a==3) { printf("FILL(%d)\n",1); } else if(a==4||a==5) { printf("POUR(%d,%d)\n",2,1); } else if(a==6) { printf("FILL(%d)\n",2); } else if(a==7||a==8) { printf("POUR(%d,%d)\n",1,2); } } } return 0;}
- POJ 3414
- POJ 3414
- POJ 3414
- poj 3414
- POJ 3414
- POJ 3414
- poj-3414
- poj 3414
- 3414POJ
- poj 3414
- POJ 3414
- poj 3414
- POJ-3414
- POJ 3414
- POJ 3414
- POJ 3414 Pots
- poj 3414 pots
- POJ 3414 Pots
- LeetCode.349(350) Intersection of Two Arrays && II
- js
- 练习11.2
- OHUI v42 for Android
- with语句的使用
- POJ 3414
- Appium移动 自动化测试-环境搭建(2)
- 浅谈IT领域的发展规律及成功发展案例
- jwhois 命令 --英语版啊!!!
- UICollectView自定义Layout布局
- java创建对象
- [BFS] POJ 3984
- HashMap的简单源码分析
- Centos配置yum源