POJ3414——Pots

【代码超详解】POJ 3414 Pots(BFS,0 ms) 一、题目描述 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29772 Accepted: 12428 Special Judge Description You are given two pots, having the volume of A and B liters respectively. The ... 阅读详情
Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10021 Accepted: 4209 Special Judge

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 ≤ 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)

Source

Northeastern Europe 2002, Western Subregion


啊哈,和杭电的一题差不多,每个状态拿过来最多可以新产生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;
}


Robot Arm 机械臂源码解析 由于我们用的是步进电机,运动的时候是一步一步的,并不是平滑的,这里假设在一个平面上面,一个x步进电机,一个y步进电机。那么放大它们所走的轨迹就会是阶梯状的。而它们也只能沿着网格运动。相当于有了一个最小的分辨率,这个分辨率就是一步。如下图所示,如果x,y电机同时走就能走出45度的斜线。在三维空间里面运动也是如此,你应该可以想象的出来;首先,是updateActualPosition,用来时时更新此时的差补位置,得到的是一个路径与时间t的函数(其中路径的范围为0,1,时间范围为0,dist/v)... 阅读详情

相关推荐

Fluent动网格实战:5种区域运动类型详解与配置避坑指南

本文详细解析了Fluent动网格技术中的五种区域运动类型,包括固定、刚体、变形、用户自定义和系统耦合,并提供了配置技巧与避坑指南。通过实际案例和参数优化建议,帮助工程师有效解决网格变形、计算稳定性等常见问题,提升CFD仿真效率。

weixin_28339967的博客 415

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

Jetson设备EXT4文件系统修复实战:从superblock损坏到系统恢复

本文详细介绍了Jetson设备因异常断电等原因导致EXT4文件系统superblock损坏后的诊断与修复实战。通过从健康设备移植fsck工具、使用备份超级块等步骤,指导用户逐步恢复系统,并提供了数据备份与预防措施,帮助用户有效应对嵌入式AI开发中的存储故障。

theta的博客 592

pots题解(BFS简单应用)

题目 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 p...

qq_43301061的博客 1897

《Python Web项目集锦》-专栏介绍和目录

本专栏专注于Python Web项目,涉及内容包含FastAPI、Django、Flask等,所有项目代码完整且可直接运行。不管是零基础还是有经验的开发,相信都能从本专栏获益。有任何问题欢迎私聊博主,专栏持续更新中...

数据知道的博客 5213

WPF在特定领域的应用:打造一款专业的图像编辑工具

WPF在特定领域的应用:打造一款专业的图像编辑工具,在软件开发的广阔领域中,Windows Presentation Foundation(WPF)以其强大的图形渲染能力和丰富的用户界面控件,为开发者提供了构建高性能、美观应用程序的平台。图像编辑工具作为一种对图形处理要求极高的应用类型,WPF 的特性使其成为理想的选择。本文将深入探讨如何使用 WPF 打造一款专业的图像编辑工具,涵盖从基础概念到实际代码实现的各个方面。

不踩坑,不知道坑有多深;不总结,踩过的坑白踩。 6万+

oj_03_pots题解(BFS应用)

题目: 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

Loss 452

记住这两个纯净版Windows11各系列版本系统下载网站

记住这两个纯净版Windows11各系列版本系统下载网站 HelloWindows是一个完整极致的Windows纯净系统下载站,不夹带任何私货,系统全部来源于微软官方原版,本站只是收录官方发布的系统以及工具,方便大家下载,请放心使用。永久在线:www.hellowindows.cn。HelloWindows.cn - 精校 完整 极致 Windows系统下载仓储站。

拥抱具身智能,昇腾Ascend C 7021

POJ 3414 Pots

POJ 3414 Pots题目描述 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

退役的铜牌狗 753

【图像超分】论文复现:Pytorch实现ESPCN

论文精读:由于ESPCN没有公布源码,我们选择github上一个高star的项目来复现。准备数据集,以及数据预处理搭建网络模型设置参数并训练测试训练好的模型用训练好的模型测试自己的数据本文我们使用pycharm链接服务器训练。

主要更新底层视觉(去噪、超分等)相关的科研内容,形式为【论文精读】+【论文复现】 1万+

Ubuntu 部署 Ollama 避坑指南:下载失败 / 服务崩溃?一招解决所有问题!

本文详细介绍了在Ubuntu系统上安装Ollama大模型工具的完整流程和常见问题解决方案。主要内容包括:环境检查(系统版本、架构、依赖)、一键安装步骤、端口配置和服务启动等核心操作。重点针对下载失败、服务启动异常、权限不足等高频问题,提供了国内镜像配置、手动创建服务文件、用户组添加等实用解决方案。最后通过测试Qwen2.5轻量模型验证安装效果,并给出低配设备选型建议。全文提供可复制的命令行操作,帮助用户快速搭建离线可用的大模型环境。

2501_93651177的博客 1008

Pots-简单的BFS问题

描述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 ...

lrf2454224026的博客 314

Spring Boot:如何快速搭建一个高性能微服务架构?

通过本文的详细步骤,我们成功搭建了一个高性能的微服务架构。Spring Boot 和 Spring Cloud 提供了强大的工具链,使得开发者能够快速实现服务注册与发现、负载均衡、熔断器与降级机制等功能。同时,通过数据库连接池、Redis 缓存和异步处理等优化手段,进一步提升了系统的性能和可靠性。希望本文能够帮助你快速入门 Spring Boot 微服务架构的搭建,并为实际项目提供参考。如果你有任何问题或建议,欢迎在评论区留言!

shrgegrb的博客 1013

POJ 3414 Pots(bfs)

Pots

Pandora_heart的博客 336

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

【POJ 3414 --- Pots】BFS

【POJ 3414 --- Pots】BFS 题目来源:点击进入【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 t...

来日浅谈的博客 521

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

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

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

POJ 3414 Pots(罐子)

POJ3414 Pots(罐子) TimeLimit:1000MSMemoryLimit:65536K Description- 题目描述 You are given two pots, having the volume ofAandBliters respectively. The following operations can b...

246

POJ 3414 Pots【BFS水】

POJ 3414 Pots大意:有两只桶,容量分别为a,b,对每只桶,有以下三种操作:1.Fill(i)装满第i只桶2.DROP(i)将桶i的水倒掉3.POUR(i,j),将桶i中的水倒入桶j,若桶j满,剩余的水遗留于桶i中 问能否通过这些操作使得某只桶恰好装有容量为c的水?若能,输出最少操作次数及过程PS:c<max(a,b); 算法:BFS即可 #include&...

a48634331的博客 149

POJ - 3414 Pots BFS + 记录并输出路径

POJ - 3414 Pots 题意: 给你两个杯子的容量 和 最后杯子中剩余水的容量。有三种操作方式 FILL(i)        将第 i 个杯子装满水 DROP(i)      将第 i 个杯子的水倒空  POUR(i,j)     将第 i 个杯子中的水倒入第 j 个杯子中     可能有两种情况:(1) i 杯子中的水还没全倒入 j 杯子中 j 杯子就满了,此时 i 杯子中还...

没有梦想何以远行 272

poj 3414 Pots 【BFS+记录路径 】

//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了。然后第一次用对拍去找特殊数据折腾到十二点半终于AC,最后只想感叹没有仔细读题,没办法啊看着英语略烦躁,不扯了,那个题题解不想写了,回到这题。。。今天中午还是花了四十分钟写了这题(好慢啊orz),感觉,...

dhe56357的博客 152

倒水(BFS)Pots POJ - 3414

Pots POJ - 3414 题目链接 bfs题; 标注的状态应为2容器的体积,因为每种2容器的体积都应该只由一种状态达到。 不使用队列 #include<stdio.h> #include<string.h> struct node{ int now_x; int now_y; int step; int howcome; int before; }ll[10050];...

qq_43559193的博客 262

POJ 3414 Pots (BFS/DFS)

Pots Time Limit:1000MS Memory Limit:65536K Total Submissions:7783 Accepted:3261 Special Judge Description You are given two pots, having the volume ofA...

airen6935的博客 190

POJ 3414-Pots(暑假头疼jixun)

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...

qq_43813140的博客 314

POJ-3414 Pots

POJ-3414 Pots 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 po

sdudyl的博客 216

POJ 3414 -- Pots

POJ 3414 -- Pots 题意: 给出了两个瓶子的容量A,B, 以及一个目标水量C, 对A、B可以有如下操作: 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 po...

weixin_30416497的博客 150

POJ 3414 Pots 均分水(bfs)

题目链接: POJ 3414 Pots 题目大意: 有两个水杯,一开始都没有装水,给出两个水杯的容量,和要达到的目标容量 每个水被有三种操作:1.装满水,2.倒掉所有水,3,将水倒入另一个杯子中. 思路: 两个杯子,六种操作,,求最短路,并打印路径,6入口bfs,跟非常可乐一题类似 Description You are given two pots, having the

Dextrad_ihacker的博客 1342
上一篇: hdu1495——非常可乐
下一篇: POJ1129——Channel Allocation
tokers
博客等级 码龄12年 71粉丝 700原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值