acommonman @ 2024-09-09 21:21:22
#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
int chest[500][500];
bool vis[500][500];
int n, m,dx,dy, move1[8][2] = { {1,-2},{-1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1},{-2,-1} };
#define CHECK(x,y) (x <= n && y <= m && x > 0 && y > 0)
queue<int>qx,qy;
int main(){
cin >> n >> m>>dx>>dy;
vis[dx][dy] = true;
qx.push(dx), qy.push(dy);
while (!qx.empty()) {
for (int i = 0; i < 8; i++) {
int x = qx.front() + move1[i][0], y = qy.front() + move1[i][1];
if (CHECK(x, y) && !vis[x][y]) {
vis[x][y] = true;
chest[x][y] = chest[qx.front()][qx.front()] + 1;
qx.push(x),qy.push(y);
}
}
qx.pop(), qy.pop();
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if ((i != dx || j != dy) && !vis[i][j])cout << -1 << " ";
else cout << chest[i][j] << " ";
}
cout << endl;
}
}
by xyx404 @ 2024-09-09 21:46:53
@acommonman
#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
int chest[501][501];
bool vis[501][501];
int n, m,dx,dy, move1[8][2] = { {1,-2},{-1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1},{-2,-1} };
#define CHECK(x,y) (x <= n && y <= m && x > 0 && y > 0)
queue<int>qx,qy;
int main(){
cin >> n >> m>>dx>>dy;
vis[dx][dy] = true;
qx.push(dx), qy.push(dy);
while (!qx.empty()) {
for (int i = 0; i < 8; i++) {
int x = qx.front() + move1[i][0], y = qy.front() + move1[i][1];
if (CHECK(x, y) && !vis[x][y]) {
vis[x][y] = true;
chest[x][y] = chest[qx.front()][/*qx*/qy.front()] + 1;// 改
qx.push(x),qy.push(y);
}
}
qx.pop(), qy.pop();
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if ((i != dx || j != dy) && !vis[i][j])cout << -1 << " ";
else cout << chest[i][j] << " ";
}
cout << endl;
}
}
by henry_jfsun @ 2024-09-09 21:51:12
输出改成这个试试
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++)printf("%-5d",chest[i][j][j]);printf("\n");
}
by acommonman @ 2024-09-10 22:40:20
@xyx404 啊啊啊谢谢我眼瞎了