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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • pycharm 替换快捷键_pycharm到包快捷键

    pycharm 替换快捷键_pycharm到包快捷键查找/替换(Search/Replace)F3下一个Shift+F3前一个Ctrl+R替换(需要同时替换的变量名等)Ctrl+Shift+F整个项目中全局查找Ctrl+Shift+R整个项目中全局替换

    2022年8月26日
    5
  • pycharm怎么切换到vim编辑模式

    pycharm怎么切换到vim编辑模式首先 进行打开一个 pycharm 的编辑的代码的界面当中 进行点击 tools 的菜单 进行点击了 tools 的菜单之后 弹出了下拉菜单选中为 vimemulator 的选项 切换完成之后 的可以鼠标点击会显示了一个黑色块的 需要的 vim 的命令方式来操作的 进行再键盘中按 i 才能输入 可以看到的就输入的信息了 编辑的完成的按 esc 来退出的输入的操作

    2025年7月11日
    2
  • navicat mac 激活【2021最新】

    (navicat mac 激活)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

    2022年3月31日
    248
  • Oracle保留两位小数_java中怎么保留小数点后两位

    Oracle保留两位小数_java中怎么保留小数点后两位在最近的项目开发中,有个业务需求是界面显示的数字需要保留两位小数,目前我想到的解决方法有两种:(1)在写SQL的时候,直接保留两位小数(2)在java代码里面将查询出来的数进行格式化处理,保留两位小数先说第一种方案:在SQL中的处理我使用的oracle数据库,所以有3个函数可以选择,分别是:(1)ROUND(A/B,2)ROUND()函数是会将计算结果进行四舍五入的,如果所需

    2022年9月24日
    2
  • a标签下划线

    a标签下划线页面中有一处box中的a标签都被加上了下划线,查找元素却没有找到css中的underline。原因是<a>标签默认是有下划线的,而一般看到的<a>标签链接中的下划线都被覆盖掉了,所以误以为<a>标签的默认状态是没有下划线的,出现下划线是有css另外渲染的。其实下划线才是<a>标签的默认状态在head中加上下面一段覆盖掉<…

    2022年5月2日
    154
  • hackbar工具安装使用教程

    hackbar工具安装使用教程HackBar工具介绍HackBar是一个浏览器上的一个插件,包含一些黑客常用的工具,比如SQLinjection,XSS,加密等!免费版下载百度网盘:https://pan.baidu.com/s/1WBT6iqx9ZRSbCRbGWUfvvA提取码:1234免费版安装:按F12打开hackbar界面…

    2022年6月14日
    151

发表回复

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

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