POJ
来源:互联网 发布:淘宝 充话费 退款 编辑:程序博客网 时间:2024/05/21 14:02
Pots
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 jis 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 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’.
3 5 4
6FILL(2)POUR(2,1)DROP(1)POUR(2,1)FILL(2)POUR(2,1)
题意:有两个水杯,体积为a,b,需要称量出c体积,输出需要几步,和每步做法,FILL(1)把a杯装满
FILL(2)把b杯装满
DROP(1)把a杯清空
DROP(2)把b杯清空;
POUR(1,2)把a杯的水到如b杯中
POUR(2,1)把b杯的水到如a杯中;
数据较简单,广搜和神搜都可以。
深搜:
#include<stdio.h>#include<string.h>int s[1000],book[110][110],s1[1000];int a,b,c,flag,k;void dfs(int x,int y,int step){if(step>=k)return ;if(x==c||y==c){k=step;for(int i=0;i<k;i++)s1[i]=s[i];flag=1;return ;}int tx,ty;for(int i=1;i<=6;i++){if(i==1)tx=a,ty=y; else if(i==2) tx=x,ty=b; else if(i==3) tx=0,ty=y; else if(i==4) tx=x,ty=0; else if(i==5) { if(x+y<=b)ty=x+y,tx=0;else ty=b,tx=x+y-b;}else if(i==6){if(x+y<=a)tx=x+y,ty=0;else tx=a,ty=x+y-a;}if(!book[tx][ty]){book[tx][ty]=1;s[step]=i;dfs(tx,ty,step+1);book[tx][ty]=0;}}return ;}int main(){while(~scanf("%d %d %d",&a,&b,&c)){memset(book,0,sizeof(book));book[0][0]=1;k=99999;flag=0;dfs(0,0,0); if(flag) {printf("%d\n",k);for(int i=0;i<k;i++){if(s1[i]==1)printf("FILL(1)\n");else if(s1[i]==2)printf("FILL(2)\n");else if(s1[i]==3)printf("DROP(1)\n");else if(s1[i]==4)printf("DROP(2)\n");else if(s1[i]==5)printf("POUR(1,2)\n");else if(s1[i]==6)printf("POUR(2,1)\n");}}else printf("impossible\n");}return 0;}
广搜:
#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>using namespace std;int n,m,t,book[110][110];int flag;struct note{int x;int y;int step;int s[1011];};void bfs(){queue <note> Q ;note p,q;int tx,ty; p.x=0; p.y=0;p.step=0;book[p.x][p.y]=1;Q.push(p);while(!Q.empty()){p=Q.front();q=p;Q.pop();if(p.x==t||p.y==t){printf("%d\n",p.step);for(int i=1;i<=p.step;i++){if(p.s[i]==1)printf("FILL(1)\n");else if(p.s[i]==2)printf("FILL(2)\n");else if(p.s[i]==3)printf("DROP(1)\n");else if(p.s[i]==4)printf("DROP(2)\n");else if(p.s[i]==5)printf("POUR(1,2)\n");else if(p.s[i]==6)printf("POUR(2,1)\n");}flag=1;break;}for(int i=1;i<=6;i++){if(i==1)tx=n,ty=p.y;else if(i==2)tx=p.x,ty=m;else if(i==3)tx=0,ty=p.y;else if(i==4)tx=p.x,ty=0;else if(i==5){if(p.x+p.y<=m)ty=p.x+p.y,tx=0;else ty=m,tx=p.x+p.y-m;}else if(i==6){if(p.x+p.y<=n)tx=p.x+p.y,ty=0;else tx=n,ty=p.x+p.y-n;}if(book[tx][ty])continue;book[tx][ty]=1;q.x=tx;q.y=ty;q.step=p.step+1;q.s[q.step]=i; Q.push(q);}}return ;}int main(){while(~scanf("%d %d %d",&n,&m,&t)){memset(book,0,sizeof(book));flag=0; bfs(); if(!flag) printf("impossible\n");}return 0;}
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- 数组指针和指针数组的区别
- 新闻客户端——SmartImageView——AsyncHttpClient
- Intent和Intent过滤器
- JavaWeb会话管理之用户上次访问时间
- 搜索算法总结
- POJ
- 离散题目10
- 新人作者快速入门的最好方法都在这了
- JQuery实现导航菜单栏随滚动条上下滚动
- B
- java基础--for循环嵌套打印输出直角三角形
- Django_分页设计和Ueditor配置、图文发布
- 在QT中使用MySQL数据库
- 51Nod 1050 循环数组最大子段和