WUSTACM2021暑假集训日记:7.9

7.9

决定还是先一口气把简单搜索给搞完;

中午的时候联通装宽带的师傅来了,宿舍终于有网用了!

Kuangbin:简单搜索

A. 非常可乐(HDU_1495)

传送门

​ bfs,搜索方向有六个,就是这仨杯子互相倒水的六种情况,结束条件是出现了至少有一个杯子里的可乐是S / 2,这里需要注意一下两种情况,一是有且只有一个杯子是S / 2,二是刚好有两个杯子是S / 2,对于第一种情况,要满足题目条件还需要再走一步把两个不是S / 2的杯子合并,第二种情况直接就是满足的;

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <algorithm>
#define ll long long
#define mms(a, b) memset(a, b, sizeof(a))
using namespace std;

const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int MAXN = 100 + 5;

int S, N, M, COKE[3];
int dir[6][2] = { {0, 1}, {1, 0}, {0, 2}, {2, 0}, {1, 2}, {2, 1} };
bool vis[MAXN][MAXN][MAXN];
struct Node {
    int coke[3];
    int stp;
};

void bfs() {
    queue<Node> q;
    q.push(Node{ {S, 0, 0}, 0 });
    vis[S][0][0] = true;

    while (!q.empty()) {
        Node t = q.front();
        q.pop();

        int cnt = 0;
        for (int i = 0; i < 3; i++) {
            if (t.coke[i] == S / 2)
                cnt++;
        }
        if (cnt) {
            if (cnt == 2) {
                cout << t.stp << '\n';
                return;
            }
            else {
                cout << t.stp + 1 << '\n';
                return;
            }
        }

        for (int i = 0; i < 6; i++) {
            int from = dir[i][0], to = dir[i][1];
            int pour = min(COKE[to] - t.coke[to], t.coke[from]);
            if (!pour)
                continue;
            Node mid = t;
            mid.coke[from] -= pour;
            mid.coke[to] += pour;
            ++mid.stp;

            if (!vis[mid.coke[0]][mid.coke[1]][mid.coke[2]]) {
                vis[mid.coke[0]][mid.coke[1]][mid.coke[2]] = true;
                q.push(mid);
            }
        }
    }

    cout << "NO" << '\n';
}

void solve() {
    if (S & 1) {
        cout << "NO" << '\n';
        return;
    }
    bfs();
}

void init() {
    COKE[0] = S;
    COKE[1] = N;
    COKE[2] = M;
    mms(vis, false);
}

signed main() {
    std::ios::sync_with_stdio(false);
    /* cin.tie(nullptr);
    cout.tie(nullptr); */

    while (cin >> S >> N >> M) {
        if (S == 0 && N == 0 && M == 0) {
            break;
        }
        init();
        solve();
    }

    return 0;
}

B. Find a way(HDU_2612)

传送门

​ 双源bfs,两个二维数组存两个人到每一个格子所需的步数,然后把俩人到同一个KFC的步数加起来,求出所有中的最小值就是答案;

​ 这里要注意存在有到不了KFC要考虑,我在这里wa了一发,因为两个二维数组是用0去初始化的,导致这种到不了的KFC反而是作为最小值0输出,所以这里两个数组应该用INF去初始化,这样就不会有问题了;

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <algorithm>
#define ll long long
#define mms(a, b) memset(a, b, sizeof(a))
using namespace std;

const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int MAXN = 200 + 5;

int n, m;
int Map[MAXN][MAXN][2];
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
struct Node {
    int x, y, stp;
}Y, M;
vector<Node> K;

bool jdg(int x, int y) {
    return x >= 1 && x <= n && y >= 1 && y <= m;
}

void bfs(Node start, int h) {
    bool vis[MAXN][MAXN];
    mms(vis, false);
    queue<Node> q;
    q.push(start);
    vis[start.x][start.y] = true;

    while(!q.empty()) {
        Node now = q.front();
        q.pop();

        for(int i = 0; i < 4; ++i) {
            int x = now.x + dir[i][0], y = now.y + dir[i][1], stp = now.stp + 1;
            if(jdg(x, y) && !vis[x][y] && Map[x][y][h] != -1) {
                Map[x][y][h] = stp;
                vis[x][y] = true;
                q.push(Node{x, y, stp});
            }
        }
    }
}

void solve() {
    int ans = INF;

    bfs(Y, 0);
    bfs(M, 1);

    for(auto it : K) {
        ans = min(ans, Map[it.x][it.y][0] + Map[it.x][it.y][1]);
    }
    cout << ans * 11 << '\n';
}

void init() {
    mms(Map, INF);
    K.clear();
}

signed main() {
    std::ios::sync_with_stdio(false);
    /* cin.tie(nullptr);
    cout.tie(nullptr); */

    while(cin >> n >> m) {
        init();
        for(int i = 1; i <= n; ++i) {
            for(int j = 1; j <= m; ++j) {
                char ch;
                cin >> ch;
                if(ch == 'Y') {
                    Y = Node{i, j, 0};
                    Map[i][j][0] = 0;
                }
                else if(ch == 'M') {
                    M = Node{i, j, 0};
                    Map[i][j][1] = 0;
                }
                else if(ch == '#') {
                    Map[i][j][0] = -1;
                    Map[i][j][1] = -1;
                }
                else if(ch == '@') {
                    K.push_back(Node{i, j, 0});
                }
            }
        }
        solve();
    }

    return 0;
}

C. Shuffle’m Up(POJ_3087)

传送门

​ 虽然这题是在搜索专题里面,但是我感觉就是一个模拟题,直接模拟题目中的过程,满足条件就输出,这里我用一个mao<string, int>来判断组合出来的串是否出现过,如果出现过就说明无法满足条件;

​ 之前没用过substr这个函数,感觉用来求子串很方便;

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <map>
#include <queue>
#include <vector>
#include <cmath>
#include <algorithm>
#define ll long long
#define mms(a, b) memset(a, b, sizeof(a))
using namespace std;

const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int MAXN = 200 + 5;

int n, Case = 0;
string s1, s2, s12, s;
map<string, int> mp;

void solve() {
    int ans = 0;

    while(s != s12) {
        if(s != "") {
            s1 = s.substr(0, n);
            s2 = s.substr(n, n);
        }

        s.clear();
        for(int i = 0; i < n; ++i) {
            s += s2[i];
            s += s1[i];
        }
        ans++;

        if(mp[s]) {
            ans = -1;
            break;
        }
        else {
            mp[s]++;
        }
    }

    cout << ++Case << ' ' << ans << '\n';
}

void init() {
    s = "";
    mp.clear();
}

int main() {
    std::ios::sync_with_stdio(false);

    int t;
    cin >> t;
    while(t--) {
        cin >> n;
        cin >> s1 >> s2 >> s12;
        init();
        solve();
    }
}

D. Pots(POJ_3414)

传送门

​ bfs,搜索方向有六个,水都倒掉 * 2,杯子装满 * 2, 两个杯子互相倒 * 2;

​ 这个题代码写起来有点长,因为我暂时没想出来怎么把这六个搜索方向放在一个循环里或者简化掉,所以用了六个if-else来模拟这六个过程;

​ 这个题还要用到dfs来输出操作步骤,跟之前POJ_3984的迷宫问题是一个操作;

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <map>
#include <queue>
#include <vector>
#include <cmath>
#include <algorithm>
#define ll long long
#define mms(a, b) memset(a, b, sizeof(a))
using namespace std;

const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int MAX = 100 + 5;

int A, B, C, ans;
string str[6] = { "FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)" };
bool vis[MAX][MAX]; //记录a,b中水的状态,如果有出现过的状态就不入队
struct Node {
    int a, b;       //两个杯子中水的多少
    int stp, pre, pos, op;   
    //stp是所经历的步骤,pre是上一个Node位置,pos是当前位置,op是使用了什么操作
};
vector<Node> vec;

int bfs() {
    int pos = 0;
    Node start = Node{ 0, 0, 0, -1, 0, -1 };
    queue<Node> q;
    q.push(start);
    vec.push_back(start);
    vis[0][0] = true;
    while (!q.empty()) {
        Node now = q.front();
        q.pop();
        for (int i = 1; i <= 6; ++i) {
            int op = -1;
            int a = now.a, b = now.b, stp = now.stp;
            if (i == 1) {
                a = A;
                op = 0;
            }
            else if (i == 2) {
                b = B;
                op = 1;
            }
            else if (i == 3) {
                a = 0;
                op = 2;
            }
            else if (i == 4) {
                b = 0;
                op = 3;
            }
            else if (i == 5) {
                int pour = min(a, B - b);
                a -= pour;
                b += pour;
                op = 4;
            }
            else if (i == 6) {
                int pour = min(b, A - a);
                b -= pour;
                a += pour;
                op = 5;
            }

            if (!vis[a][b]) {
                vis[a][b] = true;
                ++stp;
                ++pos;
                int pre = now.pos;
                Node tmp = { a, b, stp, pre, pos, op };
                q.push(tmp);
                vec.push_back(tmp);
                if (a == C || b == C) {
                    ans = tmp.stp;
                    return tmp.pos;
                }
            }
        }
    }
    return -1;
}

void dfs(Node t) {
    if (t.pre == -1) {
        return;
    }

    dfs(vec[t.pre]);
    cout << str[t.op] << '\n';
}

void solve() {
    int flag = bfs();
    if (flag == -1) {
        cout << "impossible" << '\n';
        return;
    }
    else {
        cout << ans << '\n';
    }

    dfs(vec[flag]);
}

void init() {
    ans = 0;
    mms(vis, false);
    vec.clear();
}

int main() {
    std::ios::sync_with_stdio(false);

    while(cin >> A >> B >> C) {
        init();
        solve();
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Luther_w

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值