马的遍历BFS 90pts,WA#8

P1443 马的遍历

@[brownball](/user/495599) 场宽呢?还有我都看不出来你那是广搜。 ```cpp #include <iostream> #include <cstring> #include <queue> using namespace std; #define FOR(i,j,n,k) for(int i=(j);i<=(n);i+=k) #define ROR(i,j,n,k) for(int i=(j);i>=(n);i-=k) const int dx[8]={-1,-2,-2,-1,1,2,2,1}; const int dy[8]={2,1,-1,-2,2,1,-1,-2}; int n,m; int map[410][410]; struct csp{ int x,y,a; }; bool vis[410][410]; bool check(int x,int y){ return (x>=1&&x<=n&&y>=1&&y<=m); } int bfs(int x,int y,int a){ queue<csp> q; q.push((csp){x,y,a}); while(!q.empty()){ x=q.front().x; y=q.front().y; a=q.front().a; q.pop(); if(vis[x][y]) continue; vis[x][y]=1; map[x][y]=a; FOR(i,0,7,1){ int tx=x+dx[i]; int ty=y+dy[i]; if(check(tx,ty)) q.push((csp){tx,ty,a+1}); } } return -1; } int main(){ int x,y; scanf("%d%d%d%d",&n,&m,&x,&y); memset(map,-1,sizeof(map)); int d=bfs(x,y,0); FOR(i,1,n,1){ FOR(j,1,m,1){ if(i==x&&j==y){ printf("%-5d",0); continue; } printf("%-5d",map[i][j]); } printf("\n"); } return 0; } ```
by danlao @ 2024-02-07 11:24:17


|