poj3414 pots 倒水模拟

避坑指南:Quectel EC200A-CN在OpenWrt或嵌入式Linux下的RNDIS/ECM网络配置全解析 本文详细解析了Quectel EC200A-CN模块在OpenWrt或嵌入式Linux下的RNDIS/ECM网络配置,包括模式选择、内核配置、动态切换AT指令、网络接口生成与排错、路由与防火墙设置、性能调优及特殊场景应对策略,帮助开发者避免常见配置陷阱,提升网络稳定性和效率。 阅读详情

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 ≤ i ≤ 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 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
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)

题意: 给你两个杯子,每个杯子容量不一定,每次对一个杯子可以进行如下操作: 倒满,倒水,将水倒到另一个杯子中去。求最短路径使得任一杯子中有C大小的水。


分析:我们需要模拟倒水的操作。可以发现,初始状态相同时结果是一样的,所以需要一个记忆化数组来避免重复操作(这里TLE了)。然后就是六个方向的bfs。。。

难点在于如何打印路径,我选择将每一个状态的路径记录到一个vector里,最后通过转化函数(turn)来打印路径。


代码:

#include<iostream>
#include<cstdio>
#include<string.h>
#include<math.h>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<queue>
#include<iomanip>
using namespace std;
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;

struct sta {
	int p1, p2;
	vector<int> act;		//记录每次操作
	int cnt;				//步数
	sta(int _cnt = 0) {
		cnt = _cnt;
	}
};
int a, b, c;
void fillpot(sta &temp, int i)
{
	temp.cnt++;
	if (i == 1) {
		temp.p1 = a;
		temp.act.push_back(1);
	}
	else {
		temp.p2 = b;
		temp.act.push_back(2);
	}
	return;
}
void drop(sta &temp, int i)
{
	temp.cnt++;
	if (i == 1) {
		temp.p1 = 0;
		temp.act.push_back(10);
	}
	else {
		temp.p2 = 0;
		temp.act.push_back(20);
	}
	return;
}
void pour(sta &temp, int i, int j)          // pour water from i to j
{
	temp.cnt++;
	if (i == 1) {
		temp.act.push_back(12);
		if (temp.p2 + temp.p1 > b) {
			int left = b - temp.p2;
			temp.p1 = temp.p1 - left;
			temp.p2 = b;
		}
		else {
			temp.p2 = temp.p2 + temp.p1;
			temp.p1 = 0;
		}
	}
	else {
		temp.act.push_back(21);
		if (temp.p1 + temp.p2 > a) {
			int left = a - temp.p1;
			temp.p2 = temp.p2 - left;
			temp.p1 = a;
		}
		else {
			temp.p1 = temp.p1 + temp.p2;
			temp.p2 = 0;
		}
	}
}
void turn(int t)
{
	if (t == 1) {
		printf("FILL(1)\n");
	}
	else if (t == 2) {
		printf("FILL(2)\n");
	}
	else if (t == 12) {
		printf("POUR(1,2)\n");
	}
	else if (t == 21) {
		printf("POUR(2,1)\n");
	}
	else if (t == 10) {
		printf("DROP(1)\n");
	}
	else {
		printf("DROP(2)\n");
	}
}
int main()
{
	while (scanf("%d%d%d", &a, &b, &c) != EOF) {
		int vis[200][200];			//记忆化数组
		memset(vis, 0, sizeof(vis[0][0]) * 200 * 200);
		vis[0][0] = 1;
		queue<sta> q;
		sta temp;
		temp.p1 = 0;
		temp.p2 = 0;
		q.push(temp);
		int flag = 0;
		while (!q.empty()) {
			temp = q.front();
			q.pop();
			if (temp.p1 == c || temp.p2 == c) {
				flag = 1;		//成功
				printf("%d\n", temp.cnt);
				while (!temp.act.empty()) {
					int t = temp.act.front();
					temp.act.erase(temp.act.begin());
					turn(t);
				}
				break;
			}
			if (temp.p1 != a && temp.p2 != b) {
				sta p1 = temp;
				fillpot(p1, 1);
				if (vis[p1.p1][p1.p2] == 0) {
					vis[p1.p1][p1.p2] = 1;
					q.push(p1);
				}
			}
			if (temp.p2 != b && temp.p1 != a) {
				sta p2 = temp;
				fillpot(p2, 2);
				if (vis[p2.p1][p2.p2] == 0) {
					vis[p2.p1][p2.p2] = 1;
					q.push(p2);
				}
			}
			if (temp.p1 != 0 && temp.p2 != 0) {
				sta p3 = temp;
				drop(p3, 1);
				if (vis[p3.p1][p3.p2] == 0) {
					vis[p3.p1][p3.p2] = 1;
					q.push(p3);
				}
			}
			if (temp.p2 != 0 && temp.p1 != 0) {
				sta p4 = temp;
				drop(p4, 2);
				if (vis[p4.p1][p4.p2] == 0) {
					vis[p4.p1][p4.p2] = 1;
					q.push(p4);
				}
			}
			if (temp.p1 != 0 && temp.p2 != b) {
				sta p5 = temp;
				pour(p5, 1, 2);
				if (vis[p5.p1][p5.p2] == 0) {
					vis[p5.p1][p5.p2] = 1;
					q.push(p5);
				}
			}
			if (temp.p1 != a && temp.p2 != 0) {
				sta p6 = temp;
				pour(p6, 2, 1);
				if (vis[p6.p1][p6.p2] == 0) {
					vis[p6.p1][p6.p2] = 1;
					q.push(p6);
				}
			}
		}
		if (flag == 0) printf("impossible\n");

	}
}


博途PLC PID仿真LSim_PT1库算法(一阶系统差分方程推导全过程) 博途PLC的PID仿真可以参看下面这篇博客,这篇文章我们主要论坛仿真库LSim的算法由来。如何利用博途PLC PID_Compact 进行PID仿真(一阶系统)_RXXW_Dor的博客-优快云博客博途PLC PID控制器 PID_Compact FB如何使用可以参看下面这篇文章,这里不再赘述。 阅读详情

相关推荐

CMAC算法原理

2、K1 通过以下运算导出:若 L 的最高有效位等于 0,则 K1 是 L 的左移 1 位。3、K2 通过如下运算导出:若 K1 的最高有效位等于 0,则 K2 是 K1 的左移 1 位。采用了 AES 算法中的类似 CBC 模式的加密方式,即对前一个分组的密文与当前分组的明文进行异或操作后在加密,2、分块处理:首先将消息分成多个固定长度的块,若消息长度不是块大小的倍数,则可以使用填充来将其填充到合适的大小。1、子密钥计算,通过主密钥生成子密钥(key1,key2),子密钥用于最后一个块处理;

sky的博客 1658

BFS-POJ-3414-Pots

Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11798 Accepted: 4992 Special Judge DescriptionYou are given two pots, having the volume of A and B liters respectivel

懒鬼的私人泳池 579

树莓派4B学习笔记一

树莓派4b烧录Ubuntu20.04&&配置Ubuntu桌面

m0_55202222的博客 3634

POJ 3414-Pots(BFS-模拟倒水)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15785   Accepted: 6661   Special Judge Description You are given two pots, having the volume of A and 

Some. 975

深入解析APS开源排程工具OptaPlanner:链式结构与移动优化

本文深入解析了开源APS排程工具OptaPlanner的核心机制,重点阐述了链式结构如何作为排程优化的“骨架”,通过单一`previous`变量隐式定义资源与顺序,从而简化建模。文章详细介绍了基于此结构的移动操作(如`ChainChangeMove`)如何驱动优化引擎探索解空间,并自动触发影子变量更新与分数计算,为构建高效、灵活的生产计划系统提供了实践指导。

keras9composer的博客 296

自动驾驶仿真(二)—— 基于PreScan与Simulink的联合仿真

自动驾驶仿真软件介绍二—— PreScan与Simulink联合仿真1. Prescan仿真软件2. Prescan与Simulink联合仿真2. 1 Prescan和matlab软件安装2. 2 联合仿真配置3. 联合仿真实例3.1 一辆不按法规行驶的小车3.2 另一辆的正常行驶的小车3.3 视觉辅助摄像机3.4 传感器检测4. 系列学习视频教程5. 参考学习的书目教材 上一篇博文简单介绍了Carsim与Simulink联合仿真,接着这篇就再继续简单介绍另一款自动驾驶仿真软件Prescan,并且简单做个P

zhudj08的博客 1万+

广搜最短路径变形,(POJ3414)

题目链接:http://poj.org/problem?id=3414 解题报告: 1、每个节点都是一个独立的状态 2、这里的状态转移就是有几种出路,4种:1、倒掉a中的水,2、把a中的水倒到b中去,3、倒掉b 中的水,4、把b中的水倒到a中去 3、每次的步骤记录到str[][]中去 4、搜索最短路径 #include <iostream> #includ...

weixin_33743880的博客 173

从零开始:Vivado中自定义串口通信IP核的完整实现

本文详细介绍了在Vivado中从零开始实现自定义串口通信IP核的完整流程,包括Verilog核心代码编写、仿真验证、IP核封装与参数化配置技巧。通过AXI-Lite总线实现动态配置,提升FPGA开发效率,适用于各种嵌入式系统通信需求。

weixin_29274969的博客 325

poj3414

尽管我的代码写的如此冗长,难入法眼。我承认自己的弱。但至少此刻我很开心。 import java.io.BufferedInputStream; import java.util.Collections; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; import java.util...

qq_24884193的博客 247

Autodesk全家桶:从AutoCAD到Maya,设计师必备的7款神器全解析

本文全面解析Autodesk全家桶中的7款设计师必备软件,包括AutoCAD、Revit、Inventor、Fusion 360、Maya和3ds Max等。从二维制图到三维建模,从建筑信息模型(BIM)到影视动画制作,这些工具在工程设计、建筑可视化和数字创意领域展现出强大的协同效应。通过实战案例展示如何整合工作流,显著提升设计效率与项目质量。

weixin_29237635的博客 460

亚马逊跨境电商选品数据分析:Python爬虫实战指南

本文将带领大家构建一套完整的亚马逊选品数据采集与分析系统,使用最新的Python技术栈,从零开始爬取亚马逊商品数据,并进行深度分析。

2201_76125261的博客 299

J - Pots

J - Pots Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3414 Description You are given two pots, having the volume of A and

CSUST_ACM的专栏 726

(SEED-Lab)Buffer Overflow Vulnerability Lab缓冲区溢出实验

(SEED-Lab)Buffer Overflow Vulnerability Lab 欢迎大家访问我的GitHub博客 https://lunan0320.cn 文章目录一、实验目的二、实验步骤与结果2.1 环境初始化2.2 Task1 运行shellcode2.3漏洞程序2.4 Task 22.5 Task 3: ASLR2.6 Task 4: Stack Guard2.7 Task 5:不可执行栈三、参考文献 一、实验目的 理解缓冲区溢出攻击的原理以及如何实施攻击 理解针对缓冲区溢出的防御措施

lunan的博客 8981

POJ 3414 Pots【bfs模拟倒水问题】

BFS 处理容器倒水问题 给你两个容器 A B 要你用这两个容器称量出 C 体积的水 同时输出路径

free斩 5291

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

POJ 3414 Pots(模拟倒水)

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

光明~~~ 2万+

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

bfs+模拟(poj3414 pots倒水问题)

PotsTime Limit: 1000MS Memory Limit: 65536KTotal Submissions: 21047 Accepted: 8976 Special JudgeDescriptionYou are given two pots, having the volume of A and B liters respectively. The following opera...

luojiushenzi的博客 284

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: FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap; D...

指弹键盘哼民谣 908

POJ3414 Pots —— BFS + 模拟

题目链接:http://poj.org/problem?id=3414 Pots Time Limit:1000MS Memory Limit:65536K Total Submissions:18550 Accepted:7843 Special Judge ...

alince20008的博客 122

H - Pots POJ - 3414(倒水问题+BFS回溯)

H - Pots POJ - 3414 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(...

石旭先森的博客 482

kuangbin专题 专题一 简单搜索 Pots POJ - 3414

Pots POJ - 3414 题目链接:https://vjudge.net/problem/POJ-3414 题意:给你两个杯子,分别容量为A(1),B(2)和一个C,C是需要经过下列操作,得到的一个升数。 (1) FILL(i) :把编号为i的杯子中水灌满 (2)DROP(i):把编号为i的杯子中水全部倒了 (3)POUR(i,j):把编号为i的杯子中的水倒入编号为j的杯子中,如果编号j杯子...

SSummerzz的博客 263

POJ 3414 Pots(水壶)

题目链接:POJ 3414 题意: 给两个容量分别为A,B的水壶,标号为1和2,初始水量都是0。每次操作有如下三种选择: ①FILL(i),将i水壶灌满(i=1或2) ②POUR(i),将i水壶中水倒完(i=1或2) ③DROP(i,j)。从i水壶倒水进j水壶,将j水壶倒满或i水壶中水倒完。 问能否经过若干次操作,使得其中一个水壶中的水量是C? 如果能,输出操作次数,以及操作历程

ramay7 628

POJ-3414 Pots(倒水问题-bfs手写队列)

Pots Time Limit: 1000MS Memory Limit: 65536K 题目链接http://poj.org/problem?id=3414 Description You are given two pots, having the volume of A and B liters respectively. The following operations can be p...

孤风的博客 436

POJ-3414(Pots)——BFS

题目大意:给你两个容器的容量A和B,经过以下操作后是否可以得到C量的水,如果可以输出倒水的步骤 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

shengtao96的专栏 553

Pots POJ - 3414

Pots POJ - 3414 小明给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作 FILL(i) 将第i个容器从水龙头里装满(1 ≤ i ≤ 2); DROP(i) 将第i个容器抽干 POUR(i,j) 将第i个容器里的水倒入第j个容器(这次操作结束后产生两种结果,一是第j个容器倒满并且第i个容器依旧有剩余,二是第i个容器里的水全部倒入j中,第i个容器为空) 现在要求你写一个程序,来找出能使其中任何一个容器里的水恰好有C升,找出最少操作数并给出操作过程

chenghuang_yi的博客 140
上一篇: FFT
下一篇: 背包集合
mwhybmj
博客等级 码龄10年 8粉丝 74原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值