前言
本次填空题偏简单,编程大题有难(keng)度(die),4个钟,1个钟ak填空,3个钟写编程大题时间还是很紧张,算法以暴力为主,并没有时间想最优解。
试题 A: 美丽的 2
思路:签到题,简单模拟一下即可
#include<bits/stdc++.h>
using namespace std;
/*
答案:563
*/
bool is(int year) {
while (year) {
if (year % 10 == 2) return true;
year /= 10;
}
return false;
}
int main() {
int res = 0;
for (int i = 1; i <= 2020; i++) if (is(i)) res++;
cout << res << endl;
return 0;
}
试题 B: 扩散
读完题目,易知这是道bfs的模板题,从4个点开始,不断向四个方向扩散,唯一比较难办的是如何解决坐标为负数数组会越界的问题,比赛时我想的最直接的方法是将给定一个偏移量base,将坐标上的点进行整体的移动,但是另一种思路更加的简单,用哈希表的思想,将每个坐标映射成一个数且这个数是唯一的,然后可以用哈希表来维护出现过的坐标 ,虽然程序跑起来会比较长
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
typedef long long LL;
typedef pair<int, int> PII;
int dx[4] = {
0, -1, 0, 1};
int dy[4] = {
1, 0, -1, 0};
/*
答案:20312088
*/
// 映射函数f,系数可以随便取,原则是让每个坐标唯一对应一个数
LL f(LL u, LL v) {
return u * 2333333 + v;
}
LL bfs() {
queue<PII> q;
// unordered_set是c++11的东西,可以换成set,不过unordered_set的查找效率会比set高
unordered_set<LL> se;
// 先把四个点插入进来
q.push({
0, 0});
q.push({
2020, 11});
q.push({
11, 14});
q.push({
2000, 2000});
se.insert(f(0, 0));
se.insert(f(2020, 11));
se.insert(f(11, 14));
se.insert(f(2000, 2000));
int cnt = 0;
LL res = 4;
while (!q.empty()) {
int sz = q.size();
// bfs第i层对应经过了i分钟
for (int i = 1; i <= sz; i++) {
auto t = q.front();
q.pop();
for (int j = 0; j < 4