比赛链接:AtCoder Beginner Contest 387
A - Happy New Year 2025
思路:模拟即可。
#include <bits/stdc++.h>
#define endl '\n'
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define LL long long
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
using namespace std;
int a,b;
int main()
{
cin >> a >> b;
cout << (a + b) * (a + b);
return 0;
}
B - 9x9 Sum
思路:模拟即可。
#include <bits/stdc++.h>
#define endl '\n'
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define LL long long
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
using namespace std;
int x;
int main()
{
cin >> x;
int ans = 0;
rep(i,1,9)
{
rep(j,1,9)
{
if(i*j!=x)
ans += i * j;
}
}
cout << ans;
return 0;
}
D - Snaky Walk
思路:广度优先搜索,特殊的地方在于每一层的拓展方向是垂直和水平交替的。我们通过用一个结构体来记录每个点拓展的方向,拓展出来的点的拓展方向则相反。
#include <bits/stdc++.h>
#define endl '\n'
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define LL long long
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
using namespace std;
int h, w;
pair<int, int> s,e; // 起点终点坐标
// 该状态的横坐标,纵坐标,该点下一步的方向,到该点的步数
//0是垂直,1是水平
struct state
{
int x, y, d, b;
};
queue<state> q;
bool vs[1005][1005][2], z[1005][1005];//记录访问和障碍
int bfs()
{
queue<state> q;
//由于第一步可以走任意方向,故将两种方向的状态都压入队列
q.push({s.first, s.second, 0, 0});
q.push({s.first, s.second, 1, 0});
vs[s.first][s.second][0] = 1;
vs[s.first][s.second][1] = 1;
while(!q.empty())
{
state u = q.front();//当前状态
q.pop();
if(u.x==e.first&&u.y==e.second)
{
return u.b;//返回走到当前状态的步数
}
for (int d = -1; d <= 1; d+=2)//每次只走一步
{
state ns = u;//拓展的新状态
if(u.d==0)//当前状态下一步的方向是垂直
ns.x += d;
else
ns.y += d;
ns.d = 1 - ns.d;//新状态下一步的方向要取反
ns.b++;//到新状态的步数+1
if(ns.x<1||ns.x>h||ns.y<1||ns.y>w)//越界判断
continue;
if (z[ns.x][ns.y]||vs[ns.x][ns.y][ns.d])//障碍和访问过判断
continue;
vs[ns.x][ns.y][ns.d] = 1;
q.push(ns);
}
}
return -1;
}
int main()
{
IOS;
cin >> h >> w;
rep(i, 1, h)
{
rep(j, 1, w)
{
char c;
cin >> c;
if (c == 'S')//记录起点
s = {i, j};
if (c == 'G')//记录终点
e = {i, j};
if (c == '#')//记录障碍
z[i][j] = 1;
}
}
cout << bfs();
return 0;
}