| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
BFS倒水 不要信翻译 网页翻译看不懂题目的 写代码1小时 debug一天我的网页翻译是这个样子 太可怕了 这是什么玩意????
题目看了我半天
你会得到两个罐子,它的容量是A和B一升一升。可以执行下列操作:
把罐子装满i(1≤)i ≤2);
倒空罐子i到排水沟;
从锅中倒出(i,j)i到锅j这次手术后,要么是锅j已经满了(而且可能还剩些水在锅里。)i),或者锅i是空的(它的所有内容都被移到了罐子里。)j).
编写一个程序,以找到这些操作的最短序列,这些操作将产生准确的结果。C其中一个罐子里有一升水。
-----------------------------------------------------------------
先分析完例子 就明白题目意思了
3 5 4
0 0
1.把B装满 -> FILL(2) -> 0 5
2.把B倒入A -> POUR(2,1) -> 3 2
3.把A倒了 -> DROP(1) -> 0 2
4.把B倒入A -> POUR(2,1) -> 2 0
5.把B装满 -> FILL(2) -> 2 5
6.把B倒入A -> POUR(2,1) -> 2 4
搞定了 是不是
BFS 我的理解是 对应情况全部讨论 一共6种情况 不合格的删掉
这个输出的话 我用了前驱的结点和栈实现。。
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
int n,m,k;
int vis[105][105] = {0};
struct node{
int a;
int b;
int step;
};
struct node1{
int data;
node1 *pre;
};
node1 op[10005];
void print(int n){
if(n == 0) cout << "FILL(1)" << endl;
else if(n == 1) cout << "POUR(1,2)" << endl;
else if(n == 2) cout << "DROP(1)" << endl;
else if(n == 3) cout << "FILL(2)" << endl;
else if(n == 4) cout << "POUR(2,1)" << endl;
else if(n == 5) cout << "DROP(2)" << endl;
}
int BFS(int n,int m,int k)
{
queue <node> q;
node head,end;
head.a = head.b = head.step = 0;
vis[head.a][head.b] = 1; //标记用的
q.push(head);
int s = 0; op[s].pre = NULL;
int ks = -1;
while(!q.empty()){
head = q.front();
q.pop();
//cout << head.a <<" " << head.b << " " << head.step <<endl;
ks++;
for(int i = 0; i < 6; i++){
memcpy(&end,&head,sizeof(node)); //复制
if(i == 0){
end.a = n;
}
else if(i == 1){
if(head.a >= m - head.b){
end.a = head.a - (m - head.b);
end.b = m;
}
else{
end.a = 0;
end.b = head.a + head.b;
}
}
else if(i == 2){
end.a = 0;
}
else if(i == 3) {
end.b = m;
}
else if(i == 4){
if(head.b >= n - head.a){
end.b = head.b -(n - head.a);
end.a = n;
}
else{
end.b = 0;
end.a = head.a + head.b;
}
}
else if(i == 5){
end.b = 0;
}
if(!vis[end.a][end.b]){
s++;
op[s].data = i;
op[s].pre = &op[ks];
end.step = head.step + 1;
vis[end.a][end.b] = 1;
q.push(end);
if(end.a == k || end.b == k){
//cout << end.a << " " << end.b << endl;
cout << end.step << endl;
stack <node1> ss;
node1 xx = op[s];
while(xx.pre != NULL){
ss.push(xx);
xx = *(xx.pre);
}
while(!ss.empty())
{
xx = ss.top();
//cout << xx.data << " ";
print(xx.data);
ss.pop();
}
return 0;
}
}
}
}
cout << "impossible" << endl;
}
int main()
{
cin >> n >> m >> k;
BFS(n,m,k);
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator