kuangbin 简单搜索 H题
来源:互联网 发布:淄博网络办徐淑娟老公 编辑:程序博客网 时间:2024/06/05 04:03
H - 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 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找最短路,dfs回溯求出路径。
代码量有点长,下次优化一下,不过0ms可过。
代码:
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>using namespace std;const int MAXN = 110;const int INF = 0x3f3f3f3f;struct Status{ int x,y; Status(int x,int y):x(x),y(y){} Status(){}};int A,B,C;char op[6][10]={{"FILL(1)"},{"FILL(2)"},{"DROP(1)"},{"DROP(2)"},{"POUR(1,2)"},{"POUR(2,1)"}};typedef struct{ int op,steps;}Vis;Vis vis[MAXN][MAXN];vector<string> ans;bool flag=0;void dfs(int x,int y,int times){ if(flag) return; if(times==0&&(x==C||y==C)) { for(int i=0;i<ans.size();i++) { cout<<ans[i]<<endl; } flag=1; return; } //FILL(1) int nx = A,ny = y; if(vis[nx][ny].steps==vis[x][y].steps+1) { ans.push_back(op[vis[nx][ny].op]); dfs(nx,ny,times-1); ans.pop_back(); } //FILL(2) nx = x,ny = B; if(vis[nx][ny].steps==vis[x][y].steps+1) { ans.push_back(op[vis[nx][ny].op]); dfs(nx,ny,times-1); ans.pop_back(); } //DROP(1) nx=0,ny=y; if(vis[nx][ny].steps==vis[x][y].steps+1) { ans.push_back(op[vis[nx][ny].op]); dfs(nx,ny,times-1); ans.pop_back(); } //DROP(2) nx=x,ny=0; if(vis[nx][ny].steps==vis[x][y].steps+1) { ans.push_back(op[vis[nx][ny].op]); dfs(nx,ny,times-1); ans.pop_back(); } //POUR(1,2) if(x<=B-y) { nx=0; ny=y+x; } else { nx=x-(B-y); ny=B; } if(vis[nx][ny].steps==vis[x][y].steps+1) { ans.push_back(op[vis[nx][ny].op]); dfs(nx,ny,times-1); ans.pop_back(); } //POUR(2,1) if(y<=A-x) { nx=y+x; ny=0; } else { nx=A; ny=y-(A-x); } if(vis[nx][ny].steps==vis[x][y].steps+1) { ans.push_back(op[vis[nx][ny].op]); dfs(nx,ny,times-1); ans.pop_back(); }}void bfs(){ memset(vis,0x3f,sizeof(vis)); queue<Status> que; que.push(Status(0,0)); vis[0][0].steps=0; while(que.size()) { Status start = que.front(); que.pop(); if(start.x==C||start.y==C) { printf("%d\n",vis[start.x][start.y].steps); flag=0; dfs(0,0,vis[start.x][start.y].steps); return; } Status temp; //FILL(1) temp.x=A,temp.y=start.y; if(vis[A][temp.y].steps==INF) { vis[A][temp.y].steps=vis[start.x][start.y].steps+1; vis[A][temp.y].op=0; que.push(temp); } //FILL(2) temp.x=start.x,temp.y=B; if(vis[temp.x][B].steps==INF) { vis[temp.x][B].steps=vis[start.x][start.y].steps+1; vis[temp.x][B].op=1; que.push(temp); } //DROP(1) temp.x=0,temp.y=start.y; if(vis[0][temp.y].steps==INF) { vis[0][temp.y].steps=vis[start.x][start.y].steps+1; vis[0][temp.y].op=2; que.push(temp); } //DROP(2) temp.x=start.x,temp.y=0; if(vis[temp.x][0].steps==INF) { vis[temp.x][0].steps=vis[start.x][start.y].steps+1; vis[temp.x][0].op=3; que.push(temp); } //POUR(1,2) if(start.x<=B-start.y) { temp.x=0; temp.y=start.y+start.x; } else { temp.x=start.x-(B-start.y); temp.y=B; } if(vis[temp.x][temp.y].steps==INF) { vis[temp.x][temp.y].steps=vis[start.x][start.y].steps+1; vis[temp.x][temp.y].op=4; que.push(temp); } //POUR(2,1) if(start.y<=A-start.x) { temp.x=start.y+start.x; temp.y=0; } else { temp.x=A; temp.y=start.y-(A-start.x); } if(vis[temp.x][temp.y].steps==INF) { vis[temp.x][temp.y].steps=vis[start.x][start.y].steps+1; vis[temp.x][temp.y].op=5; que.push(temp); } } printf("impossible\n");}int main(){ while(cin>>A>>B>>C) { bfs(); ans.clear(); } return 0;}
- kuangbin 简单搜索 H题
- kuangbin 简单搜索C题
- kuangbin 简单搜索 L题
- kuangbin 简单搜索 F题
- [kuangbin带你飞]专题一 简单搜索 - H - Pots
- [kuangbin带你飞]专题一 简单搜索 H POJ3414
- [kuangbin带你飞]专题一 简单搜索 H
- [kuangbin带你飞]专题1 简单搜索 H
- kuangbin [简单搜索专题]
- kuangbin 简单搜索 B
- kuangbin 简单搜索 E
- kuangbin 简单搜索 I
- kuangbin KMP H题
- [kuangbin带你飞]专题一 简单搜索H - Pots(POJ 3414)
- [kuangbin带你飞]专题一 简单搜索 H - Pots poj 3414
- kuangbin求带飞简单搜索 棋盘问题
- kuangbin 简单搜索 A 棋盘问题
- kuangbin 简单搜索 J 双bfs
- 676. Implement Magic Dictionary
- java中&和&&的区别?
- 拉丁方块填数字--蓝桥杯国赛历年真题
- 免费的论文查重网站
- CCF-201512-1-数位之和
- kuangbin 简单搜索 H题
- 原生JS获取元素css属性
- 指数加权平均 exponentially weighted averages
- codeforce 698A Vacations——贪心——动态规划
- set header()
- Android应用程序适配多设备
- windows+jenkins+robotframework无法修改浏览器分辨率
- a标签总结
- linux下安装python3.5