1 条题解

  • 0
    @ 2025-6-10 21:10:30

    题意分析

    题目要求计算立方体在一个彩色棋盘上按照指定顺序访问特定颜色格子的最少步数。棋盘包含红、绿、蓝、青、品红、黄各一个,白色若干,黑色若干。立方体初始位于白色格子,六个面颜色固定,滚动时顶面颜色需与目标格子颜色一致(白色除外)。需按顺序访问所有彩色格子,每个彩色格子只能访问一次,白色格子可重复访问,黑色格子不可访问。

    解题思路

    1)使用广度优先搜索(BFS)遍历所有可能状态;2)状态需记录位置、顶面颜色、已访问彩色格子;3)每次滚动时更新立方体朝向,检查是否满足颜色约束;4)最终返回按顺序访问所有彩色格子的最短路径长度。需注意:1)处理立方体滚动时的状态变化;2)剪枝优化避免无效搜索。

    #include<bits/stdc++.h>
    using namespace std;
    
    int t, n, tot, m;
    int cnt[1000000];
    string s;
    
    struct TRIE {
        int son[26];
        int num;
    } trie[500000];
    
    void build_trie(string& s) {
        int len = s.size();
        int position = 0;
    
        for (int i = 0; i < len; ++i) {
            if (!trie[position].son[s[i] - 'A'])
                trie[position].son[s[i] - 'A'] = ++tot;
            position = trie[position].son[s[i] - 'A'];
        }
    
        trie[position].num++;
    }
    
    void work() {
        for (int i = 1; i <= n; ++i)
            cin >> s, build_trie(s);
    
        for (int i = 0; i <= tot; ++i)
            if (trie[i].num)
                cnt[trie[i].num]++;
    
        for (int i = 1; i <= n; ++i)
            cout << cnt[i] << '\n';
    }
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(NULL), cout.tie(NULL);
    
        while (cin >> n >> m) {
            if (n == 0 && m == 0)
                break;
    
            memset(trie, 0, sizeof(trie));
            memset(cnt, 0, sizeof(cnt));
            tot = 0;
    
            work();
        }
    
        return 0;
    }
    
    • 1

    信息

    ID
    2936
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    1
    已通过
    1
    上传者