[kuangbin带你飞]专题一 简单搜索 M - 非常可乐

题目:[kuangbin带你飞]专题一 简单搜索 M - 非常可乐


思路

  • bfs没什么特别的,注意目标条件,奇数肯定不能平分。
  • 输入顺序搞错了,wa了。
  • 忘记重置了 wa了一次
  • 搜索过程写的有点麻烦,用数组记录3个容器,然后再开一个对应编号的数组储存最大容量,代码量会少很多,但是懒得写了

AC代码

#include
#include
#include
#include 
using namespace std;
const int MAXN = 110;
int A,B,S; 
bool vis[MAXN][MAXN][MAXN];
struct node{
    int abot;
    int bbot;
    int sbot;
    int steps;
};

node pour(node nownode,int i){
    node nextnode;
    nextnode = nownode;
    switch(i){
        case 0://a倒b 
            if(nownode.abot+nownode.bbot <=B){
                nextnode.abot = 0;
                nextnode.bbot = nownode.abot + nownode.bbot;
            }else{
                nextnode.abot = nownode.abot + nownode.bbot -B;
                nextnode.bbot = B;
            }
            break;
        case 1://b倒a 
            if(nownode.abot+nownode.bbot <=A){
                nextnode.bbot = 0;
                nextnode.abot = nownode.abot + nownode.bbot;
            }else{
                nextnode.bbot = nownode.abot + nownode.bbot -A;
                nextnode.abot = A;
            }
            break;
        case 2://a倒s 
            if(nownode.abot+nownode.sbot <=S){
                nextnode.abot = 0;
                nextnode.sbot = nownode.abot + nownode.sbot;
            }else{
                nextnode.abot = nownode.abot + nownode.sbot -S;
                nextnode.sbot = S;
            }
            break;
        case 3://b到s 
            if(nownode.abot+nownode.bbot <=S){
                nextnode.bbot = 0;
                nextnode.sbot = nownode.sbot + nownode.bbot;
            }else{
                nextnode.bbot = nownode.sbot + nownode.bbot -S;
                nextnode.sbot = S;
            }
            break;
        case 4://s倒b 
            if(nownode.sbot+nownode.bbot <=B){
                nextnode.sbot = 0;
                nextnode.bbot = nownode.sbot + nownode.bbot;
            }else{
                nextnode.sbot = nownode.sbot + nownode.bbot -B;
                nextnode.bbot = B;
            }
            break;
        case 5://s倒a 
            if(nownode.abot+nownode.sbot <=A){
                nextnode.sbot = 0;
                nextnode.abot = nownode.abot + nownode.sbot;
            }else{
                nextnode.sbot = nownode.abot + nownode.sbot -A;
                nextnode.abot = A;
            }
            break;
    }
    return nextnode;
    
    
}

bool test(node nownode){
    if(nownode.abot==S/2)
        if(nownode.bbot == S/2)
            return true;
        else if(nownode.sbot == S/2)
             return true;
    if(nownode.bbot == S/2)
        if(nownode.bbot == nownode.sbot)
            return true;
    return false;
}

int bfs(node st){
    queue Q;
    Q.push(st);
    vis[st.abot][st.bbot][st.sbot] = true;
    while(!Q.empty()){
        node nownode;
        node nextnode;
        nownode = Q.front();Q.pop();
        if(test(nownode)){
//          cout<

你可能感兴趣的:([kuangbin带你飞]专题一 简单搜索 M - 非常可乐)