@[MUNLELEE](/space/show?uid=150709)
您会用Markdown吗,这样没人给您看代码的。
by Reio_Stark @ 2019-04-20 23:35:13
@[Reio_Stark](/space/show?uid=165056) 要怎么用呢
by MUNLELEE @ 2019-04-20 23:37:37
```cpp
define _CRT_SECURE_NO_WARNINGS
include<iostream>
include<algorithm>
include<cstring>
include<queue>
include<cmath>
include<cstdio>
using namespace std;
const int MAX = 405;
char map[MAX][MAX];//用来记录走到的步数 bool visit[MAX][MAX];//用来标记是否被访问过
struct node
{
int x, y;
} start;
queue<node> que; //八方向遍历数组
int dx[8] = { -2, -2, -1, -1, 1, 1, 2, 2 };
int dy[8] = { 1, -1, 2, -2, -2, 2, 1, -1 };
int n, m, sx, sy;//大小,起始坐标 bool in_range(int x, int y)
{
if (x < 1 || y < 1 || x > n || y > m || visit[x][y] || map[x][y] != -1)
{
return false;
}
return true;
}
void bfs()
{
while (que.size())
{
node now = que.front(); que.pop();
for (int i = 0; i < 8; ++i)
{
int nx = now.x + dx[i];
int ny = now.y + dy[i];
if (in_range(nx, ny))
{
node other;
other.x = nx;
other.y = ny;
map[nx][ny] = map[now.x][now.y] + 1;
visit[nx][ny] = true;
que.push(other);
}
}
}
}
int main(void)
{
ios::sync_with_stdio(false);
cin >> n >> m >> sx >> sy; memset(map, -1, sizeof(map)); memset(visit, false, sizeof(visit)); start.x = sx;
start.y = sy;
map[start.x][start.y] = 0; visit[start.x][start.y] = true; que.push(start);
bfs();
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
printf("%-5d", map[i][j]);
}
cout << endl;
} system("pause");
return 0;
}
```
## 就这样
by Reio_Stark @ 2019-04-20 23:44:28
那能帮我看下代码嘛ball ball u
by MUNLELEE @ 2019-04-20 23:50:01
不行,我也调不出来
by Reio_Stark @ 2019-04-21 00:07:52
太玄学了
by Reio_Stark @ 2019-04-21 00:08:09
@[Reio_Stark](/space/show?uid=165056) memset的问题,应该用for循环
by aminoas @ 2019-04-21 00:50:30
@[Reio_Stark](/space/show?uid=165056)
你输出格式不对,第一列是不空格的。
by nofind @ 2019-04-21 02:12:42
@[Reio_Stark](/space/show?uid=165056)
https://www.luogu.org/recordnew/show/18407464
把格式一调就对了,你把最后一行换成:
```
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
printf("%-5d", map[i][j]);
printf("\n");
}
```
对了,以后开变量不要用map,最好写mp,因为map是关键字~~您应该知道吧,我好像多嘴了``~~
by nofind @ 2019-04-21 02:18:11
@[MUNLELEE](/space/show?uid=150709)
我好像@错人了~~~
by nofind @ 2019-04-21 02:19:27