POJ 3414 Pots
PotsTime Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7438 Accepted: 3120 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
[Submit] [Go Back] [Status] [Discuss]
考察点:bfs 理清思路做就可以了
[cpp]
#include <stdio.h>
#include <string.h>
struct queue
{
int x,y;
int pre;
}a[1000000];
int status[150][150],sum[150][150],ope[150][150],A,B,C;
int res[100000];
int main()
{
void bfs();
int i,j,n,m,s,t;
memset(status,0,sizeof(status));
memset(sum,0,sizeof(sum));
scanf("%d %d %d",&A,&B,&C);
bfs();
return 0;
}
void bfs()
{
int base,top,i,j,x,y,xend,yend,key,n;
key=0;
base=top=0;
a[top].x=0; a[top++].y=0;
status[0][0]=1;
while(base<top)
{
x=a[base].x; y=a[base++].y;
if(x==C||y==C)
{
key=1;
break;
}
for(i=0;i<=5;i++)
{
if(i==0)
{
xend=A; yend=y;
}else if(i==1)
{
xend=0; yend=y;
}else if(i==2)
{
if(A-x==0)
{
continue;
}
if((A-x)>=y)
{
xend=x+y; yend=0;
}else
{
xend=A; yend=y-(A-x);
}
}else if(i==3)
{
yend=B; xend=x;
}else if(i==4)
{
yend=0; xend=x;
}else if(i==5)
{
if((B-y)==0)
{
continue;
}
if((B-y)>=x)
{
yend= y+x; xend=0;
}else
{
yend=B; xend=x-(B-y);
}
}
if(!status[xend][yend]&&xend>=0&&xend<=A&¥d>=0&¥d<=B)
{
status[xend][yend]=1;
sum[xend][yend]=sum[x][y]+1;
a[top].x=xend; a[top].y=yend;
a[top++].pre=base-1;
ope[xend][yend]=i;
}
}
}
if(!key)
{
printf("impossible\n");
}else
{
printf("%d\n",sum[x][y]);
base--; &nbs
补充:软件开发 , C++ ,
- 更多C/C++疑问解答:
- 关于c++的cout输出的问题。
- 在学校里学过C和C++,不过学的很一般,现在自学C#,会不会很难?
- 全国计算机二级C语言笔试题
- 已知某树有2个2度结点,3个3度结点,4个4度结点,问有几个叶子结点?
- c++数据结构内部排序问题,整数排序
- 2012九月计算机二级C语言全国题库,,急求急求
- 如果assert只有一个字符串作为参数,是什么意思呢?
- C语言中,哪些运算符具有左结合性,哪些具有右结合性,帮忙总结下,谢谢了!
- 为什么用结构体编写的程序输入是,0输不出来啊~~~
- 将IEEE—754的十六进制转化为十进制浮点类型,用C或C++都行,多谢各位大侠啊,非常感谢!
- 为什么这个程序求不出公式?
- 这个链表倒置的算法请大家分析下
- c语言函数库调用
- C语言unsigned int纠错
- C语言快排求解啊