隱式圖

整理代碼中,隱式圖的遍歷,不知道是什麼時候的代碼,也不知道有沒有ac

//
//  main.cpp
//  implicit graph-2
//
//  Created by Esion on 2017/3/24.
//  Copyright © 2017年 Esion. All rights reserved.
//
/*
 
Traversal of Implicit graph
QUESTION:
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’.
ANLYSIS

*/


#include <iostream>
#include <vector>
#define max 200
int tip[max][max],A_max,B_max,target,heard=0,last=0,step=0,script=0;
bool success=false;
struct node{
    int A;
    int B;
    int op;
    int step;
    int pre;
}arr[max*max];
void bfs(){
    node now,next;
    while(last>=heard){
    now=arr[heard];
    for(int i=0;i<=5;i++){
        if(i==0){             //fill(1)
            next.A=A_max;
            next.B=now.B;
        }
        if(i==1){             //fill(2)
            next.A=now.A;
            next.B=B_max;
        }
        if(i==2){             //Drop(1)
            next.A=0;
            next.B=now.B;
        }
        if(i==3){             //Drop(2)
            next.A=now.A;
            next.B=0;
        }
        if(i==4){             //pour(1,2)
            if(now.A>=B_max-now.B){
                next.A=now.A+now.B-B_max;
                next.B=B_max;
            }
            else{
                next.A=0;
                next.B=now.A+now.B;
            }
                
        }
        if(i==5){              //pour(2,1)
            if(now.B>=A_max-now.A){
                next.B=now.B+now.A-A_max;
                next.A=A_max;
            }
            else{
                next.B=0;
                next.A=now.A+now.B;
            }

        }
        if(tip[next.A][next.B]!=1){
            next.op=i;
            next.pre=heard;
            next.step=now.step+1;
            last++;
            arr[last]=next;
            tip[next.A][next.B]=1;
        }
        if(next.A==target||next.B==target){
            step=next.step;
            script=last;
            success=true;
            return ;
        }
    }
    heard++;
    }
}
void prin(int a){
    if(arr[a].pre!=0){
        prin(arr[a].pre);
    }
    switch (arr[a].op) {
        case 0:
            std::cout<<"FILL(1)"<<std::endl;
            break;
        case 1:
            std::cout<<"FILL(2)"<<std::endl;
            break;
        case 2:
            std::cout<<"DROP(1)"<<std::endl;
            break;
        case 3:
            std::cout<<"DROP(2)"<<std::endl;
            break;
        case 4:
            std::cout<<"ROUR(1,2)"<<std::endl;
            break;
        case 5:
            std::cout<<"POUR(2,1)"<<std::endl;
            break;
        default:
            break;
    }
}
int main(int argc, const char * argv[]) {
    while(std::cin>>A_max>>B_max>>target){
        heard=0;last=0;step=0;script=0;success=false;
    memset(tip,0,sizeof(tip));
    arr[heard].A=0;arr[heard].B=0;arr[heard].step=0;
    tip[0][0]=1;
    bfs();
    if(success==true){
        std::cout<<step<<std::endl;
        prin(script);
    }
    else{
        std::cout<<"impossible"<<std::endl;
    
    }
    }
    return 0;
}

点赞