AtCoder Beginner Contest 176 (ABC水题,D题01BFS,E数组处理)
补题链接:Here
A - Takoyaki
很容易看出
B - Multiple of 9
给定一个很大的整数,问其是否是
累加各个位数,然后判断取余结果
C - Step
给定一个数组,每次操作可以给一个数增加
贪心地将当前数字加大到左边的最大值即可,如果当前数字大于最大值,则更新最大值。
D - Wizard in Maze
题意:有一个巫师在迷宫中,要从起点到终点。有两种走法:一是直接沿着路走,二是使用魔法,跳到以当前格子为中心的
思路:经典 01 BFS,沿着路走的代价为 0-1BFS
,也即双端队列来处理即可。
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const int inf = 0x3f3f3f3f;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int h, w, sx, sy, ex, ey;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
cin >> h >> w >> sx >> sy >> ex >> ey;
sx--, sy--, ex--, ey--;
vector<string> a(h);
for (int i = 0; i < h; ++i) cin >> a[i];
vector<vector<int>> dist(h, vector<int>(w, inf));
vector<vector<bool>> vis(h, vector<bool>(w, false));
deque<pair<int, int>> q;
q.push_back({sx, sy});
dist[sx][sy] = 0;
while (q.size()) {
auto f = q.front();
q.pop_front();
int ci = f.first, cj = f.second;
if (ci == ex && cj == ey) {
cout << dist[ci][cj] << "\n";
return 0;
}
if (vis[ci][cj]) continue;
vis[ci][cj] = true;
for (int k = 0; k < 4; ++k) {
int ni = ci + dy[k], nj = cj + dx[k];
if (ni < 0 || ni >= h || nj < 0 || nj >= w ||
dist[ni][nj] <= dist[ci][cj] || a[ni][nj] == '#')
continue;
dist[ni][nj] = dist[ci][cj];
q.push_front({ni, nj});
}
for (int ni = ci - 2; ni <= ci + 2; ++ni)
for (int nj = cj - 2; nj <= cj + 2; ++nj) {
if (ni < 0 || ni >= h || nj < 0 || nj >= w || a[ni][nj] == '#' ||
dist[ni][nj] <= dist[ci][cj] + 1)
continue;
dist[ni][nj] = dist[ci][cj] + 1;
q.push_back({ni, nj});
}
}
cout << -1 << "\n";
return 0;
}
E - Bomber
有一个
思路:
我们可以在读入的时候就维护好每行每列的目标个数,然后找到最大值的行列(可能有多个)
然后一次遍历这些点。
注意可能刚好炮塔落点的位置上有目标,然后就会重复计算一次
// Murabito-B 21/04/07
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int h, w, m;
cin >> h >> w >> m;
vector<int> hc(h + 1), wc(w + 1);
vector<pair<int, int>> p(m);
for (int i = 0; i < m; ++i) {
cin >> p[i].first >> p[i].second;
hc[p[i].first]++, wc[p[i].second]++;
}
int hm = *max_element(hc.begin(), hc.end());
int wm = *max_element(wc.begin(), wc.end());
vector<int> vh, vw;
for (int i = 1; i <= h; ++i)
if (hc[i] == hm) vh.emplace_back(i);
for (int i = 1; i <= w; ++i)
if (wc[i] == wm) vw.emplace_back(i);
int sh = vh.size(), sw = vw.size();
if (sh * sw > m) {
cout << hm + wm;
return 0;
}
set<pair<int, int>> s(p.begin(), p.end());
for (int i : vh)
for (int j : vw)
if (!s.count({i, j})) {
cout << hm + wm;
return 0;
}
cout << hm + wm - 1;
return 0;
}
F - Brave CHAIN
待补。。。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· 分享4款.NET开源、免费、实用的商城系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 全程不用写代码,我用AI程序员写了一个飞机大战
2020-04-08 LeetCode | 67. 二进制求和