poj3414(bfs广度优先搜索)

来源:互联网 发布:衣服淘宝店铺 编辑:程序博客网 时间:2024/05/16 03:51

      Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10197 Accepted: 4301 Special Judge
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)

      终于没有在没有看别人的代码的情况下,把广度优先遍历所涉及到的知识给写了一遍,原来广度优先遍历还可以通过一直记录先前的节点来作为当前节点的父节点,用来输出路径,真不错,这道题和1606太像了,只不过是比1606多了个不存在的情况,加一个判断就行了,所以我稍微改动了一下就提交了,结果也ac了,嘿嘿,第一次提交的时候就是因为没看到不成立的情况,结果就wa了,看来还是得好好看题,考虑全面一点,看了好多道bfs的题目,但是就是不敢真正下手做,有点害怕,其实真正去读题,理解题意还好啦,其实就是应该做到题目中去,认真分析,写好关键部分的算法,不要有点想法就去敲代码了,那样会出现各种错误。好了,放代码

#include<stdio.h>#include<stdlib.h>#include<string.h>#include<queue>using namespace std;int dist[105][105];//用来记录所做的操作的个数 。 int visit[105][105];//用来标记是否已经访问过,来决定是否入队列 。 int last[105][105];//用来标记所做的操作,1代表把A杯子充满,2代表把A杯子清空,3代表A倒向B杯子,4代表把B杯子充满,5代表把B杯子清空,6代表B杯子倒向A杯子 。 int dir[100000];//存放last[][]数组的值,以便转化为具体的操作。 int c;typedef struct{int a,b;}node;node m,t;node fa[105][105]={{0,0}};//用于存放节点的父节点,以便以后能找到路径 int path(int x,int y)//此函数用于求出路径 {c=0;node q;for(;;){if(fa[x][y].a==x&&fa[x][y].b==y)break;q=fa[x][y];dir[c++]=last[x][y];x=q.a;y=q.b;}return 0;} int bfs(int x,int y,int z){queue<node>q;m.a=0;m.b=0;q.push(m);dist[m.a][m.b]=0;fa[m.a][m.b]=m;visit[m.a][m.b]=1;while(!q.empty()){m=q.front();//printf("%d**%d\n",m.a,m.b);//printf("%d&*&*&\n",dist[m.a][m.b]);q.pop();if(m.a==z||m.b==z){   // printf("%d * %d\n",m.a,m.b);   // printf("%d&*%d\n",fa[m.a][m.b].a,fa[m.a][m.b].b);    path(m.a,m.b);  // printf("%d^\n",last[0][5]);  // printf("%d^\n",last[3][4]);return dist[m.a][m.b];}else{if(m.a!=x){ t=m; t.a=x;if(!visit[t.a][t.b]){q.push(t);fa[t.a][t.b]=m;last[t.a][t.b]=1;dist[t.a][t.b]=dist[m.a][m.b]+1;visit[t.a][t.b]=1; }    }    if(m.a!=0)    {    t=m;     t.a=0;if(!visit[t.a][t.b]){q.push(t);fa[t.a][t.b]=m;last[t.a][t.b]=2;dist[t.a][t.b]=dist[m.a][m.b]+1;visit[t.a][t.b]=1; }    }    if(m.a!=0)    {    t=m;    if(t.a+t.b>=y)    {    t.a=t.a+t.b-y;    t.b=y;    }    else    {    t.b=t.a+t.b;    t.a=0;        }    if(!visit[t.a][t.b])    {    q.push(t);    fa[t.a][t.b]=m;    last[t.a][t.b]=3;    dist[t.a][t.b]=dist[m.a][m.b]+1;    visit[t.a][t.b]=1;    }    }    if(m.b!=y)    {    t=m;   t.b=y;if(!visit[t.a][t.b]){q.push(t);fa[t.a][t.b]=m;last[t.a][t.b]=4;dist[t.a][t.b]=dist[m.a][m.b]+1;visit[t.a][t.b]=1; }    }    if(m.b!=0)    {    t=m;     t.b=0;if(!visit[t.a][t.b]){q.push(t);fa[t.a][t.b]=m;last[t.a][t.b]=5;dist[t.a][t.b]=dist[m.a][m.b]+ 1 ;visit[t.a][t.b]=1; }    }    if(m.b!=0)    {    t=m;    if(t.a+t.b>=x)    {    t.b=t.b+t.a-x;    t.a=x;    }    else    {    t.a=t.a+t.b;    t.b=0;    }    if(!visit[t.a][t.b])    {    q.push(t);    fa[t.a][t.b]=m;    last[t.a][t.b]=6;    dist[t.a][t.b]=dist[m.a][m.b]+1;    visit[t.a][t.b]=1;    }    }}}return 0;}int main() {int x,y,z;while(scanf("%d%d%d",&x,&y,&z)!=EOF){memset(dist,0,sizeof(dist));memset(visit,0,sizeof(visit));memset(last,0,sizeof(last));memset(dir,0,sizeof(dir));int k=bfs(x,y,z);if(k==0)printf("impossible\n");else{printf("%d\n",k);while(c--){if(dir[c]==1)puts("FILL(1)");    else if(dir[c]==2)    puts("DROP(1)");    else if(dir[c]==3)puts("POUR(1,2)");else if(dir[c]==4)puts("FILL(2)");else if(dir[c]==5)puts("DROP(2)");else if(dir[c]==6)puts("POUR(2,1)");}}}}

       

0 0
原创粉丝点击