当前位置: 首页 > news >正文

Pots bfs()记录每一种状态,直到求出最优值

Problem 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)
***************************************************************************************************************************
bfs(),简单的,记录路径很重要
***************************************************************************************************************************
  1 #include<iostream>
  2 #include<string>
  3 #include<cstring>
  4 #include<cstdio>
  5 #include<vector>
  6 using namespace std;
  7 int a,b,c;
  8 int vis[1001][1001],step1,lastv;//vis标记数组
  9 int id[10001];
 10 int tail,head,flag;
 11 struct  node
 12 {
 13     int pre;//记录前驱
 14     int k1,k2;//两个桶中的水量
 15     int operation;//操作
 16     int step;//记录步数
 17 }que[10000001],now,next;
 18 void bfs()
 19 {
 20     tail=head=0;
 21         now.k1=0;
 22         now.k2=0;
 23         now.pre=0;
 24         now.operation=0;
 25         now.step=0;
 26         que[tail++]=now;
 27         vis[0][0]=1;
 28         while(head<tail)
 29         {
 30             now=que[head];
 31             head++;
 32             if(now.k1==c||now.k2==c)
 33             {
 34                 flag=1;
 35                 step1=now.step;
 36                 lastv=head-1;
 37                 return;
 38             }
 39             for(int it=0;it<6;it++)//六种操作
 40             {
 41                 if(it==0)//fill(1);
 42                 {
 43                     next.k1=a;
 44                     next.k2=now.k2;
 45                 }
 46                 if(it==1)//fill(2);
 47                  {
 48                      next.k1=now.k1;
 49                      next.k2=b;
 50                  }
 51                 if(it==2)//drop(1);
 52                  {
 53                      next.k1=0;
 54                      next.k2=now.k2;
 55                  }
 56                  if(it==3)//drop(2);
 57                  {
 58                      next.k1=now.k1;
 59                      next.k2=0;
 60                  }
 61                  if(it==4)//pour(1,2);
 62                  {
 63                      if(now.k1+now.k2<=b)
 64                      {
 65                          next.k1=0;
 66                          next.k2=now.k1+now.k2;
 67                      }
 68                      else
 69                      {
 70                          next.k1=now.k1+now.k2-b;
 71                          next.k2=b;
 72                      }
 73                  }
 74                  if(it==5)//pour(2,1);
 75                  {
 76                      if(now.k1+now.k2<=a)
 77                      {
 78                          next.k1=now.k1+now.k2;
 79                          next.k2=0;
 80                      }
 81                      else
 82                      {
 83                          next.k2=now.k1+now.k2-a;
 84                          next.k1=a;
 85                      }
 86                  }
 87                  next.operation=it;
 88                  if(!vis[next.k1][next.k2])//标记检测完退出
 89                  {
 90                      vis[next.k1][next.k2]=1;
 91                      next.step=now.step+1;
 92                      next.pre=head-1;
 93                      //cout<<next.pre<<endl;
 94                      que[tail].k1=next.k1;que[tail].k2=next.k2;
 95                      que[tail].step=next.step;que[tail].pre=next.pre;
 96                      que[tail].operation=next.operation;
 97                      tail++;
 98                  }
 99                  if(next.k1==c||next.k2==c)
100                  {
101                      flag=1;
102                      step1=next.step;
103                      lastv=tail-1;
104                      return;
105 
106                  }
107             }
108 
109         }
110 }
111 int main()
112 {
113     while(scanf("%d%d%d",&a,&b,&c)!=EOF)
114     {
115         flag=0;
116         step1=0;
117         memset(vis,0,sizeof(vis));
118         bfs();
119         if(flag)
120         {
121             printf("%d\n",step1);
122             id[step1]=lastv;
123             for(int i=step1-1;i>=1;i--)
124              {
125                  //printf("que[id[%d+1]].pre: %d\n",que[id[i+1]].pre);
126                  id[i]=que[id[i+1]].pre;
127              }
128             for(int i=1;i<=step1;i++)
129             {
130                 //printf("id[%d]:%d\n",i,id[i]);
131                 if(que[id[i]].operation==0)
132                   printf("FILL(1)\n");
133                 if(que[id[i]].operation==1)
134                   printf("FILL(2)\n");
135                 if(que[id[i]].operation==2)
136                   printf("DROP(1)\n");
137                 if(que[id[i]].operation==3)
138                   printf("DROP(2)\n");
139                 if(que[id[i]].operation==4)
140                   printf("POUR(1,2)\n");
141                 if(que[id[i]].operation==5)
142                   printf("POUR(2,1)\n");
143             }
144         }
145         else
146             printf("impossible\n");
147     }
148     return 0;
149 }
View Code

 

转载于:https://www.cnblogs.com/sdau--codeants/p/3365030.html

相关文章:

  • .Net Remoting(分离服务程序实现) - Part.3
  • ssh架构简单解释和vo po解释
  • [NOIP2018 PJ T4]对称二叉树
  • 移动互联网时代的人才管理新思维之学习笔记
  • Codeforces Round #159 (Div. 2) B. Playing Cubes
  • Gridview常用技巧
  • Open xml 操作Excel 透视表(Pivot table)-- 实现Excel多语言报表
  • Delphi 数据类型列表
  • 在struts1.1框架下,利用smartupload实现文件的上传(可以是多个文件)
  • [转帖]三星F488E的JAVA安装方法
  • UICheckBox 用法解析
  • MySQL笔记系列:数据库概述
  • JOIN 和 WHERE?简单的问题也有学问。
  • 图像替换技术
  • WCF 第四章 绑定 创建一个自定义绑定
  • 【Leetcode】104. 二叉树的最大深度
  • 【跃迁之路】【519天】程序员高效学习方法论探索系列(实验阶段276-2018.07.09)...
  • cookie和session
  • Hibernate【inverse和cascade属性】知识要点
  • IP路由与转发
  • Java IO学习笔记一
  • JDK 6和JDK 7中的substring()方法
  • Linux后台研发超实用命令总结
  • mysql 数据库四种事务隔离级别
  • 分布式事物理论与实践
  • 腾讯优测优分享 | 你是否体验过Android手机插入耳机后仍外放的尴尬?
  • 新手搭建网站的主要流程
  • 责任链模式的两种实现
  • 正则表达式-基础知识Review
  • ​​​​​​​​​​​​​​汽车网络信息安全分析方法论
  • #HarmonyOS:软件安装window和mac预览Hello World
  • #pragam once 和 #ifndef 预编译头
  • #pragma once与条件编译
  • (a /b)*c的值
  • (delphi11最新学习资料) Object Pascal 学习笔记---第2章第五节(日期和时间)
  • (动手学习深度学习)第13章 计算机视觉---微调
  • (附源码)springboot猪场管理系统 毕业设计 160901
  • (简单) HDU 2612 Find a way,BFS。
  • (免费领源码)Java#ssm#MySQL 创意商城03663-计算机毕业设计项目选题推荐
  • (三) diretfbrc详解
  • (转)可以带来幸福的一本书
  • . Flume面试题
  • .NET 5种线程安全集合
  • .net生成的类,跨工程调用显示注释
  • /etc/fstab 只读无法修改的解决办法
  • ::前边啥也没有
  • :O)修改linux硬件时间
  • @RequestBody详解:用于获取请求体中的Json格式参数
  • @Transactional 竟也能解决分布式事务?
  • @我的前任是个极品 微博分析
  • [ 代码审计篇 ] 代码审计案例详解(一) SQL注入代码审计案例
  • [C++]二叉搜索树
  • [CentOs7]搭建ftp服务器(2)——添加用户
  • [DevEpxress]GridControl 显示Gif动画
  • [EULAR文摘] 脊柱放射学持续进展是否显著影响关节功能