震惊!!!为何没有输出!!!
查看原帖
震惊!!!为何没有输出!!!
872386
Officer楼主2024/5/16 21:56
#include<bits/stdc++.h>
using namespace std;
struct node
{
	int x,y,step;
};
char a,b;
int ax,ay,bx,by;
int dir[8][2]={{1,2},{2,1},{-1,2},{2,-1},{-2,1},{1,-2},{-1,-2},{-2,-1}};
int vis[15][15];
void bfs(int x,int y)
{
	queue<node> q;
	q.push(node{x,y,0});
	vis[x][y]=1;
	while(!q.empty())
	{
		node u=q.front();
		q.pop();
		if(u.x==bx&&u.y==by)
		{
			printf("To get from %s%d to %s%d takes %d knight moves.\n",a,ay,b,by,u.step);
			return;
		}
		for(int i=0;i<8;i++)
		{
			int bx=u.x+dir[i][0];
			int by=u.y+dir[i][1];
			if(bx<1||bx>8||by<1||by>8)
			{
				continue;
			}
			if(vis[bx][by])
			{
				continue;
			}
			vis[bx][by]=1;
			q.push(node{bx,by,u.step+1});
		}
	}
	
}
int main()
{
	while(scanf("%s%d %s%d",a,ay,b,by))
	{
		memset(vis,0,sizeof(vis));
		ax=a-'a'+1;
		bx=b-'a'+1;
		bfs(ax,ay);
	}
    return 0;
}
/*
*/

2024/5/16 21:56
加载中...