动态规划(3)Pots (BFS)
来源:互联网 发布:怎么在ubuntu上安装qq 编辑:程序博客网 时间:2024/06/06 06:45
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 potj is full (and there may be some water left in the poti), or the poti is empty (and all its contents have been moved to the potj).
Write a program to find the shortest possible sequence of these operations that will yield exactlyC liters of water in one of the pots.
Input
On the first and only line are the numbers A, B, andC. These are all integers in the range from 1 to 100 andC≤max(A,B).
Output
The first line of the output must contain the length of the sequence of operationsK. The followingK 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
6FILL(2)POUR(2,1)DROP(1)POUR(2,1)FILL(2)POUR(2,1)
Source
#include<stdio.h>#include<queue>#include<stdlib.h>#include<string.h>#define MAX 1000using namespace std;int goal,m,n,flag=1;//goal 目标;m,n两个瓶子的容量;flag 标记能否达到目标。int p = 0; //用于记录路径的下标struct State //状态{int x,y;char op[MAX]; // 记录路径int step_counter; //记录步数 }st;bool visit[MAX][MAX]; //记录是否已访问bool check_state(State s){if(!visit[s.x][s.y])return 1;else return 0;};State full1 (State a){a.x=m;a.op[p] = '1';return a;};State full2 (State a ){a.y=n;a.op[p] = '2';return a;}State drop1(State a){a.x=0;a.op[p] = '3';return a;};State drop2(State a){a.y=0;a.op[p] = '4';return a;};State pour1(State a){if(a.x>n-a.y){ a.x = a.x-n+a.y;a.y = n;}else{ a.y += a.x; a.x = 0; }a.op[p] = '5';return a;};State pour2(State a){if(a.y>m-a.x){ a.y = a.y+a.x-m;a.x = m;}else{ a.x += a.y; a.y = 0; }a.op[p] = '6';return a;};void bfs(State start){queue <State> q;State now,next;q.push(start);start.step_counter = 0;visit[start.x][start.y] = 1;while(!q.empty()){now = q.front();p=now.step_counter;if(now.x == goal || now.y == goal)//符合条件{printf("%d\n",now.step_counter);flag = 0;for(int k=0;k<now.step_counter;k++){ switch(now.op[k]-'0')// 输出路径 { case(1): printf("FILL(1)\n");break; case(2): printf("FILL(2)\n");break; case(3): printf("DROP(1)\n");break; case(4): printf("DROP(2)\n");break; case(5): printf("POUR(1,2)\n");break; case(6): printf("POUR(2,1)\n");break; }}return;}for(int i=1;i<=6;i++){switch(i){case(1):next = full1(now);break;//六种操作case(2):next = full2(now);break;case(3):next = drop1(now);break;case(4):next = drop2(now);break;case(5):next = pour1(now);break;case(6):next = pour2(now);break;}if(check_state(next)){next.step_counter = now.step_counter + 1;q.push(next);visit[next.x][next.y]= 1;}}q.pop();}}int main(){scanf("%d %d %d",&m,&n,&goal);st.step_counter = st.x = st.y = 0;for(int h=0;h<MAX;h++)st.op[h] = '\0';//初始化bfs(st);if(flag) printf("impossible\n");system("pause");return 0;}
- 动态规划(3)Pots (BFS)
- (BFS)3414 Pots
- pots(BFS)
- Pots(BFS)
- POJ3414 Pots(BFS)
- 【POJ3414】Pots(BFS)
- poj_3414 Pots(bfs)
- Pots (bfs)
- POJ 3414 Pots(BFS)||SDUT(Pots)
- POJ 3414 Pots (BFS)
- SDUT--Pots(二维BFS)
- POJ 3414 Pots (BFS)
- POJ 3414 Pots(bfs)
- poj 3414 Pots(Bfs)
- 【POJ 3414】Pots(BFS)
- POJ 3414 Pots(bfs)
- POJ.3414 Pots (BFS)
- 【POJ 3414】Pots(BFS)
- Android 对图片的缩放,拖地,跟系统图库的效果一样
- NYOJ三点顺序 南工68
- 数据结构之线性表1
- 数据库范式详解
- MyEclipse插件安装的几种方式
- 动态规划(3)Pots (BFS)
- 如何把数据库以二进制存储的excel,word文档写入excel,word
- 使用JQuery.js & JQuery.form.js 插件完成对StrutsAction的异步请求,返回JSON数据
- 1113 正/负数统计
- 开发者应该了解的 12 款 Eclipse 插件
- SQL Server默认值中的所有字段的列出
- Type Casting
- Java设计模式之十四(组合模式)
- 最长公共上升子序列(LCIS)的O(n^2)算法