SDUTOJ【2780】 pots---bfs
来源:互联网 发布:mac怎么循环播放视频 编辑:程序博客网 时间:2024/06/06 23:17
Description
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
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. 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
Hint
FILL(2)
POUR(2,1)
code:
#include<stdio.h>#include<string.h>struct node{ int a,b; int cs;}stack[100005],qq;int vt[105][105],a,b,c;void bfs(){ int v=0,u=0,i,j,k,s,e,z,x; stack[v].a=0; stack[v].b=0; stack[v].cs=0; v++; while(u<v) { qq=stack[u]; if(qq.a==c || qq.b==c) { printf("%d\n",stack[u].cs); return ; } {//把b倒入a if(qq.b>=a-qq.a) { x=qq.b-a+qq.a; z=a; } else { x=0; z=qq.a+qq.b; } if(vt[z][x]==0 && z>=0 && z<=a && x>=0 && x<=b) { stack[v].a=z; stack[v].b=x; stack[v].cs=stack[u].cs+1; v++; vt[z][x]=1; } } {//把b倒满 z=qq.a; x=b; if(vt[z][x]==0 && z>=0 && z<=a && x>=0 && x<=b) { stack[v].a=z; stack[v].b=x; stack[v].cs=stack[u].cs+1; v++; vt[z][x]=1; } } {//把b倒掉 z=qq.a; x=0; if(vt[z][x]==0 && z>=0 && z<=a && x>=0 && x<=b) { stack[v].a=z; stack[v].b=x; stack[v].cs=stack[u].cs+1; v++; vt[z][x]=1; } } {//把a倒入b if(qq.a>=b-qq.b) { z=qq.a-b+qq.b; x=b; } else { z=0; x=qq.a+qq.b; } if(vt[z][x]==0 && z>=0 && z<=a && x>=0 && x<=b) { stack[v].a=z; stack[v].b=x; stack[v].cs=stack[u].cs+1; v++; vt[z][x]=1; } } {//把a倒满 z=a; x=qq.b; if(vt[z][x]==0 && z>=0 && z<=a && x>=0 && x<=b) { stack[v].a=z; stack[v].b=x; stack[v].cs=stack[u].cs+1; v++; vt[z][x]=1; } } {//把a倒掉 z=0; x=qq.b; if(vt[z][x]==0 && z>=0 && z<=a && x>=0 && x<=b) { stack[v].a=z; stack[v].b=x; stack[v].cs=stack[u].cs+1; v++; vt[z][x]=1; } } //printf("%d \n",u); u++; }printf("impossible\n");}int main(){ int n,m,i,j,k; while(scanf("%d%d%d",&a,&b,&c)!=EOF) { memset(vt,0,sizeof(vt)); vt[0][0]=1; bfs(); } return 0;}
0 0
- SDUTOJ【2780】 pots---bfs
- SDUTOJ 2780 ——Pots BFS
- SDUT 2780 Pots BFS
- SDUT-2780 Pots(BFS)
- Pots(bfs)
- (BFS)Pots
- Pots-BFS
- Pots(BFS)
- Pots --bfs
- POJ 3414 Pots BFS
- (BFS)3414 Pots
- POJ 3414 Pots(BFS)
- POJ 1324 Pots(BFS)
- poj 3414 Pots bfs
- pots(BFS)
- Pots——BFS
- Pots(BFS)
- poj 3414 Pots BFS
- mysql压力测试
- 自考《信息系统开发与管理》之泛读概总
- BZOJ3670 [Noi2014]动物园
- mysql 配置向导
- Java Proxy学习总结
- SDUTOJ【2780】 pots---bfs
- spring技术内幕17-Spring HTTP调用器实现远程调用
- FFmpeg转码指令(测试通过)
- HDU 4891 The Great Pan(模拟)
- HDU[1253]--bfs
- BZOJ3671 [Noi2014]随机数生成器
- hdu1010 dfs
- CF 17B Hierarchy
- c++驱动编译出错解决方法