POJ-1606 Jugs
2012-11-20 20:45
218 查看
Jugs
DescriptionIn the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3-gallon jug and a 5-gallon jug and were asked to fill the 5-gallon jug with exactly 4 gallons. This problem generalizes that puzzle.You have two jugs, A and B, and an infinite supply of water. There are three types of actions that you can use: (1) you can fill a jug, (2) you can empty a jug, and (3) you can pour from one jug to the other. Pouring from one jug to the other stops when the first jug is empty or the second jug is full, whichever comes first. For example, if A has 5 gallons and B has 6 gallons and a capacity of 8, then pouring from A to B leaves B full and 3 gallons in A.A problem is given by a triple (Ca,Cb,N), where Ca and Cb are the capacities of the jugs A and B, respectively, and N is the goal. A solution is a sequence of steps that leaves exactly N gallons in jug B. The possible steps arefill A
fill B
empty A
empty B
pour A B
pour B A
successwhere "pour A B" means "pour the contents of jug A into jug B", and "success" means that the goal has been accomplished.You may assume that the input you are given does have a solution.
InputInput to your program consists of a series of input lines each defining one puzzle. Input for each puzzle is a single line of three positive integers: Ca, Cb, and N. Ca and Cb are the capacities of jugs A and B, and N is the goal. You can assume 0 < Ca <= Cb and N <= Cb <=1000 and that A and B are relatively prime to one another.
OutputOutput from your program will consist of a series of instructions from the list of the potential output lines which will result in either of the jugs containing exactly N gallons of water. The last line of output for each puzzle should be the line "success". Output lines start in column 1 and there should be no empty lines nor any trailing spaces.
Sample Input
Sample Output
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 4076 | Accepted: 2411 | Special Judge |
fill B
empty A
empty B
pour A B
pour B A
successwhere "pour A B" means "pour the contents of jug A into jug B", and "success" means that the goal has been accomplished.You may assume that the input you are given does have a solution.
InputInput to your program consists of a series of input lines each defining one puzzle. Input for each puzzle is a single line of three positive integers: Ca, Cb, and N. Ca and Cb are the capacities of jugs A and B, and N is the goal. You can assume 0 < Ca <= Cb and N <= Cb <=1000 and that A and B are relatively prime to one another.
OutputOutput from your program will consist of a series of instructions from the list of the potential output lines which will result in either of the jugs containing exactly N gallons of water. The last line of output for each puzzle should be the line "success". Output lines start in column 1 and there should be no empty lines nor any trailing spaces.
Sample Input
3 5 4 5 7 3
Sample Output
fill B pour B A empty A pour B A fill B pour B A success fill A pour A B fill A pour A B empty B pour A B success
/*注意:本题是让 b 中达到目标状态 用到队列 + 记忆化搜索 //代码一:纯模拟---从讨论区看见的,由于本题没有要求输出的结果是最少的操作次数,所以这种方法也是可以的 #include <iostream> using namespace std; int main() { int ca,cb,n; while (cin >> ca >> cb >> n) { int bnow; int b = 0; while (b != n) { for (int i = 0;i <= (cb - b) / ca;i++) { cout << "fill A" << endl; cout << "pour A B" << endl; bnow = b + ca * (i + 1); if (bnow == n) { break; } } if (bnow == n) { break; } cout << "empty B" << endl; int a; a = ca - (cb - b) % ca; cout << "pour A B" << endl; b = a; if (b == n) { break; } } cout << "success" << endl; } return 0; } */ /* //代码二: 用STL队列,可以保证结果肯定是最少的操作次数-----搜索路径算法还未完成 #include <iostream> #include <cstring> //#include <queue> using namespace std; struct state { int a; int b; int opt; int last_opt; int tot_opt; }; char cmd[6][10] = {"fill A", "fill B", "empty A", "empty B", "pour A B", "pour B A"}; bool visit[1001][1001]; int path[200]; int ca, cb, n; int BFS(int sa, int sb, int steps) { queue <state> q; front = tail = 0; state t1, t2; t1.a = 0; t1.b = 0; t1.opt = -1; t1.last_opt = -1; t1.tot_opt = 0; q.push(t1); while(!q.empty()) { t2 = q.front(); q.pop(); if(t2.b ==n) { 。。。。。。。。 。。。。。。。。 // 因为用的是stl中的queue,所以这里还没想到应该咋实现才能回溯遍历所有的路径 。。。。。。。。 } else { state t3; if(t2.a != ca && !visit[ca][t2.b]) //fill A { t3.a = ca; t3.b = t2.b; t3.opt = 0; t3.last_opt = t2.opt; t3.tot_opt = t2.tot_opt + 1; visit[ca][t2.b] = true; q.push(t3); } if(t2.b != cb && !visit[t2.a][cb]) // fill B { t3.a = t2.a; t3.b = cb; t3.opt = 1; t3.last_opt = t2.opt; t3.tot_opt = t2.tot_opt + 1; visit[t2.a][cb] = true; q.push(t3); } if(t2.a != 0 && !visit[0][t2.b]) // empty A { t3.a = 0; t3.b = t2.b; t3.opt = 2; t3.last_opt = t2.opt; t3.tot_opt = t2.tot_opt + 1; visit[0][t2.b] = true; q.push(t3); } if(t2.b != 0 && !visit[t2.a][0]) // empty B { t3.a = t2.a; t3.b = 0; t3.opt = 3; t3.last_opt = t2.opt; t3.tot_opt = t2.tot_opt + 1; visit[t2.a][0] = true; q.push(t3); } if(sb != cb && t2.a != 0) // pour A to B { if(t2.a <= cb - t2.b) { t3.a = 0; t3.b = t2.b + t2.a; } else { t3.a = t2.a - (cb - t2.b); t3.b = cb; } t3.opt = 4; t3.last_opt = t2.opt; t3.tot_opt = t2.tot_opt + 1; visit[t3.a][t3.b] = true; q.push(t3); } if(t2.a != ca && cb != 0) // pour B to A { if(t2.b <= ca - t2.a) { t3.b = 0; t3.a = t2.a + t2.b; } else { t3.a = ca; t3.b = t2.b - (ca - t2.a); } t3.opt = 5; t3.last_opt = t2.opt; t3.tot_opt = t2.tot_opt + 1; visit[t3.a][t3.b] = true; q.push(t3); } } } } int main() { while(cin >> ca >> cb >> n) { memset(visit, false, sizeof(visit)); memset(path, 0, sizeof(path)); visit[0][0] = true; int tot_step = BFS(0, 0, 0); for(int i = 1; i <= tot_step; ++i) cout << cmd[path[i]] << endl; cout << "success" <<endl; } return 0; } */ //代码三:------AC #include <iostream> #include <cstring> using namespace std; struct state { int a; //记录当前a中的量 int b; //记录当前b中的量 int opt; //记录当前状态是由前一状态怎样得来的,即记录的是操作数----对应的是cmd数组的一维下标 int last_path; //记录上一步所在状态的队列下标,方便回溯查找路径 int tot_opt; //记录到底本状态总共用了多少步 }q[10000]; char cmd[6][10] = {"fill A", "fill B", "empty A", "empty B", "pour A B", "pour B A"}; bool visit[1001][1001]; int path[200]; int ca, cb, n; int BFS(int sa, int sb, int steps) { int front, tail; front = tail = 0; state t1, t2; t1.a = 0; t1.b = 0; t1.opt = -1; t1.last_path = -1; t1.tot_opt = 0; q[tail++] = t1; while(front != tail) { t2 = q[front++]; if(t2.b == n) { state t = t2; for(int i = t.tot_opt; i > 0; --i) { path[i] = t.opt; t = q[t.last_path]; } return t2.tot_opt; } else { state t3; if(t2.a != ca && !visit[ca][t2.b]) //fill A { t3.a = ca; t3.b = t2.b; t3.opt = 0; t3.last_path = front - 1; t3.tot_opt = t2.tot_opt + 1; visit[ca][t2.b] = true; q[tail++] = t3; } if(t2.b != cb && !visit[t2.a][cb]) // fill B { t3.a = t2.a; t3.b = cb; t3.opt = 1; t3.last_path = front - 1; t3.tot_opt = t2.tot_opt + 1; visit[t2.a][cb] = true; q[tail++] = t3; } if(t2.a != 0 && !visit[0][t2.b]) // empty A { t3.a = 0; t3.b = t2.b; t3.opt = 2; t3.last_path = front - 1; t3.tot_opt = t2.tot_opt + 1; visit[0][t2.b] = true; q[tail++] = t3; } if(t2.b != 0 && !visit[t2.a][0]) // empty B { t3.a = t2.a; t3.b = 0; t3.opt = 3; t3.last_path = front - 1; t3.tot_opt = t2.tot_opt + 1; visit[t2.a][0] = true; q[tail++] = t3;; } if(sb != cb && t2.a != 0) // pour A to B { if(t2.a <= cb - t2.b) { t3.a = 0; t3.b = t2.b + t2.a; } else { t3.a = t2.a - (cb - t2.b); t3.b = cb; } t3.opt = 4; t3.last_path = front - 1; t3.tot_opt = t2.tot_opt + 1; visit[t3.a][t3.b] = true; q[tail++] = t3; } if(t2.a != ca && cb != 0) // pour B to A { if(t2.b <= ca - t2.a) { t3.b = 0; t3.a = t2.a + t2.b; } else { t3.a = ca; t3.b = t2.b - (ca - t2.a); } t3.opt = 5; t3.last_path = front - 1; t3.tot_opt = t2.tot_opt + 1; visit[t3.a][t3.b] = true; q[tail++] = t3; } } } } int main() { while(cin >> ca >> cb >> n) { memset(visit, false, sizeof(visit)); memset(path, 0, sizeof(path)); visit[0][0] = true; int tot_step = BFS(0, 0, 0); for(int i = 1; i <= tot_step; ++i) cout << cmd[path[i]] << endl; cout << "success" <<endl; } return 0; }
相关文章推荐
- [POJ] 1606 Jugs(BFS+路径输出)
- ZOJ1005 POJ1606 Jugs 数论法
- 【原】 POJ 1606 Jugs 状态BFS 解题报告
- POJ 1606 Jugs
- POJ 1606 Jugs
- poj 1606 Jugs and poj 3414 Pots(数组模拟BFS) uva 571 (数论)
- POJ 1606 && HDU 1415 Jugs(机智)
- POJ_1606_Jugs
- poj 1606 Jugs
- POJ-1606 Jugs
- POJ 1606 Jugs 已被翻译
- poj 1606 Jugs
- poj1606 Jugs
- zoj 1005 Jugs || poj 1606 (广搜做法~~~~)
- poj 1606 Jugs
- POJ 1606 Jugs
- poj 1606 Jugs(广搜BFS+路径输出)
- POJ 1606 Jugs(BFS:找最短路径并输出)
- poj 1606 Jugs (bfs)
- poj 1606 Jugs