第十三次CCF计算机软件能力认证

1、跳一跳

ACwing 3257

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int n, t = 0, res = 0;
    while (cin >> n, n) 
        if (n == 1) res++, t = 0;
        else t += 2, res += t;
    cout << res << endl;
    return 0;
}

2、碰撞的小球

ACwing3258

#include <iostream>
#include <algorithm>
using namespace std;

const int N = 110;

int n, L, T;
struct Ball {
    int p, v;
} b[N];

int main() {
    cin >> n >> L >> T;
    for (int i = 0; i < n; i++) {
        cin >> b[i].p; b[i].v = 1;
    }
    while (T--) {
        for (int i = 0; i < n; i++) {
            b[i].p += b[i].v;
            if (b[i].p == L || !b[i].p) 
                b[i].v *= -1;
        }
        for (int i = 0; i < n; i++)
            for (int j = i + 1; j < n; j++)
                if (b[i].p == b[j].p)
                    b[i].v *= -1, b[j].v *= -1;
    }
    for (int i = 0; i < n; i++) cout << b[i].p << " ";
    puts("");
    return 0;
}

3、URL映射

ACwing 3259

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;

const int N = 110;

int n, m;
struct Url {
    string path, name;
} url[N];

string get_number(string &str) { // 获取数字
    string res;
    for (auto c: str)
        if (isdigit(c)) res += c;
        else {
            res.clear(); return res;
        }
    int k = 0;
    while (k + 1 < res.size() && res[k] == '0') k++; // 去掉前导0,k+1是至少保留一个0
    return res.substr(k);
}

vector<string> get(string &path, string &str) {
    vector<string> res(1);
    int i, j;
    for (i = 1, j = 1; i < path.size() && j < str.size();) { // 跳过 '/'
        int u = i + 1, v = j + 1;
        while (u < path.size() && path[u] != '/') u++;
        while (v < str.size() && str[v] != '/') v++;
        string a = path.substr(i, u - i), b = str.substr(j, v - j);
        if (a == "<str>") {
            res.push_back(b);
            i = u + 1, j = v + 1;
        } else if (a == "<int>") {
            auto t = get_number(b);
            if (t.empty()) {
                res.clear(); return res;
            }
            res.push_back(t);
            i = u + 1, j = v + 1;
        } else if (a == "<path>") {
            res.push_back(str.substr(j)); return res;
        } else if (a != b) {
            res.clear(); return res;
        } else i = u + 1, j = v + 1;
    }
    if (i - path.size() != j - str.size()) res.clear(); // 防止url后面还有 '/'
    return res;
}

void work(string &str) {
    for (int i = 0; i < n; i++) { // 将输入的str和每一个url进行比较
        auto res = get(url[i].path, str); // 匹配成功
        if (res.size()) {
            cout << url[i].name;
            for (int j = 1; j < res.size(); j++) cout << " " << res[j];
            cout << endl;
            return;
        }
    }
    puts("404");
}

int main() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) cin >> url[i].path >> url[i].name;
    while (m--) {
        string str; cin >> str;
        work(str);
    }
    return 0;
}

4、棋局评估(最大最小搜索、 α β \alpha \beta αβ剪枝搜索)

ACwing 3260

使用DFS进行搜索,对于先手,搜索其画出图案后所能获得的最大值,对于后手,搜索其能画出图案后所能获得的最小值。

#include <iostream>
#include <algorithm>
using namespace std;

const int N = 3, INF = 1e8;

int g[N][N];

bool check(int x) {
    for (int i = 0; i < 3; i++) {
        int s = 0;
        for (int j = 0; j < 3; j++)
            if (g[i][j] == x) s++;
        if (s == 3) return true;
        s = 0;
        for (int j = 0; j < 3; j++)
            if (g[j][i] == x) s++;
        if (s == 3) return true;
    }
    if (g[0][0] == x && g[1][1] == x && g[2][2] == x) return true; // 对角线
    if (g[2][0] == x && g[1][1] == x && g[0][2] == x) return true;
    return false;
}

int evaluate() {
    int s = 0;
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            if (!g[i][j]) s++; // 空格数量
    if (check(1)) return s + 1; // 'X' 是否获胜
    if (check(2)) return -(s + 1); // 'O' 是否获胜
    if (!s) return 0; // 空格数量
    return INF;
}

int dfs(int u) {
    int t = evaluate(); // 评估当前棋局得分
    if (t != INF) return t;

    if (!u) { // Alice
        int res = -INF;
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                if (!g[i][j]) {
                    g[i][j] = 1;
                    res = max(res, dfs(1));
                    g[i][j] = 0;
                }
        return res;
    } else { // Bob
        int res = INF;
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                if (!g[i][j]) {
                    g[i][j] = 2;
                    res = min(res, dfs(0));
                    g[i][j] = 0;
                }
        return res;
    }
}

int main() {
    int T; cin >> T;
    while (T--) {
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                cin >> g[i][j];
        cout << dfs(0) << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值