清华大学上机题

编程入门 行业动态 更新时间:2024-10-10 13:23:42

<a href=https://www.elefans.com/category/jswz/34/1757004.html style=清华大学上机题"/>

清华大学上机题

题目链接

#include <iostream>
#include <string>
#include <queue>
#include <unordered_map>
using namespace std;struct node {string s;int cnt;node(string x, int y) : s(x), cnt(y) {};
};
unordered_map<string, bool> mp;
int n;bool check(string s) {if (s.size() < 4) return false;for (int i = 0; i < s.size(); i++) {string str = s.substr(i, 4);if (str == "2012") return true;}return false;
}void bfs(string s) {queue<node> q;node t(s, 0);q.push(t);mp[s] = true;while (!q.empty()) {node tmp = q.front();q.pop();if (check(tmp.s)) {cout << tmpt << endl;return;}for (int i = 0; i < tmp.s.size() - 1; i++) {string str = tmp.s;swap(str[i], str[i + 1]);if (mp.count(str) == 0) {mp[str] = true;q.push(node(str, tmpt + 1));}}}cout << -1 << endl;
}int main() {
#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);
#endifstring s;cin >> n >> s;bfs(s);return 0;
}

更多推荐

清华大学上机题

本文发布于:2024-03-10 13:15:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1728074.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:清华大学   上机题

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!