保龄求助

P1443 马的遍历

@[QAQ_juruo](/user/1098781) ```cpp #include<bits/stdc++.h> using namespace std; int dx[8]={1,2,2,1,-1,-2,-2,-1}; int dy[8]={-2,-1,1,2,2,1,-1,-2}; int n,m,x,y,ans[1005][1005]; bool vis[1005][1005]; struct node{ int x,y; }; queue<node> q; void bfs(int x,int y){ q.push(node{x,y}); vis[x][y]=1; ans[x][y]=0; while(!q.empty()){ node now=q.front(); q.pop(); for(int i=0;i<8;i++){ int tx=now.x+dx[i]; int ty=now.y+dy[i]; if(tx>=1 && tx<=n && ty<=m && ty>=1 && !vis[tx][ty]){ vis[tx][ty]=1; ans[tx][ty]=ans[now.x][now.y]+1; q.push(node{tx,ty}); } } } } int main(){ cin>>n>>m>>x>>y; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ ans[i][j]=-1; } } bfs(x,y); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ printf("%-5d",ans[i][j]); } printf("\n"); } return 0; } ```
by wei2013 @ 2024-02-10 09:46:40


@[QAQ_juruo](/user/1098781) ```cpp vis[400+1][400+1]={-1} ``` 这行相当于是吧第0项化为-1
by wei2013 @ 2024-02-10 09:48:13


@[wei2013](/user/1060672) thanks
by QAQ_juruo @ 2024-02-15 16:24:34


|