你sum表示的是啥。
by ran_qwq @ 2023-03-04 19:45:23
@[Lish_Xizse](/user/355424) 这样会重复搜索同一个点吧,搜过的点打个标记不再搜就行了
by buaa_czx @ 2023-03-04 20:00:25
@[Lovely_Ran](/user/743048) 记次数
by Lish_Xizse @ 2023-03-04 20:37:22
@[buaa_czx](/user/90036) 可我设置了没走过再走啊……
by Lish_Xizse @ 2023-03-04 20:37:59
@[Lish_Xizse](/user/355424) a应该表示的是最小步数而不是搜索的次数
by ran_qwq @ 2023-03-04 21:28:57
@[Lovely_Ran](/user/743048) emm…什么意思
by Lish_Xizse @ 2023-03-04 21:50:14
@[Lish_Xizse](/user/355424) 换句话说,这样就过了
就是加一个queue维护时间
```
#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
int a[410][410]={-2},n,m,x,y;
int dx[8]={-2,-2,+2,+2,-1,-1,+1,+1};
int dy[8]={-1,+1,-1,+1,-2,+2,-2,+2};
queue<int> p;
queue<int> q;
queue<int> tim;
void BFS(){
while(!p.empty()&&!q.empty()){
for(int i=0;i<8;i++){
if(a[p.front()+dx[i]][q.front()+dy[i]]==-1){
a[p.front()+dx[i]][q.front()+dy[i]]=tim.front()+1;
p.push(p.front()+dx[i]);
q.push(q.front()+dy[i]);
tim.push(tim.front()+1);
}
}
p.pop();
q.pop();
tim.pop();
}
}
int main()
{
cin >> n >> m >> x >> y;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++)a[i][j]=-1;
}
a[x][y]=0;
p.push(x);
q.push(y);
tim.push(0);
BFS();
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
printf("%-5d", a[i][j]);
}
cout << endl;
}
return 0;
}
```
by ran_qwq @ 2023-03-04 21:53:35
![](https://cdn.luogu.com.cn/upload/image_hosting/o637opsa.png)
这是一个搜索树,黑色数字表示最小步数,红色数字表示你的sum
by ran_qwq @ 2023-03-04 21:56:37
@[Lovely_Ran](/user/743048) 哦,我明白了,谢谢大佬
by Lish_Xizse @ 2023-03-04 22:00:34