F-POJ-3414 Pots
来源:互联网 发布:大数据分析用的数据库 编辑:程序博客网 时间:2024/06/05 18:32
POJ-3414
Time Limit:1000 ms Memory Limit:65536 K 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 4Sample Output
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
BFS,然后模拟一个树来存走过的路径,压入栈。
//AC: 16MS 740KB#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<stack>using namespace std;char S[6][10]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};struct Node{ int step; int a,b;};struct { int num; int parent;}path[1000000]; //模拟树存路径bool vis[105][105];void BFS(int A,int B,int C){ Node now,next; memset(vis,false,sizeof(vis)); now.step=now.a=now.b=0; int j=0,k=-1; vis[0][0]=true; queue<Node>Q; stack<int>N; Q.push(now); while(!Q.empty()){ now=Q.front(); k++; Q.pop(); if(now.a==C||now.b==C){ N.push(path[k].num); while(path[k].parent){ k=path[k].parent; N.push(path[k].num); } printf("%d\n",now.step); for(int i=1;i<=now.step;i++){ k=N.top(); N.pop(); printf("%s\n",S[k]); } return; } for(int i=0;i<6;i++){ if(i==0){ next.a=A; next.b=now.b; if(!vis[next.a][next.b]){ next.step=now.step+1; Q.push(next); vis[next.a][next.b]=true; j++; path[j].num=i; path[j].parent=k; } } if(i==1){ next.a=now.a; next.b=B; if(!vis[next.a][next.b]){ next.step=now.step+1; Q.push(next); vis[next.a][next.b]=true; j++; path[j].num=i; path[j].parent=k; } } if(i==2){ next.a=0; next.b=now.b; if(!vis[next.a][next.b]){ next.step=now.step+1; Q.push(next); vis[next.a][next.b]=true; j++; path[j].num=i; path[j].parent=k; } } if(i==3){ next.a=now.a; next.b=0; if(!vis[next.a][next.b]){ next.step=now.step+1; Q.push(next); vis[next.a][next.b]=true; j++; path[j].num=i; path[j].parent=k; } } if(i==4){ next.a=now.a-(B-now.b); if(next.a<0) next.a=0; next.b=now.b+now.a; if(next.b>B) next.b=B; if(!vis[next.a][next.b]){ next.step=now.step+1; Q.push(next); vis[next.a][next.b]=true; j++; path[j].num=i; path[j].parent=k; } } if(i==5){ next.b=now.b-(A-now.a); if(next.b<0) next.b=0; next.a=now.a+now.b; if(next.a>A) next.a=A; if(!vis[next.a][next.b]){ next.step=now.step+1; Q.push(next); vis[next.a][next.b]=true; j++; path[j].num=i; path[j].parent=k; } } } } printf("impossible\n");}int main(){ int a,b,c; scanf("%d%d%d",&a,&b,&c); BFS(a,b,c); return 0;}
- F-POJ-3414 Pots
- POJ 3414 Pots
- poj 3414 pots
- POJ 3414 Pots
- POJ 3414 Pots BFS
- POJ 3414 Pots
- poj 3414 Pots
- poj 3414 Pots
- POJ 3414 Pots
- poj 3414 Pots
- POJ 3414(Pots)
- POJ 3414 Pots
- POJ 3414 Pots
- POJ-3414-Pots
- POJ 3414 pots
- POJ 3414 Pots
- POJ 3414 Pots
- POJ 3414 Pots
- C语言之单链表(增删改查逆)
- uva694
- Java基础之继承、多态、重载、重写
- poj-1452-按天数卖东西
- cts cmd
- F-POJ-3414 Pots
- textview文本超出部分用省略号表示
- js中的事件对象和添加事件
- 2017.07.24回顾 plt.hist直方图绘制
- 17-Vue实例与生命周期
- BZOJ 4592: [Shoi2015]脑洞治疗仪
- 山峰
- JDE TableIO达不到预想结果的原因
- oracle deferred_segment_creation 段延迟分配的一个隐患