Pots(bfs)

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8266 Accepted: 3507 Special Judge

Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

  1. FILL(i)        fill the pot i (1 ≤ ≤ 2) from the tap;
  2. DROP(i)      empty the pot i to the drain;
  3. 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 AB, 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)

题意:给定三个数A B C,前两个数代表容量为A和B的容器,有三种操作,FILL,DROP,POUR;求最少经过几次这样的操作可以得到容量为C的水;

思路:因为每个容器都有FILL,DROP,POUR三种操作,将两个容器的状态用队列来维护,两个容器共有六种操作的可能;
另一个关键是打印路径,可以用一个结构体数组保存前驱,通过栈再将路径打印出来;
  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<algorithm>
  4 #include<queue>
  5 #include<stack>
  6 //各种状态
  7 #define FILL_A 1;
  8 #define FILL_B 2;
  9 #define DROP_A 3;
 10 #define DROP_B 4;
 11 #define POURA_B 5;
 12 #define POURB_A 6;
 13 using namespace std;
 14 
 15 struct node
 16 {
 17     int x,y;
 18     int step;
 19 };
 20 queue<struct node>que;
 21 
 22 struct Path
 23 {
 24     int x,y;
 25     int way;
 26 }path[110][110];//保存前驱;
 27 
 28 int vis[110][110];
 29 int a,b,c;
 30 
 31 //打印路径
 32 void Print_Path(int x, int y)
 33 {
 34     stack<struct Path>st;
 35     while(!st.empty())
 36         st.pop();
 37         
 38     while(!(x == 0 && y == 0))
 39     {
 40         st.push(path[x][y]);
 41         int sx = path[x][y].x;
 42         int sy = path[x][y].y;
 43         x = sx;
 44         y = sy;
 45     }
 46 
 47     while(!st.empty())
 48     {
 49         switch(st.top().way)
 50         {
 51             case 1:printf("FILL(1)\n");break;
 52             case 2:printf("FILL(2)\n");break;
 53             case 3:printf("DROP(1)\n");break;
 54             case 4:printf("DROP(2)\n");break;
 55             case 5:printf("POUR(1,2)\n");break;
 56             case 6:printf("POUR(2,1)\n");break;
 57         }
 58         st.pop();
 59     }
 60 }
 61 
 62 void bfs()
 63 {
 64     while(!que.empty())
 65         que.pop();
 66     que.push((struct node){0,0,0});//初始状态进队列
 67     vis[0][0] = 1;
 68     while(!que.empty())
 69     {
 70         struct node u = que.front();
 71         que.pop();
 72         if(u.x == c || u.y == c)
 73         {
 74             printf("%d\n",u.step);
 75             Print_Path(u.x,u.y);
 76             return;
 77         }
 78         //FILL_A
 79         if(u.x < a && !vis[a][u.y])
 80         {
 81             vis[a][u.y] = 1;
 82             que.push((struct node){a,u.y,u.step+1});
 83             path[a][u.y] = (struct Path){u.x,u.y,1};
 84         }
 85         //FILL_B
 86         if(u.y < b && !vis[u.x][b])
 87         {
 88             vis[u.x][b] = 1;
 89             que.push((struct node){u.x,b,u.step+1});
 90             path[u.x][b] = (struct Path){u.x,u.y,2};
 91         }
 92         //DROP_A
 93         if(u.x >0 && !vis[0][u.y])
 94         {
 95             vis[0][u.y] = 1;
 96             que.push((struct node){0,u.y,u.step+1});
 97             path[0][u.y] = (struct Path){u.x,u.y,3};
 98         }
 99         //DROP_B
100         if(u.y > 0 && !vis[u.x][0])
101         {
102             vis[u.x][0] = 1;
103             que.push((struct node){u.x,0,u.step+1});
104             path[u.x][0] = (struct Path){u.x,u.y,4};
105         }
106         //POURA_B
107         if(u.x < a && u.y > 0)
108         {
109             int tmp = min(a-u.x,u.y);
110             if(!vis[u.x+tmp][u.y-tmp])
111             {
112                 vis[u.x+tmp][u.y-tmp] = 1;
113                 que.push((struct node){u.x+tmp,u.y-tmp,u.step+1});
114                 path[u.x+tmp][u.y-tmp] = (struct Path){u.x,u.y,6};
115             }
116         }
117         //POURB_A
118         if(u.x > 0 && u.y < b)
119         {
120             int tmp = min(u.x,b-u.y);
121             if(!vis[u.x-tmp][u.y+tmp])
122             {
123                 vis[u.x-tmp][u.y+tmp] = 1;
124                 que.push((struct node){u.x-tmp,u.y+tmp,u.step+1});
125                 path[u.x-tmp][u.y+tmp] = (struct Path){u.x,u.y,5};
126             }
127         }
128     }
129     printf("impossible\n");
130 }
131 int main()
132 {
133     scanf("%d %d %d",&a,&b,&c);
134     memset(vis,0,sizeof(vis));
135     bfs();
136     return 0;
137 }
View Code

 

转载于:https://www.cnblogs.com/LK1994/p/3270989.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值