Algo-Trian

This commit is contained in:
e2hang
2025-09-21 12:21:50 +08:00
parent 24522486f1
commit 3bde00039c
25 changed files with 842 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
#include <climits>
using namespace std;
const int INF = INT_MAX >> 1;
int dx[] = {1, 2, 2, 1, -1, -2, -2, -1};
int dy[] = {2, 1, -1, -2, -2, -1, 1, 2};
bool check(int bx, int by, int x, int y) {
return (x >= 0 && x < bx && y >= 0 && y < by);
}
int main() {
int n, m, x, y;
cin >> n >> m >> x >> y;
vector<vector<int>> mp(n, vector<int>(m, INF));
x--; y--; // ת<><D7AA><EFBFBD><EFBFBD> 0-based
queue<tuple<int, int, int>> q;
q.push({x, y, 0});
mp[x][y] = 0;
while (!q.empty()) {
auto [qx, qy, path] = q.front();
q.pop();
for (int i = 0; i < 8; i++) {
int nx = qx + dx[i];
int ny = qy + dy[i];
if (check(n, m, nx, ny) && mp[nx][ny] == INF) {
mp[nx][ny] = path + 1;
q.push({nx, ny, path + 1});
}
}
}
for (auto &row : mp) {
for (auto v : row) {
cout << (v == INF ? -1 : v) << " ";
}
cout << "\n";
}
return 0;
}