# AtCoder Beginner Contest 176 (ABC水题，D题01BFS，E数组处理)

## D - Wizard in Maze

#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

posted @ 2021-04-08 20:13  Murabito-B  阅读(6)  评论(0编辑  收藏