POJ -3414-Pots
2015-08-19 14:34
330 查看
题意:
给你,两个容量分别为a,b的杯子,让你看需要几步可以得到c的水,不能的话,impossible,可以的话,输出步数,和路径
思路:
BFS,暴搜
给你,两个容量分别为a,b的杯子,让你看需要几步可以得到c的水,不能的话,impossible,可以的话,输出步数,和路径
思路:
BFS,暴搜
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<iostream> #include<algorithm> #include<queue> using namespace std; int a, b, c; struct node { int step; int k; int n, m; int qb; } ls[1010],now, next; int vis[101][101]; int ki[1010]; void bfs() { memset(vis,0,sizeof(vis)); queue<node> que; now.step = 0; now.k = 0; now.n = 0; now.m = 0; que.push(now); vis[0][0] = 1; int i; int ti = 0; while(!que.empty()) { now = que.front(); que.pop(); if(now.n == c || now.m == c) { printf("%d\n",now.step); int ko = now.step; i = 1; int c = now.qb; ki[0] = now.k; while(1) { ki[i++] = ls[c].k; if(i > ko) break; c = ls[c].qb; } for(i = ko-1; i >= 0; i--) { switch(ki[i]) { case 5: printf("FILL(1)\n");break; case 6: printf("FILL(2)\n");break; case 3: printf("POUR(1,2)\n");break; case 4: printf("POUR(2,1)\n");break; case 1: printf("DROP(1)\n");break; case 2: printf("DROP(2)\n");break; } } return ; } ls[++ti] = now; for(i = 1; i <= 6; i++) { if(i == 1) { next.n = 0; next.m = now.m; next.step = now.step+1; next.k = i; if(!vis[next.n][next.m]) { vis[next.n][next.m] = 1; next.qb = ti; que.push(next); } } if(i == 2) { next.n = now.n; next.m = 0; next.step = now.step+1; next.k = i; if(!vis[next.n][next.m]) { vis[next.n][next.m] = 1; next.qb = ti; que.push(next); } } if(i == 3) { if(now.n > b-now.m) { next.n = now.n-(b-now.m); next.m = b; } else { next.n = 0; next.m = now.n+now.m; } next.k = i; next.step = now.step+1; if(!vis[next.n][next.m]) { vis[next.n][next.m] = 1; next.qb = ti; que.push(next); } } if(i == 4) { if(now.m > a-now.n) { next.m = now.m-(a-now.n); next.n = a; } else { next.m = 0; next.n = now.m+now.n; } next.k = i; next.step = now.step+1; if(!vis[next.n][next.m]) { vis[next.n][next.m] = 1; next.qb = ti; que.push(next); } } if(i == 5) { next.n = a; next.m = now.m; next.step = now.step+1; next.k = i; if(!vis[next.n][next.m]) { vis[next.n][next.m] = 1; next.qb = ti; que.push(next); } } if(i == 6) { next.n = now.n; next.m = b; next.step = now.step+1; next.k = i; if(!vis[next.n][next.m]) { vis[next.n][next.m] = 1; next.qb = ti; que.push(next); } } } } printf("impossible\n"); } int main() { while(~scanf("%d%d%d",&a,&b,&c)) { bfs(); } return 0; }
相关文章推荐
- 指针的各种形式
- Android 模拟器AVD,设置电池状态
- lsof简介
- 实现几个连续输入的textfield
- 常用正则表达式
- PostgreSQL使用技巧
- iOS开发UI篇—CAlayer(自定义layer)
- 自定义UITableViewCell勾选框的保持
- [转]异常:android.os.NetworkOnMainThreadException
- CSS布局之水平垂直居中
- 拥抱生命……
- 在Hyper-V中使用USB设备的方法(简略)
- 九度OJ 题目1065:输出梯形
- 人体所需的7大营养素
- HDU 4322 Candy (最大费用最大流)经典
- Android Log图文详解(Log.v,Log.d,Log.i,Log.w,Log.e)
- tree
- Win7旗舰版升级Win10提示错误代码C1900107的解决方法
- 自己平时长期积累的java资料可供大家学习
- MYSQL备份与恢复