【其他】一些很规范但是目前不太值得学习的代码
汪楚钧灌水区发布于 2026/10/11 20:3442 次浏览3 条回复已编辑
编辑历史(保留最近 5 版)
- 2026/10/11 20:36
#include <array>
#include <iostream>
#include <vector>
class Vector2 {
public:
int x = 0, y = 0;
constexpr Vector2 operator+(const Vector2 other) const {
return {x + other.x, y + other.y};
}
};
using TumorGraphLine = std::vector<unsigned short>;
using TumorGraph = std::vector<TumorGraphLine>;
constexpr int THRESHOLD = 50;
constexpr std::array<Vector2, 4> DIRS = {Vector2{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
bool is_border(const TumorGraph& tumor_graph, Vector2 pos) {
if (tumor_graph[pos.x][pos.y] > THRESHOLD) { return false; }
for (const auto dir : DIRS) {
Vector2 new_pos = pos + dir;
if (tumor_graph[new_pos.x][new_pos.y] > THRESHOLD) { return true; }
}
return false;
}
int calc_area(const TumorGraph& tumor_graph) {
int area = 0;
for (const auto& line : tumor_graph) {
for (const auto val : line) {
if (val <= THRESHOLD) { area++; }
}
}
return area;
}
int calc_perimeter(const TumorGraph& tumor_graph) {
int perimeter = 0;
for (int i = 1; i < tumor_graph.size() - 1; i++) {
for (int j = 1; j < tumor_graph.size() - 1; j++) {
if (is_border(tumor_graph, {i, j})) { perimeter++; }
}
}
return perimeter;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
TumorGraph tumor_graph(n + 2, TumorGraphLine(n + 2, 127));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
std::cin >> tumor_graph[i][j];
}
}
std::cout << calc_area(tumor_graph) << ' ' << calc_perimeter(tumor_graph) << std::endl;
return 0;
}回复(3)
一看到
std::我就难受
