原题:

Pots

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 13227 Accepted: 5550 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)

Source

Northeastern Europe 2002, Western Subregion

题意:给了你两个瓶子 (姑且认为它们分别为M和N吧)

让你做三种操作:

1.把M瓶(N瓶)装满;

2.把M瓶(N瓶)中的水倒掉;

3.把M(N)瓶里的水倒进N(M)瓶中去。(如果会溢出,那么倒到正好装满为止,剩下的水还在瓶子里);

问 经过哪些操作能使瓶中(不管是M瓶还是N瓶)剩余的水量为C,并输出操作步骤。(难点)

如果不能 输出impossible。(被impossible坑害一次)。。。

解题思路 :BFS,大致分为6个入口 两个瓶子互相倒水的那步其实可以分为溢出和不溢出两种情况。 一共就算8个入口吧。 这个搜索极其容易把下标搞错!!注意注意千万别错,否则死都不知道自己怎么死的。。。

难的是记录步骤 我选用的是开仨数组。 reca记录在进行那一步之前M瓶的状态,recb记录在进行那一步之前N瓶的状态,rec_sta记录进行的是哪一步操作。搜出来结果以后往回推,顺便记录进行了几步和每一步分别是什么。逆序输出答案。

直接扔代码啦(这个是没有去掉注释的)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int m,n,k,reca[222][222],recb[222][222],rec_sta[222][222],tempa,tempb;
int rec_a[66666],rec_b[66666],tot=0;
bool vis[222][222];
queue<int> a,b;
bool bfs()
{
while(!a.empty())
{
tempa=a.front(),tempb=b.front();
a.pop();b.pop();
// printf("tempa=%d tempb=%d reca[tempa][tempb]=%d recb[tempa][tempb]=%d\n",tempa,tempb,reca[tempa][tempb],recb[tempa][tempb]);
if(tempa==k||tempb==k)
return 1;
if(!vis[tempa][0])//DROP(2)
{
a.push(tempa);b.push(0);vis[tempa][0]=1;
reca[tempa][0]=tempa;recb[tempa][0]=tempb;rec_sta[tempa][0]=1;//printf("1 %d 0\n",tempa);
}
if(!vis[0][tempb])//DROP(1)
{
a.push(0);b.push(tempb);vis[0][tempb]=1;
reca[0][tempb]=tempa;recb[0][tempb]=tempb;rec_sta[0][tempb]=2;//printf("2 0 %d \n",tempb);
}
if(!vis[m][tempb])//FILL(1)
{
a.push(m);b.push(tempb);vis[m][tempb]=1;
reca[m][tempb]=tempa;recb[m][tempb]=tempb;rec_sta[m][tempb]=3;//printf("3 %d %d\n",m,tempb);
}
if(!vis[tempa][n])//FILL(2)
{
a.push(tempa);b.push(n);vis[tempa][n]=1;
reca[tempa][n]=tempa;recb[tempa][n]=tempb;rec_sta[tempa][n]=4;// printf("4 %d %d\n",tempa,n);
}
int t=tempa+tempb;
if(t<=m&&!vis[t][0])//POUR(2,1)
{
a.push(t);b.push(0);vis[t][0]=1;
reca[t][0]=tempa;recb[t][0]=tempb;rec_sta[t][0]=5; //printf("5 %d 0\n",t);
}
if(t>m&&!vis[m][t-m])//POUR(2,1)
{
a.push(m);b.push(t-m);vis[m][t-m]=1;
reca[m][t-m]=tempa;recb[m][t-m]=tempb;rec_sta[m][t-m]=5;//printf("5 %d %d\n",k,t-k);
}
if(t<=n&&!vis[0][t])//POUR(1,2)
{
a.push(0);b.push(t);vis[0][t]=1;
reca[0][t]=tempa;recb[0][t]=tempb;rec_sta[0][t]=6;//printf("6 %d %d\n",0,t);
}
if(t>n&&!vis[t-n][n])//POUR(1,2)
{
a.push(t-n);b.push(n);vis[t-n][n]=1;
reca[t-n][n]=tempa;recb[t-n][n]=tempb;rec_sta[t-n][n]=6;//printf("6 %d %d\n",t-k,k);
}
}
return 0;
}
int main()
{
scanf("%d%d%d",&m,&n,&k);
a.push(0);b.push(0);vis[0][0]=true;
if(!bfs())
printf("impossible\n");
else
{
/*for(int i=0;i<6;i++)
{
printf("\n");
for(int j=0;j<6;j++)
{
printf("%d",vis[i][j]);
}
}*/
// printf("tempa=%d tempb=%d\n",tempa,tempb);
rec_a[tot]=tempa;rec_b[tot]=tempb;
while(tempa||tempb)
{
tot++;
// printf("%d %d %d %d\n",tempa,tempb,reca[tempa][tempb],recb[tempa][tempb]);
int t=reca[tempa][tempb];
rec_a[tot]=t;
tempb=recb[tempa][tempb];
tempa=t;
rec_b[tot]=tempb;
}
printf("%d\n",tot);
tot++;
while(tot--)
{
// printf("%d %d %d\n",rec_a[tot],rec_b[tot],rec_sta[rec_a[tot]][rec_b[tot]]);
if(rec_sta[rec_a[tot]][rec_b[tot]]==1)printf("DROP(2)\n");
else if(rec_sta[rec_a[tot]][rec_b[tot]]==2)printf("DROP(1)\n");
else if(rec_sta[rec_a[tot]][rec_b[tot]]==3)printf("FILL(1)\n");
else if(rec_sta[rec_a[tot]][rec_b[tot]]==4)printf("FILL(2)\n");
else if(rec_sta[rec_a[tot]][rec_b[tot]]==5)printf("POUR(2,1)\n");
else if(rec_sta[rec_a[tot]][rec_b[tot]]==6)printf("POUR(1,2)\n");
}
}
}

去掉注释的:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int m,n,k,reca[222][222],recb[222][222],rec_sta[222][222],tempa,tempb;
int rec_a[66666],rec_b[66666],tot=0;
bool vis[222][222];
queue<int> a,b;
bool bfs()
{
while(!a.empty())
{
tempa=a.front(),tempb=b.front();
a.pop();b.pop();
if(tempa==k||tempb==k)
return 1;
if(!vis[tempa][0])
{
a.push(tempa);b.push(0);vis[tempa][0]=1;
reca[tempa][0]=tempa;recb[tempa][0]=tempb;rec_sta[tempa][0]=1;
}
if(!vis[0][tempb])
{
a.push(0);b.push(tempb);vis[0][tempb]=1;
reca[0][tempb]=tempa;recb[0][tempb]=tempb;rec_sta[0][tempb]=2;
}
if(!vis[m][tempb])
{
a.push(m);b.push(tempb);vis[m][tempb]=1;
reca[m][tempb]=tempa;recb[m][tempb]=tempb;rec_sta[m][tempb]=3;
}
if(!vis[tempa][n])
{
a.push(tempa);b.push(n);vis[tempa][n]=1;
reca[tempa][n]=tempa;recb[tempa][n]=tempb;rec_sta[tempa][n]=4;
}
int t=tempa+tempb;
if(t<=m&&!vis[t][0])
{
a.push(t);b.push(0);vis[t][0]=1;
reca[t][0]=tempa;recb[t][0]=tempb;rec_sta[t][0]=5;
}
if(t>m&&!vis[m][t-m])
{
a.push(m);b.push(t-m);vis[m][t-m]=1;
reca[m][t-m]=tempa;recb[m][t-m]=tempb;rec_sta[m][t-m]=5;
}
if(t<=n&&!vis[0][t])
{
a.push(0);b.push(t);vis[0][t]=1;
reca[0][t]=tempa;recb[0][t]=tempb;rec_sta[0][t]=6;
}
if(t>n&&!vis[t-n][n])
{
a.push(t-n);b.push(n);vis[t-n][n]=1;
reca[t-n][n]=tempa;recb[t-n][n]=tempb;rec_sta[t-n][n]=6;
}
}
return 0;
}
int main()
{
scanf("%d%d%d",&m,&n,&k);
a.push(0);b.push(0);vis[0][0]=true;
if(!bfs())
printf("impossible\n");
else
{
rec_a[tot]=tempa;rec_b[tot]=tempb;
while(tempa||tempb)
{
tot++;
int t=reca[tempa][tempb];
rec_a[tot]=t;
tempb=recb[tempa][tempb];
tempa=t;
rec_b[tot]=tempb;
}
printf("%d\n",tot);
tot++;
while(tot--)
{
if(rec_sta[rec_a[tot]][rec_b[tot]]==1)printf("DROP(2)\n");
else if(rec_sta[rec_a[tot]][rec_b[tot]]==2)printf("DROP(1)\n");
else if(rec_sta[rec_a[tot]][rec_b[tot]]==3)printf("FILL(1)\n");
else if(rec_sta[rec_a[tot]][rec_b[tot]]==4)printf("FILL(2)\n");
else if(rec_sta[rec_a[tot]][rec_b[tot]]==5)printf("POUR(2,1)\n");
else if(rec_sta[rec_a[tot]][rec_b[tot]]==6)printf("POUR(1,2)\n");
}
}
}

POJ 3414 解题报告!的更多相关文章

  1. POJ 1001 解题报告 高精度大整数乘法模版

    题目是POJ1001 Exponentiation  虽然是小数的幂 最终还是转化为大整数的乘法 这道题要考虑的边界情况比较多 做这道题的时候,我分析了 网上的两个解题报告,发现都有错误,说明OJ对于 ...

  2. poj分类解题报告索引

    图论 图论解题报告索引 DFS poj1321 - 棋盘问题 poj1416 - Shredding Company poj2676 - Sudoku poj2488 - A Knight's Jou ...

  3. POJ 1003 解题报告

    1.问题描述: http://poj.org/problem?id=1003 2.解题思路: 最直观的的想法是看能不能够直接求出一个通项式,然后直接算就好了, 但是这样好水的样子,而且也不知道这个通项 ...

  4. POJ 1004 解题报告

    1.题目描述: http://poj.org/problem?id=1004 2.解题过程 这个题目咋一看很简单,虽然最终要解出来的确也不难,但是还是稍微有些小把戏在里面,其中最大的把戏就是float ...

  5. POJ 1005 解题报告

    1.题目描述   2.解题思路 好吧,这是个水题,我的目的暂时是把poj第一页刷之,所以水题也写写吧,这个题简单数学常识而已,给定坐标(x,y),易知当圆心为(0,0)时,半圆面积为0.5*PI*(x ...

  6. POJ 2411 解题报告

    传送门:http://poj.org/problem?id=2411 题目简述 有一个\(W\)行\(H\)列的广场,需要用\(1*2\)小砖铺满,小砖之间互相不能重叠,问 有多少种不同的铺法? 输入 ...

  7. 广大暑假训练1 E题 Paid Roads(poj 3411) 解题报告

    题目链接:http://poj.org/problem?id=3411 题目意思:N个city 由 m 条路连接,对于一条路(假设连接Cityia和 Cityb),如果从Citya 去 Cityb的途 ...

  8. POJ旅行商问题——解题报告

    旅行商问题 总时间限制: 1000ms 内存限制: 65536kB 描述 某国家有n(1<=n<=10)座城市,给定任意两座城市间距离(不超过1000的非负整数).一个旅行商人希望访问每座 ...

  9. POJ 2182 解题报告

    Lost Cows Time Limit: 1000 MS Memory Limit: 65536 KB Description N (2 <= N <= 8,000) cows have ...

随机推荐

  1. 计算机网络(9)-----TCP可靠传输的实现

    TCP可靠传输的实现 以字节为单位的滑动窗口 滑动窗口的滑动是以字节为单位的,发送方A和接收方B在TCP三次握手的前两次握手时协商好了发送窗口和接受窗口的大小,发送方A根据B发送来的确认连接报文中标明 ...

  2. 机器学习相关的Awesome系列

    Index Awesome 备注 1 Awesome Machine Learning 机器学习资源大全中文版 2 Awesome Artificial Intelligence 人工智能 3 Awe ...

  3. HPL/SQL与CDH5.4.7集成

    1.下载hplsql-0.3.13到本地并解压 2.修改plsql,为如下内容 #!/bin/bash export "HADOOP_CLASSPATH=/opt/cloudera/parc ...

  4. extjs combobox

    states.js中 Ext.example.states=[ ['AL','ALabama','The Heart of Dixie'], ['AK','Alaska','The Land of t ...

  5. 《SharePoint 2013 应用开发实战》目录

    博客地址:http://blog.csdn.net/FoxDave 第 1 章  1 ◄SharePoint概述►        1 1.1  SharePoint的发展历程 1 1.1.1  Sha ...

  6. 《C++primer》v5 第7章 类 读书笔记 习题答案

    7.1.7.2.7.3 #include<iostream> #include<cstdio> #include<vector> #include<strin ...

  7. Django实现表单验证、CSRF、cookie和session、缓存、数据库多表操作(双下划綫)

    通常验证用户输入是否合法的话,是前端js和后端共同验证的,这是因为前端js是可以被禁用的,假如被禁用了,那就没法用js实现验证合法与否了,也就是即使用户输入的不合法,但是也没提示,用户也不知道怎么输入 ...

  8. [转]使用Scrapy建立一个网站抓取器

    英文原文:Build a Website Crawler based upon Scrapy 标签: Scrapy Python 209人收藏此文章, 我要收藏renwofei423 推荐于 11个月 ...

  9. oracle xmltype导入并解析Excel数据 (四)特别说明

    1.Excel导出,此处没有给出 2.错误原因在中间表,T_EXCEL_IMPORT_GENERATION,其中errormsg不为空的数据 3,中间表入库过程: 需要自己实现,为一个存储过程,存储过 ...

  10. 【一套C语言控制台的输出框代码】

    效果演示 可以生成一个输出框 首先 要创建输出框,设置输出框风格,可以设置的元素包括: 左上角符号,右上角符号,左下角符号,右下角符号,以及上下左右边界线符号,理论上,只要你电脑能显示的符号,都可以支 ...