蒟蒻求助

P1443 马的遍历

这是我的AC代码: ```cpp #include<iostream> #include<queue> #include<cstring> using namespace std; int place[8][2]={{1,2},{-1,2},{1,-2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1}}; int n,m,dt[405][405]; struct pc { int x; int y; int ct; }; queue<pc> a; int bfs() { pc nw,nt; while(!a.empty()) { nw=a.front(); a.pop(); for(int i=0;i<8;i++) { nt.x=nw.x+place[i][0]; nt.y=nw.y+place[i][1]; nt.ct=nw.ct+1; if((nt.x>=1 && nt.x<=m) && (nt.y>=1 && nt.y<=n) && dt[nt.y][nt.x]==-1) { dt[nt.y][nt.x]=nt.ct; a.push(nt); } } } return 0; } int main() { pc st; cin>>n>>m>>st.y>>st.x; memset(dt,-1,sizeof(dt)); dt[st.y][st.x]=0; st.ct=0; a.push(st); bfs(); for(int y=1;y<=n;y++) { for(int x=1;x<=m;x++) {cout<<dt[y][x]<<" ";} cout<<endl; } return 0; } ```
by AZYDLL @ 2023-08-10 12:28:25


@[AZYDLL](/user/881733) 感谢
by Cedric_Cedris @ 2023-08-10 17:41:41


|