死循环叫$Runtime$ $Error$($RE$)
by aminoas @ 2019-03-02 20:42:15
@[键盘超人](/space/show?uid=143121)
bfs状态要去重
by Smile_Cindy @ 2019-03-02 20:42:34
@[Alpha](/space/show?uid=87058) 啊?蒟蒻不太懂,那我需要修改哪个地方
by hunz1 @ 2019-03-02 20:51:04
@[2018J1605](/space/show?uid=143834) 哦哦,顺便问下你这个Runtime Error是用markdown打出来的吗
by hunz1 @ 2019-03-02 20:59:53
是的
by aminoas @ 2019-03-02 21:00:58
@[2018J1605](/space/show?uid=143834) 具体格式代码是怎样的,我百度找不到诶。。
by hunz1 @ 2019-03-02 21:34:11
换了一种写法又ac了,好奇怪
```cpp
#include <bits/stdc++.h>
using namespace std;
const int maxn = 444;
int a[maxn][maxn];
bool vis[maxn][maxn];
int ans[maxn][maxn];
int dir_x[8] = {-1, -1, -2, -2, 1, 1, 2, 2};
int dir_y[8] = {2, -2, 1, -1, 2, -2, 1, -1};
struct Node
{
int x, y;
};
int n, m;
int sx, sy;
void bfs()
{
queue<Node> q;
Node f;
f.x = sx;
f.y = sy;
ans[sx][sy] = 0;
vis[sx][sy] = true;
q.push(f);
while (!q.empty())
{
Node t = q.front();
q.pop();
//cout << 1 << endl;
for (int i = 0; i < 8; i++)
{
Node dt;
dt.x = t.x + dir_x[i];
dt.y = t.y + dir_y[i];
if (dt.x >= 1 && dt.x <= n && dt.y >= 1 && dt.y <= m && !vis[dt.x][dt.y])
{
q.push(dt);
ans[dt.x][dt.y] = ans[t.x][t.y] + 1;
vis[dt.x][dt.y] = true;
}
}
}
}
int main()
{
cin >> n >> m >> sx >> sy;
memset(ans, -1, sizeof(ans));
bfs();
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
printf("%-5d", ans[i][j]);
cout << endl;
}
return 0;
}
```
by hunz1 @ 2019-03-02 21:34:50
@[Alpha](/space/show?uid=87058) 懂了懂了,想了半天,要在push的时候设置下访问数组QAQ
by hunz1 @ 2019-03-02 23:19:33