POJ 3414 Pots【广搜】
Description
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
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 4Sample Output
6 FILL(2) POUR(2,1) DROP(1) POUR(2,1) FILL(2) POUR(2,1) code: View Code #include<stdio.h>#include<string.h>
#define clr(x)memset(x,0,sizeof(x))
int v[1001][1001];
int va;
int vb;
struct node
{
int x,y,xu,step;
}q[100000];
int pre[100000];
//1 full 1 2 full 2 3 drop 1 4 drop 2 5 from a to b 6 from b to a
void print(int x)
{
if(pre[x]!=0)
print(pre[x]);
//printf("%d\n",q[x].xu);
if(q[x].xu==1)
printf("FILL(1)\n");
else if(q[x].xu==2)
printf("FILL(2)\n");
else if(q[x].xu==3)
printf("DROP(1)\n");
else if(q[x].xu==4)
printf("DROP(2)\n");
else if(q[x].xu==5)
printf("POUR(1,2)\n");
else printf("POUR(2,1)\n");
}
int main()
{
int a,b,c,i,j,front,rear,tmp,v1,v2,res,ans;
while(scanf("%d%d%d",&a,&b,&c)!=EOF)
{
va=vb=0;
front=rear=0;
q[rear].xu=0; q[rear].step=0; q[rear].x=0; q[rear++].y=0;
res=-1;
v[0][0]=1;
memset(pre,0,sizeof(pre));
while(front<=rear)
{
va=q[front].x; vb=q[front].y;
//printf("%d %d\n",va,vb);
if(va==c||vb==c) { res=q[front].step;ans=front; break;}
if(!v[a][vb]) { v[a][vb]=1; q[rear].x=a; q[rear].y=vb; q[rear].xu=1; pre[rear]=front; q[rear++].step=q[front].step+1; }
if(!v[va][b]) { v[va][b]=1; q[rear].x=va; q[rear].y=b; q[rear].xu=2; pre[rear]=front; q[rear++].step=q[front].step+1; }
if(!v[0][vb]) { v[0][vb]=1; q[rear].x=0; q[rear].y=vb; q[rear].xu=3; pre[rear]=front; q[rear++].step=q[front].step+1; }
if(!v[va][0]) { v[va][0]=1; q[rear].x=va; q[rear].y=0; q[rear].xu=4; pre[rear]=front; q[rear++].step=q[front].step+1; }
if((va+vb)/b<1){
if(!v[0][va+vb]){ v[0][va+vb]=1; q[rear].x=0; q[rear].y=va+vb; q[rear].xu=5; pre[rear]=front; q[rear++].step=q[front].step+1; }
}
else if(!v[va-(b-vb)][b]) { v[va-(b-vb)][b]=1; q[rear].x=va-(b-vb); q[rear].y=b; q[rear].xu=5; pre[rear]=front; q[rear++].step=q[front].step+1; }
if((va+vb)/a<1){
if(!v[va+vb][0]){ v[va+vb][0]=1; q[rear].x=va+vb; q[rear].y=0; q[rear].xu= 6; pre[rear]=front; q[rear++].step=q[front].step+1; }
}
else if(!v[a][vb-(a-va)]) { v[a][vb-(a-va)]=1; q[rear].x=a; q[rear].y=vb-(a-va); q[rear].xu=6; pre[rear]=front; q[rear++].step=q[front].step+1; }
front++;
}
if(res==-1)printf("impossible\n");
else {
printf("%d\n",res);
print(ans);
}
}
return 0;
}
轉(zhuǎn)載于:https://www.cnblogs.com/dream-wind/archive/2012/03/22/2412181.html
總結(jié)
以上是生活随笔為你收集整理的POJ 3414 Pots【广搜】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: T-SQL自定义函数返回前一天或后一天日
 - 下一篇: 阴囊潮湿,尿液老是有浑浊物,是什么情况?