[蓝桥杯2016初赛]卡片换位
发布日期:2021-05-15 07:31:17 浏览次数:17 分类:精选文章

本文共 3028 字,大约阅读时间需要 10 分钟。

BFS���������������������������������������������������������������������������������������������������������������������������������������������������A���������������B���������������������������������������������������������������������������������������������������������������������������������������������������������BFS������������������������������������������������������������������

���������������

  • ������������������������������������3x2���������������������������������������������������A���B���*������������������������������������������������������������������������

  • ���������������������������A���B���������������������*���������������������������������������������������

  • ���������������������������������������������������������������������������������������������������������������

  • ������������������������������������������������BFS������������������������������������������������������������������������������������������������������������

  • ���������������

    #include 
    #include
    #include
    #include
    #include
    using namespace std;
    int dir[4] = { -1, 1, 4, -4 };
    // ������������������
    struct node {
    int x;
    string s;
    };
    void bfs() {
    queue
    q; set
    visited; // ������������ string initial = " *A**B \n **B\nA B"; // ������������������������������������������������������ node start; start.x = 0; start.s = " *A**B "; q.push(start); visited.insert(start.s); while (!q.empty()) { node current = q.front(); q.pop(); // ��������������������������� if (isTarget(current.s)) { cout << current.cnt << endl; return; } // ������������������ for (int i = 0; i < 4; ++i) { string newState = current.s; int x = current.x + dir[i]; if (isValidPosition(x)) { // ��������������������������� char temp = newState[x]; newState[x] = newState[x + 1]; newState[x + 1] = temp; node nextNode; nextNode.s = newState; nextNode.cnt = current.cnt + 1; if (visited.count(newState)) continue; visited.insert(newState); q.push(nextNode); } } } // ���������������������������������-1��� // ������������������������������������ } // ��������������������������������������� bool isTarget(const string& state) { return state.find("AB") != string::npos && state.find("BA") != string::npos; } int main() { // ��������������������� // ��������������� vector
    states; // ������������������������ for (int i = 0; i < test_case_count; ++i) { states.push_back(���������); } for (auto& state : states) { bfs(); } return 0; }

    ���������

  • ���������������������������������������������������������������������������������

  • BFS������������������������������������������������������������������������������������������������������������

  • ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

  • ���������������������isTarget������������A���B���������������

  • ������������������������������������������������BFS���������������������������������������

    上一篇:[蓝桥杯2017初赛]迷宫
    下一篇:[蓝桥杯2016初赛]取球博弈C/C++

    发表评论

    最新留言

    哈哈,博客排版真的漂亮呢~
    [***.90.31.176]2025年05月06日 10时04分19秒