蒙自市凤凰小学又阝十尃亻二 • 1年前
#include <bits/stdc++.h>
using namespace std;
struct Pos {
int x, y, cost = 0;
Pos(int ax = 0, int ay = 0, int acost = 0) {
x = ax, y = ay, cost = acost;
}
};
int n, m, a, b;
bool isok, vis[405][405];
int ans[405][405];
void bfs(int o, int p, int cost) {
queue <Pos> q;
q.push(Pos(a, b, 0));
while (!q.empty()) {
Pos now = q.front();
q.pop();
int x = now.x, y = now.y, cost = now.cost;
if (x < 1 || x > n) // 处理越界,如果 x 不在 [1,n] 范围内
continue;
if (y < 1 || y > m) // 处理越界,如果 x 不在 [1,n] 范围内
continue;
if (vis[x][y])
continue;
vis[x][y] = true;
/*if (x == o && y == p) {
printf("%-5d", cost);
isok = true;
return;
}*/
ans[x][y] = cost;
q.push(Pos(x + 1, y + 2, cost + 1));
q.push(Pos(x - 1, y + 2, cost + 1));
q.push(Pos(x + 1, y - 2, cost + 1));
q.push(Pos(x - 1, y - 2, cost + 1));
q.push(Pos(x + 2, y + 1, cost + 1));
q.push(Pos(x - 2, y + 1, cost + 1));
q.push(Pos(x + 2, y - 1, cost + 1));
q.push(Pos(x - 2, y - 1, cost + 1));
}
}
int main() {
cin >> n >> m >> a >> b;
memset(ans, -1, sizeof(ans));
bfs(a, b, 0);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
printf("%-5d", ans[i][j]);
}
cout << endl;
}
return 0;
}
评论:
整数加减A,B≤1000000,需要考虑这么复杂吗。。 int main() {
int a,b;
cin >> a >> b;
cout << a+b;
return 0;
}
请先登录,才能进行评论