POJ 3414 Pots(模拟倒水)

2023-11-21 21:18
文章标签 模拟 poj 倒水 3414 pots

本文主要是介绍POJ 3414 Pots(模拟倒水),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

  1. FILL(i)        fill the pot i (1 ≤ ≤ 2) from the tap;
  2. DROP(i)      empty the pot i to the drain;
  3. 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 AB, 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

6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)

题意:给出两个水杯的容量A B,问怎么操作可以使A B中一个的水量到C,题目保证AB中有一个大于C

    其实也就是一个简单的BFS,因为把顺序写错了,导致自己看了两天都没知道到错误,真的是脑残

     倒水也就有六种情况,分别让他们入队就行了。详解见代码


#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
int maxcup[2],endcup;//被子的容量和最终状态
int vis[110][110];//标记,vis[i][j]就是杯子1的水是i,杯子2的水是j
struct cupwater
{int curcup[2],step;//当前水量,步数int op[10000];//记录上一个的节点
};
queue<cupwater>Q;
cupwater p,q;
void Fillwater(int i)//往杯子里面倒水
{q=p;if(q.curcup[i]<maxcup[i])//如果杯子里面的水是满的就不用倒水了{q.curcup[i]=maxcup[i];if(i) q.op[q.step]=2;//记录倒水的杯子,方便输出else q.op[q.step]=1;q.step=q.step+1;if(vis[q.curcup[0]][q.curcup[1]]==0)//一种状态只能出现一次{Q.push(q);//入队vis[q.curcup[0]][q.curcup[1]]=1;//下次在遇到相同状态不处理}}
}
void Dropwater(int i)//把杯子里面的水倒出来
{q=p;if(q.curcup[i]>0&&q.curcup[i]<=maxcup[i])//其实只有当杯子一个杯子里面的水满了才会倒出,这样写完全是为了安全起见{q.curcup[i]=0;if(i) q.op[q.step]=4;else q.op[q.step]=3;q.step=q.step+1;if(vis[q.curcup[0]][q.curcup[1]]==0){Q.push(q);vis[q.curcup[0]][q.curcup[1]]=1;}}
}
void  Pourwater(int i,int j)//杯子i里面的水倒到j里面
{q=p;if(q.curcup[i]==0||q.curcup[j]==maxcup[j])//杯子i是空的或者杯子j是满的,直接跳出return ;if(q.curcup[i]>(maxcup[j]-q.curcup[j]))//杯子i里面的水多用不完的情况{q.curcup[i]-=(maxcup[j]-q.curcup[j]);q.curcup[j]=maxcup[j];if(i)q.op[q.step]=6;else q.op[q.step]=5;q.step=q.step+1;if(vis[q.curcup[0]][q.curcup[1]]==0){Q.push(q);vis[q.curcup[0]][q.curcup[1]]=1;}}else if(q.curcup[i]<=(maxcup[j]-q.curcup[j]))//杯子i里面的水不够倒满杯子j{q.curcup[j]=q.curcup[i]+q.curcup[j];//我把这两行写颠倒了,课害苦我了q.curcup[i]=0;if(i)q.op[q.step]=6;else q.op[q.step]=5;q.step=q.step+1;if(vis[q.curcup[0]][q.curcup[1]]==0){Q.push(q);vis[q.curcup[0]][q.curcup[1]]=1;}}
}int main()
{int i,j,f;while(~scanf("%d%d%d",&maxcup[0],&maxcup[1],&endcup)){memset(vis,0,sizeof(vis));f=0;p.curcup[0]=0;p.curcup[1]=0;p.step=0;Q.push(p);while(!Q.empty()){p=Q.front();Q.pop();if(p.curcup[0]==endcup||p.curcup[1]==endcup){f=1;printf("%d\n",p.step);for(i=0; i<p.step; i++){if(p.op[i]==1)printf("FILL(1)\n");if(p.op[i]==2)printf("FILL(2)\n");if(p.op[i]==3)printf("DROP(1)\n");if(p.op[i]==4)printf("DROP(2)\n");if(p.op[i]==5)printf("POUR(1,2)\n");if(p.op[i]==6)printf("POUR(2,1)\n");}break;}//六种情况分别考虑,能入队就入队Fillwater(0);Fillwater(1);Dropwater(0);Dropwater(1);Pourwater(0,1);Pourwater(1,0);vis[p.curcup[0]][p.curcup[1]]=1;}if(!f) printf("impossible\n");while(!Q.empty())Q.pop();}return 0;
}

这篇关于POJ 3414 Pots(模拟倒水)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/405152

相关文章

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

usaco 1.2 Transformations(模拟)

我的做法就是一个一个情况枚举出来 注意计算公式: ( 变换后的矩阵记为C) 顺时针旋转90°:C[i] [j]=A[n-j-1] [i] (旋转180°和270° 可以多转几个九十度来推) 对称:C[i] [n-j-1]=A[i] [j] 代码有点长 。。。 /*ID: who jayLANG: C++TASK: transform*/#include<

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

poj 1287 Networking(prim or kruscal最小生成树)

题意给你点与点间距离,求最小生成树。 注意点是,两点之间可能有不同的路,输入的时候选择最小的,和之前有道最短路WA的题目类似。 prim代码: #include<stdio.h>const int MaxN = 51;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int P;int prim(){bool vis[MaxN];

poj 2349 Arctic Network uva 10369(prim or kruscal最小生成树)

题目很麻烦,因为不熟悉最小生成树的算法调试了好久。 感觉网上的题目解释都没说得很清楚,不适合新手。自己写一个。 题意:给你点的坐标,然后两点间可以有两种方式来通信:第一种是卫星通信,第二种是无线电通信。 卫星通信:任何两个有卫星频道的点间都可以直接建立连接,与点间的距离无关; 无线电通信:两个点之间的距离不能超过D,无线电收发器的功率越大,D越大,越昂贵。 计算无线电收发器D

poj 1502 MPI Maelstrom(单源最短路dijkstra)

题目真是长得头疼,好多生词,给跪。 没啥好说的,英语大水逼。 借助字典尝试翻译了一下,水逼直译求不喷 Description: BIT他们的超级计算机最近交货了。(定语秀了一堆词汇那就省略吧再见) Valentine McKee的研究顾问Jack Swigert,要她来测试一下这个系统。 Valentine告诉Swigert:“因为阿波罗是一个分布式共享内存的机器,所以它的内存访问