Poj3414广泛搜索

Poj3414广泛搜索

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

<span style="color:#330099;">/*
D - D
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit
 
Status
 
Practice
 
POJ 3414
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)
By Grant Yuan
2014.7.14
poj 3414
广搜
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
bool flag=0;
int next[6]={0,1,2,3,4,5};
int a,b,c;
int aa,bb,cc;
typedef struct{
  int a;
  int b;
  int f;
  int sum;
  int ope;
}node;
int res;
node q[10000];
bool mark[101][101];
int top,base;
int top1;
int s[10000];
bool can(int x1,int y1)
{
    if(x1>=0&&x1<=aa&&y1>=0&&y1<=bb&&mark[x1][y1]==0)
      return 1;
    return 0;
}

void slove()
{   int a1,b1,f1,a2,b2;
    while(top>=base){//cout<<"zhang"<<endl;
     if(q[base].a==cc||q[base].b==cc){
          flag=1;
           res=q[base].sum;
           break;
          }
      for(int i=0;i<6;i++){
          if(i==0)
            {  a1=aa;
               b1=q[base].b;
               if(can(a1,b1)){
                q[++top].a=a1;
                q[top].b=b1;
                q[top].f=base;
                q[top].sum=q[base].sum+1;
                q[top].ope=i;
                mark[a1][b1]=1;}

            }
            else if(i==1)
            {
               a1=q[base].a;
               b1=bb;
               if(can(a1,b1)){
                q[++top].a=a1;
                q[top].b=b1;
                q[top].f=base;
                q[top].sum=q[base].sum+1;
                q[top].ope=i;
                mark[a1][b1]=1;}
            }
            else if(i==2)//1dao2
            {  int m,n;
               m=q[base].a;
               n=bb-q[base].b;
               if(m>=n){
                   a1=m-n;
                   b1=bb;
                if(can(a1,b1)){
                q[++top].a=a1;
                q[top].b=b1;
                q[top].f=base;
                q[top].sum=q[base].sum+1;
                q[top].ope=i;
                mark[a1][b1]=1;} }
                else{
                  a1=0;
                  b1=m+q[base].b;
                  if(can(a1,b1)){
                q[++top].a=a1;
                q[top].b=b1;
                q[top].f=base;
                q[top].sum=q[base].sum+1;
                q[top].ope=i;
                mark[a1][b1]=1;}
                  }}
            else if(i==3)//1dao2
            {  int m,n;
               m=aa-q[base].a;
               n=q[base].b;
               if(n>=m){
                   a1=aa;
                   b1=n-m;
                if(can(a1,b1)){
                q[++top].a=a1;
                q[top].b=b1;
                q[top].f=base;
                q[top].sum=q[base].sum+1;
                q[top].ope=i;
                mark[a1][b1]=1;} }
                else{
                  b1=0;
                  a1=n+q[base].a;
                  if(can(a1,b1)){
                q[++top].a=a1;
                q[top].b=b1;
                q[top].f=base;
                q[top].sum=q[base].sum+1;
                q[top].ope=i;
                mark[a1][b1]=1;}
                  }}
               else if(i==4)
               {
                   a1=0;
                   b1=q[base].b;
                   if(can(a1,b1)){
                    q[++top].a=a1;
                    q[top].b=b1;
                    q[top].f=base;
                    q[top].sum=q[base].sum+1;
                     q[top].ope=i;
                     mark[a1][b1]=1;
                    }}
                else if(i==5)
               {
                   b1=0;
                   a1=q[base].a;
                   if(can(a1,b1)){
                    q[++top].a=a1;
                    q[top].b=b1;
                    q[top].f=base;
                    q[top].sum=q[base].sum+1;
                    q[top].ope=i;
                     mark[a1][b1]=1;
                    }
               }

        }
        base++;
}}
void print()
{   top1=-1;
    int i=base,j;
    while(1){
        s[++top1]=q[i].ope;
        j=q[i].f;
        i=j;
        if(i==0)
          break;
          }
    for(j=top1;j>=0;j--)
    {
        if(s[j]==0)
           cout<<"FILL(1)"<<endl;
        else
          if(s[j]==1)
           cout<<"FILL(2)"<<endl;
        else
           if(s[j]==2)
            cout<<"POUR(1,2)"<<endl;
        else
          if(s[j]==3)
            cout<<"POUR(2,1)"<<endl;
        else
          if(s[j]==4)
            cout<<"DROP(1)"<<endl;
        else
          if(s[j]==5)
            cout<<"DROP(2)"<<endl;
    }
}
int main()
{
    cin>>aa>>bb>>cc;
    top=-1;
    memset(mark,0,sizeof(mark));
    mark[0][0]=1;
    base=0;
    q[++top].a=0;
    q[top].b=0;
    q[top].f=0;
    q[top].sum=0;
    q[top].ope=0;
    slove();
    if(flag==0) cout<<"impossible"<<endl;
    else{cout<<res<<endl;
    print();}
    return 0;

}
</span>

版权声明:本文博客原创文章。博客,未经同意,不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117579.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • cocos2d3.0 Scale9Sprite

    cocos2d3.0 Scale9Sprite

    2021年11月15日
    37
  • vs中如何让所有控件居中_android自定义控件

    vs中如何让所有控件居中_android自定义控件如何让一个控件在另一个控件下面,直接操作下面代码:<LinearLayoutandroid:id=”@+id/ly_dialogPersonCode_Title”android:layout_width=”match_parent”android:layout_height=”match_parent”android:layout_marginT…

    2025年6月13日
    0
  • TiKV学习笔记

    TiKV学习笔记一、TiKV简介1.1、TiKV概述TiDB的存储用的TiKV,TiKV是基于RocksDB实现了分布式(可水平扩展,支持主从),RocksDB是对单机版LevelDB的封装。TiKV是开源的、分布式、支持事务的VK数据库。TiKV不仅提供了KV的API,且提供了兼容ACID的API。TiKV用Rust语言实现,用采用Raft协议,作为TiDB的存储层,是一个兼容了MySQL协议的分布式HTAP数据库。TiKV保证数据一致性,TiKV基于Rust语言实现了Raft协议,一致性状态存储在Rock

    2022年9月24日
    0
  • c++中constexpr_define和const定义常量的区别

    c++中constexpr_define和const定义常量的区别常量表达式是指值不会改变且在编译过程中就能够得到计算结果的表达式,能在编译时求值的表达式。例1:#include&lt;iostream&gt;usingnamespacestd;intmain(){ constinta1=10;//a1是常量表达式。 constinta2=a1+20;//a2是常量表达…

    2022年9月26日
    0
  • 爬虫案例分析_爬虫爬取司法案例

    爬虫案例分析_爬虫爬取司法案例小番在这里这里涉及了一些反爬手段与方法,老铁们赶紧拿起小板凳开始听了哦爬取思路:打开一个番剧,找到播放页面,开始F12检查元素发现直接跳回首页了,这就没法用浏览器自带的抓包了。可以使用抓包工具fiddle进行抓包。先不急着抓包,先看看播放页的源代码view-source:www.zzzfun.com/vod_play_id_2029_sid_1_nid_1.html可以得到每一话的链接,那么还少了视频链接,网页源代码里面没有,只能抓包了。可以发现debug调试时跳转的代码,在右下角窗口

    2022年8月23日
    4
  • 社区打造智慧小区_idc智能化解决方案

    社区打造智慧小区_idc智能化解决方案智慧社区建设方案丨智慧小区智能化解决方案随着物联网技术和我国新一代互联网技术的发展,未来社区网络将会实现全覆盖,通过社区网络和物联网络,将会实现社区机电设备和住宅的自动化,智能化,实现远程监控和网络数字化。智慧社区是社区综合服务管理的一种创新,利用前沿的智能化基础设施建设,增强社区治理和小区管理智能化,推动便民措施服务项目智能化,使社区居民的衣食住行更为舒服、高效率。智慧社区概念介绍:智慧社区是指充分利用物联网、云计算、移动互联网等新一代信息技术的集成应用,涉及到智能楼…

    2022年10月18日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号