From f8f79fc67369cd021a26bbf338bd18cff594fd89 Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Thu, 3 Dec 2020 09:53:57 +0800 Subject: [PATCH] =?UTF-8?q?P1443=20=E9=A9=AC=E7=9A=84=E9=81=8D=E5=8E=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit R43073005 --- problem/P1443/P1443.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 problem/P1443/P1443.cpp diff --git a/problem/P1443/P1443.cpp b/problem/P1443/P1443.cpp new file mode 100644 index 00000000..4fcd09c7 --- /dev/null +++ b/problem/P1443/P1443.cpp @@ -0,0 +1,39 @@ +#include + +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> 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; +}