就是一个国际象棋的棋盘,可以八个方向走,问从起点到终点的最少步数是多少,还给出一个一个点是不可走的,简单的bfs
#include#include #include #include #include #include using namespace std;int vis[10][10];int dis[8][2]={-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1} ;struct node{ int x,y,step;};int sx,sy,ex,ey,ix,iy;int bfs(){ int ans=0; queue q; node in,out; in.x=sx; in.y=sy; in.step=0; vis[sx][sy]=1; q.push(in); while(!q.empty()) { in=q.front(); q.pop(); if(in.x==ex&&in.y==ey) { return in.step; } else { for(int i=0;i<8;i++) { out.x=in.x+dis[i][0]; out.y=in.y+dis[i][1]; out.step=in.step; if(vis[out.x][out.y]||out.x<1||out.x>8||out.y<1||out.y>8||(out.x==ix&&out.y==iy)) continue; else { vis[out.x][out.y]=1; out.step++; q.push(out); } } } } // return out.step; }int main(){ int t=0; while(scanf("%d %d %d %d %d %d",&sx,&sy,&ex,&ey,&ix,&iy)!=EOF) { memset(vis,0,sizeof(vis)); printf("Case %d: %d\n",++t,bfs()); } return 0;}