米哈游笔试题2021-9-12(偏ACM风格)

⽶哈游笔试题2021-9-12(偏ACM风格)⼀. 括号匹配加贪⼼
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int q; cin >> q;
stack<int> stk;
while (q -- ) {
string s; cin >> s;
int res = 0;
for (char c: s) {
if (c == '{') stk.push(0);
else if (c == '[') stk.push(1);
else {
int t = p(); stk.pop();
if (t == 0 && c == ']' || t == 1 && c == '}') res ++ ;
}
}
cout << res << endl;
}
return 0;
}
⼆. 数论规律数学题
#include <bits/stdc++.h>
using namespace std;
吉尔达鞋业int main() {
ios::sync_with_stdio(false);
内部质量保证体系cin.tie(0);
int q; cin >> q;
while (q -- ) {
int x; cin >> x;
x /= 3; // 12 -> 4 (1, 1, 2) (1, 2, 1) ...工业萘
int res = 0;
for (int i = 1; i <= x - 2; i ++ ) {
int d = x - i;
res += d - 1;
}
cout << res << endl;
}
return 0;
}
三. 棋盘BFS题⽬
树的同构#include <bits/stdc++.h>
using namespace std;阈值电压
typedef pair<int, int> PII;
int g[12][12], a, b, c, d, n = 10, m = 9;
bool st[12][12];
int dx[8] = {-2, -3, -3, -2, 2, 3, 3, 2};
int dy[8] = {-3, -2, 2, 3, 3, 2, -2, -3};
int k1x[8] = {0, -1, -1, 0, 0, 1, 1, 0};
int k1y[8] = {-1, 0, 0, 1, 1, 0, 0, -1};
int k2x[8] = {-1, -2, -2, -1, 1, 2, 2, 1};
int k2y[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
int bfs() {稀疏化
queue<PII> q;
q.push({a, b});
st[a][b] = true;
int lev = 0;
while (q.size()) {
int len = q.size();
for (int i = 0; i < len; i ++ ) {
auto& p = q.front(); q.pop();
int x = p.first, y = p.second;
if (x == c && y == d)
return lev;
for (int j = 0; j < 8; j ++ ) {
int _a = x + dx[j], _b = y + dy[j];
int k1a = x + k1x[j], k1b = y + k1y[j];
int k2a = x + k2x[j], k2b = y + k2y[j];
if (_a < 0 || _a >= n || _b < 0 || _b >= m ||
st[_a][_b] || (k1a == c && k1b == d) || (k2a == c && k2b == d))      continue;
q.push({_a, _b});
st[_a][_b] = true;
}
}
lev ++ ;
}
return -1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> a >> b >> c >> d;
cout << bfs() << endl;
return 0;
}

本文发布于:2024-09-21 01:19:33,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/471008.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:规律   括号   质量   数论   匹配   保证体系   数学题
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议