30分蒟蒻求助大佬

P1443 马的遍历

```cpp #include<bits/stdc++.h> using namespace std; int sx,sy,n,m; int a[401][401]; bool vis[401][401];//开bool就够省空间 short dx[9] = {1,-1,1,-1,-2,-2,2,2}; short dy[9] = {-2,-2,2,2,1,-1,1,-1}; struct node{ int x; int y; }; queue <node> q; void bfs() { q.push(node{sx,sy}); while (!q.empty()) { for (int i = 0;i < 8;i++) { int xx = q.front().x+dx[i]; int yy = q.front().y+dy[i]; if (xx >= 1 && xx <= n && yy >= 1 && yy <= m && vis[xx][yy] != 1){ vis[xx][yy] = 1; a[xx][yy] = a[q.front().x][q.front().y] + 1; q.push(node{xx,yy}); } } q.pop(); } } int main(){ std::ios::sync_with_stdio(false);//这样cin cout就快了 //cin>>m>>n>>sx>>sy; 额这里有问题你行列没分清然后就AC了其他也不需要改 cin>>n>>m>>sx>>sy; vis[sx][sy] = 1; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) a[i][j]=-1; //memset(a,-1,sizeof(a)); a[sx][sy] = 0; bfs(); for (int i = 1;i <= n;i++) { for (int j = 1;j <= m;j++) cout<<a[i][j]<<' '; //printf("%d ",a[i][j]); printf和cin不要一起用占用时间 cout<<endl; } return 0;//比赛时记得加 } ``` 改好了并进行了部分时间和空间的优化,行列一定要分清!!!!!!
by Augensterm @ 2023-07-11 13:05:34


感谢大佬的回复
by LCX_201109091514 @ 2023-07-25 18:06:33


|