Algo-Trian
This commit is contained in:
48
Algorithm/Graph/BFS/P1443 马的遍历.cpp
Normal file
48
Algorithm/Graph/BFS/P1443 马的遍历.cpp
Normal 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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user