mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-27 21:56:28 +00:00
P1443 马的遍历
R43073005
This commit is contained in:
parent
4f642b2ffc
commit
f8f79fc673
39
problem/P1443/P1443.cpp
Normal file
39
problem/P1443/P1443.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int n, m, x, y, vis[405][405];
|
||||
|
||||
const int dx[] = {0, -2, -2, -1, -1, +1, +1, +2, +2};
|
||||
const int dy[] = {0, -1, +1, -2, +2, -2, +2, -1, +1};
|
||||
|
||||
inline void bfs(int x, int y) {
|
||||
queue<pair<int, int>> q;
|
||||
q.push(make_pair(x, y));
|
||||
while (!q.empty()) {
|
||||
int xx = q.front().first;
|
||||
int yy = q.front().second;
|
||||
q.pop();
|
||||
for (int i = 1; i <= 8; i++) {
|
||||
int u = xx + dx[i];
|
||||
int v = yy + dy[i];
|
||||
if (u < 1 || u > n || v < 1 || v > m || vis[u][v] != -1) continue;
|
||||
q.push(make_pair(u, v));
|
||||
vis[u][v] = vis[xx][yy] + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
memset(vis, 0xff, sizeof(vis));
|
||||
scanf("%d%d%d%d", &n, &m, &x, &y);
|
||||
vis[x][y] = 0;
|
||||
bfs(x, y);
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= m; j++) {
|
||||
printf("%-5d", vis[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user