博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3414 Pots 记录路径的广搜
阅读量:6984 次
发布时间:2019-06-27

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

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)题意:有两个空瓶 a,b是它们的容量,c是容量目标。 能够有三种操作 充满随意一瓶。倒空随意一瓶,将随意一瓶倒入还有一瓶(能剩下但不能溢出);求随意一瓶的体积达到目标体积所须要的最小操作数。并依此输出该操作。代码:
#include
#include
#include
#include
using namespace std;int m,n,k;int vis[105][105];char opr[20][20]= {" " , "FILL(1)" , "FILL(2)" , "DROP(1)" , "DROP(2)" , "POUR(1,2)" , "POUR(2,1)" }; //共6种操作struct node{ int x,y,step; int w[200]; //用来记录路径的数组数组 };void bfs( ){ int i,j; int kx,ky; memset(vis,0,sizeof(vis)); queue
q; node now,next; now.x=0,now.y=0,now.step=0; q.push(now); vis[0][0]=1; while(!q.empty()) { now=q.front(); q.pop(); if(now.x==k||now.y==k) { cout<
<
now.x) //每种pour 应有两种情况 { next.x=0; next.y=now.x+now.y; } else { next.y=n; next.x=now.x-n+now.y; } now.w[now.step+1]=5; } else if(i==5) { if(m-now.x>now.y) { next.y=0; next.x=now.x+now.y; } else { next.x=m; next.y=now.y-m+now.x; } now.w[now.step+1]=6; } if(vis[next.x][next.y]==1) continue; vis[next.x][next.y]=1; next.step=now.step+1; for(j=1; j<=next.step; j++) //记录之前的行动 { next.w[j]=now.w[j]; } q.push(next); } } cout<<"impossible"<
>m>>n>>k) { bfs(); } return 0;}
通过自己对队列的理解成功写出了代码 还是挺开心的。

。。

转载地址:http://gmvpl.baihongyu.com/

你可能感兴趣的文章
今年光伏市场规模可达30GW 分布式有望占据三分江山
查看>>
因新漏洞问题 Firefox 49发布时间将延期一周
查看>>
WLAN产品形态之分层架构
查看>>
Chrome 隐藏 SSL 证书信息 禁止禁用 DRM
查看>>
《Windows Server 2012 Hyper-V虚拟化管理实践》——3.2 Hyper-V主机日常管理
查看>>
《C语言编程魔法书:基于C11标准》——第一篇 预备知识篇 第1章 C魔法概览1.1 例说编程语言...
查看>>
《IPv6安全》——1.7 推荐读物和资料
查看>>
《实施Cisco统一通信管理器(CIPT2)》一1.2 概述部署多站点环境时将会遇到的挑战...
查看>>
《CCNP安全Secure 642-637认证考试指南》——第8章 配置与实施路由式数据面安全...
查看>>
AngularJS 的自定义指令
查看>>
《CCNA ICND2(200-101)认证考试指南(第4版)》——第1章定义生成树协议
查看>>
什么样的 RPC 才是好用的 RPC
查看>>
《Adobe Premiere Pro CC经典教程》——14.6 特殊颜色效果
查看>>
Debian 项目不再提供 CD 格式的 ISO 镜像
查看>>
《设计团队协作权威指南》—第1章1.3节甘为螺丝钉
查看>>
android 屏幕保持唤醒 不锁屏 android.permission.WAKE_LOCK
查看>>
《Unity 3D 游戏开发技术详解与典型案例》——1.3节第一个Unity 3D程序
查看>>
Airbnb数据科学团队进化论:如何由内而外实现数据驱动
查看>>
如何用机器学习预测超售,避免美联航“暴力赶客”悲剧
查看>>
css细节(实习第1天)
查看>>