poj 3414(搜索)
来源:互联网 发布:excel数据列表创建列表 编辑:程序博客网 时间:2024/06/06 09:34
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 potj is full (and there may be some water left in the pot i), or the poti 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 exactlyC liters of water in one of the pots.
Input
On the first and only line are the numbers A, B, andC. 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 operationsK. 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的pot,按照以下三种操作方式,求出能否在一定步数后,使者两个pot的其中一个的水量为c。
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>using namespace std;struct node{ int va,vb,step; char str[151][151];};int a,b,c,vis[151][151];int bfs(){ node q[151],s,t; int i,j; s.va=0; s.vb=0; s.step=0; int r,f; vis[0][0]=1; r=f=0; q[r++]=s; while(f<r) { s=q[f++]; if(s.va==c||s.vb==c) { printf("%d\n",s.step); for(int i=0; i<s.step; i++) { printf("%s\n",s.str[i]); } return 1; } //装满a if(s.va==0) { t=s; t.va=a; strcpy(t.str[t.step++],"FILL(1)"); if(!vis[t.va][t.vb]) { vis[t.va][t.vb]=1; q[r++]=t; } } else if(s.va<=a) { t=s; t.va=0; strcpy(t.str[t.step++],"DROP(1)"); if(!vis[t.va][t.vb]) { vis[t.va][t.vb]=1; q[r++]=t; } if(s.vb<b) { t=s; if(t.vb+t.va<=b) { t.vb+=t.va; t.va=0; } else { t.va=(t.vb+t.va)-b; t.vb=b; } strcpy(t.str[t.step++],"POUR(1,2)"); if(!vis[t.va][t.vb]) { vis[t.va][t.vb]=1; q[r++]=t; } } } //装满b if(s.vb==0) { t=s; t.vb=b; strcpy(t.str[t.step++],"FILL(2)"); if(!vis[t.va][t.vb]) { vis[t.va][t.vb]=1; q[r++]=t; } } //把b中的水倒出来 else if(s.vb<=b) { t=s; t.vb=0; strcpy(t.str[t.step++],"DROP(2)"); if(!vis[t.va][t.vb]) { vis[t.va][t.vb]=1; q[r++]=t; } if(s.va<a) { t=s; if(t.vb+t.va<=a) { t.va+=t.vb; t.vb=0; } else { t.vb=(t.vb+t.va)-a; t.va=a; } strcpy(t.str[t.step++],"POUR(2,1)"); if(!vis[t.va][t.vb]) { vis[t.va][t.vb]=1; q[r++]=t; } } } } return 0;}int main(){ while(~scanf("%d%d%d",&a,&b,&c)) { memset(vis,0,sizeof(vis)); int f=bfs(); if(!f) printf("impossible\n"); } return 0;}
- poj 3414(搜索)
- POJ 3414 Pots (BFS搜索题)
- POJ 3414 Pots 解题报告(搜索)
- poj 3414( 搜索 )
- poj 3414 搜索BFS
- poj 3414 搜索
- poj 3414 Pots 搜索
- POJ 3411(搜索)
- poj-1321(搜索)
- poj Hopscotch (搜索)
- poj 搜索题目(转)
- POJ题目分类(搜索)
- POJ 2531(搜索题)
- POJ搜索专题(转转)
- POJ-滑雪-(java,搜索)
- POJ 1033 Defragment(搜索)
- POJ 2286 搜索 (IDA*)
- POJ 1084 搜索(IDA*)
- 【每日一记】设计模式——装饰器模式
- java多态
- 注解@AutoWired和@Resource的区别
- Javascript高级程序设计——15-1.匿名函数和闭包
- 大数据Spark “蘑菇云”行动第95课:Hive安全解析和实战等
- poj 3414(搜索)
- POJ 2739 E - Sum of Consecutive Prime Numbers 素数打表+尺取法
- Emirp
- 如何在Win10上优雅地安装Scrapy
- BIM执行之道: BIM落地应用的准则是什么?
- 应用集成实战系列:服务总线中的同步业务交互模式
- STM32学习笔记2——再探GPIO
- cifar数据集介绍及到图像转换的实现
- bzoj 2286 [Sdoi2011]消耗战 虚树