poj 3414 Pots ( bfs )
2013-08-28 18:22
459 查看
题目:http://poj.org/problem?id=3414
题意:给出了两个瓶子的容量A,B, 以及一个目标水量C,
对A、B可以有如下操作:
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).
问经过哪几个操作后能使得任意一个瓶子的残余水量为C。
若不可能得到则输出impossible
题意:给出了两个瓶子的容量A,B, 以及一个目标水量C,
对A、B可以有如下操作:
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).
问经过哪几个操作后能使得任意一个瓶子的残余水量为C。
若不可能得到则输出impossible
#include <iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<stack> #include<queue> #include<iomanip> #include<cmath> #include<map> #include<vector> #include<algorithm> using namespace std; int vis[110][110]; int a,b,c; struct node { int x,y,step; }pos,next; struct way { int x,y,f; }before[110][110]; void pri(int x,int y) { stack<int>st; while(x!=0||y!=0) { st.push(before[x][y].f); int tx=before[x][y].x; int ty=before[x][y].y; x=tx; y=ty; } while(!st.empty()) { switch(st.top()) { case 1:printf("DROP(1)\n"); break; case 2:printf("DROP(2)\n"); break; case 3:printf("FILL(1)\n"); break; case 4:printf("FILL(2)\n"); break; case 5:printf("POUR(1,2)\n"); break; case 6:printf("POUR(2,1)\n"); break; } st.pop(); } } int bfs() { queue<node>q; next.x=0; next.y=0; next.step=0; vis[0][0]=1; q.push(next); while(!q.empty()) { pos=q.front(); //cout<<pos.x<<" "<<pos.y<<" "<<pos.step<<" "; //cout<<before[pos.x][pos.y].x<<" "<<before[pos.x][pos.y].y<<" "<<before[pos.x][pos.y].f<<endl; q.pop(); if(pos.x==c||pos.y==c) { cout<<pos.step<<endl; pri(pos.x,pos.y); return 1; } if(!vis[0][pos.y]&&pos.x!=0) { next.x=0; next.y=pos.y; next.step=pos.step+1; q.push(next); vis[0][pos.y]=1; before[0][pos.y]=(struct way){pos.x,pos.y,1}; } if(!vis[pos.x][0]&&pos.y!=0) { next.x=pos.x; next.y=0; next.step=pos.step+1; q.push(next); vis[pos.x][0]=1; before[pos.x][0]=(struct way){pos.x,pos.y,2}; } if(!vis[a][pos.y]&&pos.x!=a) { next.x=a; next.y=pos.y; next.step=pos.step+1; q.push(next); vis[a][pos.y]=1; before[a][pos.y]=(struct way){pos.x,pos.y,3}; } if(!vis[pos.x][b]&&pos.y!=b) { next.x=pos.x; next.y=b; next.step=pos.step+1; q.push(next); vis[pos.x][b]=1; before[pos.x][b]=(struct way){pos.x,pos.y,4}; } if(pos.x>0&&pos.y<b) { int t=min(pos.x,b-pos.y); if(!vis[pos.x-t][pos.y+t]) { q.push((struct node){pos.x-t,pos.y+t,pos.step+1}); vis[pos.x-t][pos.y+t]=1; before[pos.x-t][pos.y+t]=(struct way){pos.x,pos.y,5}; } } if(pos.x<a&&pos.y>0) { int t=min(pos.y,a-pos.x); if(!vis[pos.x+t][pos.y-t]) { q.push((struct node){pos.x+t,pos.y-t,pos.step+1}); vis[pos.x+t][pos.y-t]=1; before[pos.x+t][pos.y-t]=(struct way){pos.x,pos.y,6}; } } } return 0; } int main() { cin>>a>>b>>c; memset(vis,0,sizeof(vis)); if(bfs()==0) cout<<"impossible"<<endl; return 0; }
相关文章推荐
- poj 3414 Pots ----BFS 最短路和路径
- 【原】 POJ 3414 Pots 状态BFS 解题报告
- BFS-POJ-3414-Pots
- POJ 3414 Pots bfs打印方案
- POJ 3414 Pots 暴力,bfs 难度:1
- 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+回溯路径)
- 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
- poj 3414 Pots(bfs)