博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj3414 Pots (BFS)
阅读量:4339 次
发布时间:2019-06-07

本文共 3028 字,大约阅读时间需要 10 分钟。

Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12198   Accepted: 5147   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

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

Source

, Western Subregion

 

空杯子倒水问题,这题比较简单,只有两个杯子,最初的时候都是空的,要倒出指定量的水有三种操作:

1、FILL(i)        把第i个杯子装满(i=0,1)

2、DROP(i)      把第i个杯子倒空

3、POUR(i,j)    把i的水倒入到j中,直到j满或i倒完

我的想法:把a->b,b->c,。。。。共6种倒水方法一个一个列出来,而每种都是一样的讨论方法,

虽然很好做,但对于我这样的入门级水手来说还是写不出太好看的代码,所以。。。。

看看吧,看不懂再去看看网上别人的==||

#include
#include
#include
#include
#include
using namespace std;const int maxn=500;bool vis[maxn][maxn];int fa[maxn][maxn];int op[maxn][maxn];int sa,sb,sc;struct node{ int a,b,step;};char ans[6][20]={
"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};void print(int a ,int b){ if(op[a][b]==-1) return; else{ print(fa[a][b]/1000,fa[a][b]%1000); printf("%s\n",ans[op[a][b]]); }}bool bfs(){ node u,v; u.a=0,u.b=0; u.step=0; queue
q; q.push(u); memset(op,0,sizeof(op)); memset(vis,false,sizeof(vis)); memset(fa,0,sizeof(fa)); op[0][0]=-1; fa[0][0]=0; vis[0][0]=true; while(!q.empty()){ u=q.front(); q.pop(); if(u.a==sc||u.b==sc){ printf("%d\n",u.step); print(u.a,u.b); return true; } v=u; if(u.a!=sa){ v.a=sa; if(!vis[v.a][v.b]){ v.step++; q.push(v); op[v.a][v.b]=0; vis[v.a][v.b]=true; fa[v.a][v.b]=u.a*1000+u.b; } }v=u; if(u.b!=sb){v.b=sb; if(!vis[v.a][v.b]){ v.step=u.step+1; q.push(v); op[v.a][v.b]=1; vis[v.a][v.b]=1; fa[v.a][v.b]=u.a*1000+u.b; } }v=u; if(u.a){v.a=0; if(!vis[v.a][v.b]){ v.step++; q.push(v); op[v.a][v.b]=2; vis[v.a][v.b]=true; fa[v.a][v.b]=u.a*1000+u.b; } }v=u; if(u.b){v.b=0; if(!vis[v.a][v.b]){ v.step++; q.push(v); op[v.a][v.b]=3; vis[v.a][v.b]=true; fa[v.a][v.b]=u.a*1000+u.b; } }v=u; if(u.a){ if(v.a>=sb-u.b&&u.b!=sb){v.a-=(sb-u.b);v.b=sb;} else if(v.a
=sa-u.a&&u.a!=sa){v.b-=(sa-u.a);v.a=sa;} else if(v.b

 

转载于:https://www.cnblogs.com/13224ACMer/p/4764387.html

你可能感兴趣的文章
click禁用事件
查看>>
vue 高级属性父组件provide向子组件发送数据,子组件通过inject接收数据
查看>>
vue 项目启动时npm run dev 报错
查看>>
【转】算法之堆排序
查看>>
centos 基础知识
查看>>
[hdu2222] [AC自动机模板] Keywords Search [AC自动机]
查看>>
SRS之接收推流线程:recv
查看>>
mysqldump实践
查看>>
Laravel 视图调用model方法
查看>>
Extjs4 文件目录结构
查看>>
java空指针异常
查看>>
【BZOJ】【1520】【POI2006】Szk-Schools
查看>>
16 MySQL--正确使用索引
查看>>
Ubuntu下安装Kafka Manager
查看>>
使用Aspose.Cells的基础知识整理
查看>>
前端知识点回顾之重点篇——ES6的async函数和module
查看>>
UML类图详解_补上相关代码
查看>>
Angularjs中的Controller
查看>>
HDU_1527 取石子游戏(威佐夫博弈)
查看>>
node的“宏任务(macro-task)”和“微任务(micro-task)”机制
查看>>