当前位置:网站首页>POJ 3414 pots (bfs+ clues)
POJ 3414 pots (bfs+ clues)
2022-07-05 20:52:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack , I've prepared for you today Idea Registration code .
Pots
Time Limit: 1000MS | Memory Limit: 65536K | |||
---|---|---|---|---|
Total Submissions: 10071 | Accepted: 4237 | Special Judge |
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
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
Ideas : We all have 6 Kind of operation : hold A The water is poured out , hold A Fill it up with , hold B Pour the water in A in .B and A similar .
The maximum volume of the tank is 100, Set a constant N=100, Open a two-dimensional array to record the value of state change .
1、 From the tap to A Add water in t, Record -t,
2、 From the tap to B Add water in t, Record -t-N,
3、 from B Inside A Add water t, Record t
4、 from A Inside B Add water t. Record N+t
5、 hold A Pour out the water , Record 2*N+t,(A Original water t)
6、 hold B Pour out the water , Record 3*N+t,(B Original water t)
#include<stdio.h>
#include<queue>
#include<map>
#include<string>
#include<string.h>
using namespace std;
#define N 105
const int inf=0x1f1f1f1f;
int a,b,c,flag;
int mark[N][N];
struct node
{
int x,y,t;
friend bool operator<(node a,node b)
{
return a.t>b.t;
}
};
void prif(int x,int y) // Recursive output path
{
if(x==0&&y==0)
return ;
if(mark[x][y]>3*N)
{
prif(x,mark[x][y]-3*N);
printf("DROP(2)\n");
}
else if(mark[x][y]>2*N)
{
prif(mark[x][y]-2*N,y);
printf("DROP(1)\n");
}
else if(mark[x][y]>N)
{
int tmp=mark[x][y]-N;
prif(x+tmp,y-tmp);
printf("POUR(1,2)\n");
}
else if(mark[x][y]>0)
{
int tmp=mark[x][y];
prif(x-tmp,y+tmp);
printf("POUR(2,1)\n");
}
else if(mark[x][y]>-N)
{
int tmp=-mark[x][y];
prif(x-tmp,y);
printf("FILL(1)\n");
}
else if(mark[x][y]<-N)
{
int tmp=N+mark[x][y];
prif(x,y+tmp);
printf("FILL(2)\n");
}
}
void bfs()
{
priority_queue<node>q;
node cur,next;
mark[0][0]=inf; // This state can only appear once . The assignment is inf Prevent interference with other values
mark[a][0]=-a;
mark[0][b]=-b-N;
cur.t=1;
cur.x=a;
cur.y=0;
q.push(cur);
cur.x=0;
cur.y=b;
q.push(cur);
while(!q.empty())
{
cur=q.top();
q.pop();
next.t=cur.t+1;
if(cur.x==c||cur.y==c)
{
flag=1;
printf("%d\n",cur.t);
prif(cur.x,cur.y);
return ;
}
if(cur.x<a) // towards A Add water
{
int tmp=a-cur.x;
next.y=cur.y;
next.x=a; // Water from the tap
if(!mark[next.x][next.y])
{
mark[next.x][next.y]=-tmp;
q.push(next);
}
if(cur.y>0) // come from B Of water
{
int tmp=min(cur.y,a-cur.x);
next.x=cur.x+tmp;
next.y=cur.y-tmp;
if(!mark[next.x][next.y])
{
mark[next.x][next.y]=tmp;
q.push(next);
}
}
}
if(cur.y<b) // towards B Add water
{
int tmp=b-cur.y;
next.x=cur.x;
next.y=b; // Water from the tap
if(!mark[next.x][next.y])
{
mark[next.x][next.y]=-tmp-N;
q.push(next);
}
if(cur.x>0) // come from A Of water
{
int tmp=min(cur.x,b-cur.y);
next.y=cur.y+tmp;
next.x=cur.x-tmp;
if(!mark[next.x][next.y])
{
mark[next.x][next.y]=tmp+N;
q.push(next);
}
}
}
if(cur.x>0) // Pour out the water
{
int tmp=cur.x;
next.x=0;
next.y=cur.y;
if(!mark[next.x][next.y])
{
mark[next.x][next.y]=2*N+tmp;
q.push(next);
}
}
if(cur.y>0)
{
int tmp=cur.y;
next.y=0;
next.x=cur.x;
if(!mark[next.x][next.y])
{
mark[next.x][next.y]=3*N+tmp;
q.push(next);
}
}
}
}
int main()
{
while(scanf("%d%d%d",&a,&b,&c)!=-1)
{
memset(mark,0,sizeof(mark));
flag=0;
bfs();
if(!flag)
printf("impossible\n");
}
return 0;
}
Copyright notice : This article is an original blog article , Blog , Without consent , Shall not be reproduced .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/117660.html Link to the original text :https://javaforall.cn
边栏推荐
- Duchefa丨P1001植物琼脂中英文说明书
- 王老吉药业“关爱烈日下最可爱的人”公益活动在南京启动
- 2.8 basic knowledge of project management process
- shell编程100例
- Common view container class components
- Abnova blood total nucleic acid purification kit pre installed relevant instructions
- Abnova DNA marker high quality control test program
- Which securities is better for securities account opening? Is online account opening safe?
- Abnova CRISPR spcas9 polyclonal antibody protocol
- Abnova cyclosporin a monoclonal antibody and its research tools
猜你喜欢
台风来袭!建筑工地该如何防范台风!
基於flask寫一個接口
中国的软件公司为什么做不出产品?00后抛弃互联网;B站开源的高性能API网关组件|码农周刊VIP会员专属邮件周报 Vol.097
10000+ 代码库、3000+ 研发人员大型保险集团的研发效能提升实践
haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)
Abbkine丨TraKine F-actin染色试剂盒(绿色荧光)方案
Écrire une interface basée sur flask
Which is the best online collaboration product? Microsoft loop, notion, flowus
最长摆动序列[贪心练习]
Hongmeng OS' fourth learning
随机推荐
Is it safe to open a stock account by mobile phone? My home is relatively remote. Is there a better way to open an account?
[Yugong series] go teaching course in July 2022 004 go code Notes
Abnova丨CRISPR SpCas9 多克隆抗体方案
Maker education infiltrating the transformation of maker spirit and culture
Implementation of redis unique ID generator
Which is the best online collaboration product? Microsoft loop, notion, flowus
[record of question brushing] 1 Sum of two numbers
poj 3414 Pots (bfs+线索)
从架构上详解技术(SLB,Redis,Mysql,Kafka,Clickhouse)的各类热点问题
《SAS编程和数据挖掘商业案例》学习笔记# 19
Matplotlib drawing retouching (how to form high-quality drawings, such as how to set fonts, etc.)
Return to blowing marshland -- travel notes of zhailidong, founder of duanzhitang
Duchefa low melting point agarose PPC Chinese and English instructions
Duchefa d5124 md5a medium Chinese and English instructions
Abnova CRISPR spcas9 polyclonal antibody protocol
10000+ 代码库、3000+ 研发人员大型保险集团的研发效能提升实践
Abnova丨 CD81单克隆抗体相关参数和应用
Interpreting the daily application functions of cooperative robots
Duchefa丨D5124 MD5A 培养基中英文说明书
如何让化工企业的ERP库存账目更准确