80分求调
查看原帖
80分求调
781690
Wanghaochen28楼主2025/1/13 16:08
#include <bits/stdc++.h>
using namespace std;
int dx[] = {-2, -1, 1, 2, 2, 1, -1, -2, 2, 2, -2, -2};
int dy[] = {1, 2, 2, 1, -1, -2, -2, -1, -2, 2, -2, 2};
struct node
{
	int x, y;
};
queue<node> q;
int dis[405][405], sx, sy;
int n = 25, m = 25;
bool in(int x, int y)
{
	return 1 <= x && x <= n && 1 <= y && y <= m;
}
int bfs()
{
	q = queue<node>();
	memset(dis, -1, sizeof(dis));
	q.push({sx, sy});
	dis[sx][sy] = 0;
	while (!q.empty())
	{
		int x = q.front().x;
		int y = q.front().y;
		q.pop();
		if (x == 1 && y == 1)
		{
			return dis[x][y];
		}
		for (int i = 0; i < 12; i++)
		{
			int nx = x + dx[i];
			int ny = y + dy[i];
			if (dis[nx][ny] == -1)
			{
				q.push({nx, ny});
				dis[nx][ny] = dis[x][y] + 1;
			}
		}
	}
	return -1;
}
int main()
{
	cin >> sx >> sy;
	cout << bfs() << endl;
	cin >> sx >> sy;
	cout << bfs() << endl;
	return 0;
}
2025/1/13 16:08
加载中...