【每日一题】Pots

Pots

题意:

倒水,来回倒,自己看吧

题解:

参考《非常可乐》,原理挺简单的

个人问题:

我,TMD,一直WA,参考无数代码,还是WA,心态炸了!

代码:
///Pots(WA)
#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<queue>
#include<stdio.h>
#include<string.h>
using namespace std;
struct xiao {
    int x, y, k;
}now, well;
int a, b, s = 0, mink = 100,C;
int flag[100][100], brr[10][10], ans[100000], minn[100];
int BFS(int a,int b){
    memset(flag, 1, sizeof(flag));
    xiao A;
    A.x = 0;
    A.y = 0;
    A.k = 0;
    queue<xiao> Q;
    Q.push(A);
    flag[A.x][A.y] = 0;
    while (!Q.empty()) {
        now = Q.front();///处理第一个数据
        Q.pop();///抛弃第一个数据
        if (now.x == C || now.y == C)///判断是否到达目的地
            return now.k;
        for (int i = 1; i <= 6; i++) {
            if (i == 1) { well.x = a; well.y = now.y;}
            if (i == 2) { well.x = 0; well.y = now.y;}
            if (i == 3) { well.x = now.x - b + now.y; well.y = min(b,now.x+now.y);}
            if (i == 4) { well.x = now.x; well.y = b; }
            if (i == 5) { well.x = now.x; well.y = 0; }
            if (i == 6) { well.x = min(a, now.x + now.y); well.y = now.y - a + now.x; }
            well.k = now.k * 10 + i;
            if (well.x < 0) well.x = 0;
            if (well.y < 0) well.y = 0;
            if (flag[well.x][well.y]) {
                Q.push(well);
                flag[well.x][well.y] = 0;
            }
        }
    }
    return -1;
}
int main(){
        int x, y, u = 0;
        memset(ans, 0, sizeof(ans));
        scanf("%d %d %d", &x, &y,&C);
        x = BFS(x, y);
        if (x == -1) cout << "impossible" << endl; 
        while (x > 0) {
            ans[u] = x % 10;
            x /= 10;
            u++;
        }
        cout << u << endl;
        for (int i = u - 1; i >= 0; i--) {
            if (ans[i] == 1) printf("FILL(1)\n" );
            if (ans[i] == 2) printf("DROP(1)\n");
            if (ans[i] == 3) printf("POUR(1,2)\n");
            if (ans[i] == 4) printf("FILL(2)\n" );
            if (ans[i] == 5) printf("DROP(2)\n");
            if (ans[i] == 6) printf("POUR(2,1)\n");
        }
    return 0;
}
写在最后:

推荐两篇博客个人关于搜索的总结关于这个题一位同学的题解

发布了41 篇原创文章 · 获赞 16 · 访问量 1462

猜你喜欢

转载自blog.csdn.net/weixin_43824551/article/details/104932519