Pots

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描写叙述

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.

输入

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

输出

 The first line of the output must contain the length of the sequence of operations K.  If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

演示样例输入

3 5 4

演示样例输出

6

提示

FILL(2)

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

简单搜索题,对于两个空瓶子,容积分别为A B 有6种操作 把A(或B)清空,把A(或B)装满,把A倒入B,把B倒入A 。相应这6种操作,有6种状态。典型的bfs搜索。不多了,仅仅是这题明明说的是单组输入结果答案却要多组输入才对。白白贡献5个WA。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std;
int m,n,c;
typedef struct node
{
int v1,v2,op;
};
bool vis[999][999];
void bfs()
{
node t={0,0,0};
queue <node> Q;
Q.push(t);
vis[0][0]=1;
while(!Q.empty())
{
node f=Q.front();Q.pop();
if(f.v1==c||f.v2==c)
{
cout<<f.op<<endl;
return ;
}
if(f.v1!=m)
{
t.v1=m;
t.op=f.op+1;
t.v2=f.v2;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
Q.push(t);
}
}
if(f.v2!=n)
{
t.v2=n;
t.op=f.op+1;
t.v1=f.v1;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
Q.push(t);
}
}
if(f.v1!=0)
{
t.v1=0;
t.v2=f.v2;
t.op=f.op+1;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
Q.push(t);
}
}
if(f.v2!=0)
{
t.v2=0;
t.v1=f.v1;
t.op=f.op+1;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
Q.push(t);
}
}
if(f.v2!=0&&f.v1!=m)
{
t.v2=f.v2-(m-f.v1);if(t.v2<0) t.v2=0;
t.v1=f.v1+f.v2; if(t.v1>m) t.v1=m;
t.op=f.op+1;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
Q.push(t);
}
}
if(f.v1!=0&&f.v2!=n)
{
t.v1=f.v1-(n-f.v2);if(t.v1<0) t.v1=0;
t.v2=f.v2+f.v1; if(t.v2>n) t.v2=n;
t.op=f.op+1;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
Q.push(t);
}
}
}
puts("impossible");
}
int main()
{ while(cin>>m>>n>>c)
{
memset(vis,0,sizeof(vis));
bfs();
}
return 0;
}

最新文章

  1. 深入理解Java:内部类
  2. 【转】Android Studio系列教程六--Gradle多渠道打包
  3. IIS 7.5 发布Web 网站步骤
  4. (python)对象的引用
  5. 0506 Scrum 项目1.0
  6. iOS Framework lipo报错 lipo: can&#39;t map input file
  7. CSS属性--过渡(transtion)
  8. Java GC 日志输出分析
  9. js动态给table添加行(tr)
  10. 为何与0xff进行与运算
  11. HDOJ----------1009
  12. GridView网格线都设置
  13. C#在客户端验证数字证书(Certificate)
  14. 1. 初次尝试Core Data 应用程序(Core Data 应用开发实践指南)
  15. 【Android Developers Training】 51. 序言:打印内容
  16. Visual Studio Code快速删除空行及几个常用快捷键总结
  17. Project facet Java version 1.8 not supported
  18. Restrictions查询用法
  19. Ubuntu 14.04 配置 LAMP+phpMyAdmin PHP开发环境!
  20. SQLServer 2014 AlwaysOn

热门文章

  1. 05-数据类型转换(bool类型)
  2. 云服务器 ECS Linux 系统下使用 dig 命令查询域名解析
  3. [Poi] Customize Babel to Build a React App with Poi
  4. UVA 10515 - Powers Et Al.(数论)
  5. 技术报告:APT组织Wekby利用DNS请求作为C&amp;C设施,攻击美国秘密机构
  6. BZOJ2668: [cqoi2012]交换棋子(费用流)
  7. 如何更新 CentOS 镜像源
  8. spring的事务如何配置
  9. WP8 学习笔记(002_应用程序结构)
  10. DuiVision开发教程(19)-菜单