[kuangbin带你飞]专题一 简单搜索 - H - Pots
2016-05-06 23:59
393 查看
Pots
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 pot j is 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.
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
Sample Output
分析:bfs加状态记录.水题
code:
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 pot j is 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.
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 4
Sample Output
6 FILL(2) POUR(2,1) DROP(1) POUR(2,1) FILL(2) POUR(2,1)
分析:bfs加状态记录.水题
code:
#include <cstdio> #include <queue> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; typedef long long ll; struct dat{ int a, b; int choice, c, d; struct dat *front; }; dat *star; int vis[105][105], flag, goal, cupa, cupb; void prin(dat *p,int ans) { if (p->front == NULL) { cout << ans << endl; return ; } if(p->front!=NULL) { prin(p->front,ans+1); } switch (p->choice) { case 0: { printf("FILL(%d)\n", p->c); break; } case 1: { printf("POUR(%d,%d)\n", p->c, p->d); break; } case 2: { printf("DROP(%d)\n", p->c); break; } } } dat *bfs() { star = (dat*) malloc (sizeof(dat)); queue <dat*> q; star->front = NULL; star->choice = -1; star->a = 0; star->b = 0; star->c=0,star->d=0; q.push(star); while (q.size()) { dat *now = q.front(); // prin(now,1); //cout<<now->a<<' '<<now->b<<endl; q.pop(); if (now->a == goal || now->b == goal) { flag = 1; return now; } dat *nex; nex=(dat*) malloc (sizeof(dat)); //nex=now; for (int i = 0; i < 6; i++) { nex=(dat*) malloc (sizeof(dat)); nex->a=now->a,nex->b=now->b,nex->choice=now->choice,nex->c=now->c,nex->d=now->d; switch (i) { case 0: { nex->a = cupa; nex->choice = i/2; nex->c = 1; nex->front=now; if (vis[nex->a][nex->b] == 0) { // cout<<nex->a<<' '<<nex->b<<endl; q.push(nex); vis[nex->a][nex->b] = 1; } break; } case 1: { //nex = now; nex->b = cupb; nex->choice = i / 2; nex->c = 2; nex->front=now; if (vis[nex->a][nex->b] == 0) { //cout<<nex->a<<' '<<nex->b<<endl; q.push(nex); vis[nex->a][nex->b] = 1; } break; } case 2: { if(now->choice==-1) break; if (now->a >= cupb - now->b) { nex->b = cupb; nex->a = now->a - (cupb - now->b); nex -> choice = i / 2; nex->c = 1; nex->d = 2; nex->front=now; if (vis[nex->a][nex->b] == 0) { // cout<<nex->a<<' '<<nex->b<<endl; q.push(nex); vis[nex->a][nex->b] = 1; } break; } else { nex->a = 0; nex->b = now->a + now->b; nex->choice = i / 2; nex->c = 1; nex->d = 2; nex->front=now; if (vis[nex->a][nex->b] == 0) { // cout<<nex->a<<' '<<nex->b<<endl; q.push(nex); vis[nex->a][nex->b] = 1; } break; } } case 3: { if(now->choice==-1) break; if (now->b >= cupa - now->a) { nex->a = cupa; nex->b = now->b - (cupa - now->a); nex->choice = i / 2; nex->c = 2; nex->d = 1; nex->front=now; if (vis[nex->a][nex->b] == 0) { // cout<<nex->a<<' '<<nex->b<<endl; q.push(nex); vis[nex->a][nex->b] = 1; } break; } else { nex->b = 0; nex->a = now->a + now->b; nex->choice = i / 2; nex->c = 2; nex->d = 1; nex->front=now; if (vis[nex->a][nex->b] == 0) { // cout<<nex->a<<' '<<nex->b<<endl; q.push(nex); vis[nex->a][nex->b] = 1; } break; } } case 4: { if(now->choice==-1) break; nex->choice = i / 2; nex->a = 0; nex->c = 1; nex->front=now; if (vis[nex->a][nex->b] == 0) { // cout<<nex->a<<' '<<nex->b<<endl; q.push(nex); vis[nex->a][nex->b] = 1; } break; } case 5: { if(now->choice==-1) break; nex->choice = i / 2; nex->b = 0; nex->c = 2; nex->front=now; if (vis[nex->a][nex->b] == 0) { // cout<<nex->a<<' '<<nex->b<<endl; q.push(nex); vis[nex->a][nex->b] = 1; } break; } } } } return NULL; } void solve() { memset(vis, 0, sizeof vis); flag = 0; dat *p = bfs(); if (flag) prin(p,0); else cout << "impossible" << endl; } int main(void) { while (cin >> cupa >> cupb >> goal) { solve(); } }
相关文章推荐
- Codeforces Round #350 (Div. 2) A B C D1 D2 E
- ArchLinux 安装 MongoDB
- Java之------类反射系列(二)
- 自定义动画控件 AnimationView
- 关于BitMap中getWidth()与系统如何加载Drawable资源的问题
- 单向链表结点删除问题
- 不要使用Android Studio的Git Commit了---->记一次debug
- 框架选择
- Android实现手势控制
- SystemService
- ContentProvider
- 基础算法题----兔子问题(斐波那契数列)
- javaScript 操作Css样式
- gdb调试多进程与多线程
- php实习的tips(常见的字符串处理函数)
- File
- nova boot from volume代码分析
- 数据储存方式-SQLite
- ecshop弹出登录框的效果
- Hashcat