Language: C++

#include<bits/stdc++.h>
using namespace std;
struct zt { int x; int y; };//跟白皮书的pair一个意思，表示当前状态
const int INF = 100000000;//见白皮书
int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
int n, m, k;
int sx, sy, gx, gy;
void bfs()
{
	queue<zt> que;
	char **maze = new char*[n + 2];
	for (int i = 0; i < n + 2; i++)
		maze[i] = new char[m + 2];
	int **hq = new int*[n+2];
	for (int i = 0; i < n+2; i++)
		hq[i] = new int[m+2];
	int **zq = new int*[n + 2];
	for (int i = 0; i < n + 2; i++)
		zq[i] = new int[m + 2];

	for (int i = 1; i <= n; ++i)
		scanf("%s", maze[i] + 1);

	for (int i = 0; i <= n; ++i)
		for (int j = 0; j <= m; ++j)hq[i][j] =0,zq[i][j] = 0;
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j)
		{
			
			hq[i][j] += hq[i][j - 1];
			zq[i][j] += zq[i - 1][j];
			if (maze[i][j] != '.')hq[i][j]++, zq[i][j]++;
		}
	/*for (int i = 0; i <= n + 1; ++i)//示例输出
	{
		for (int j = 0; j <= m + 1; ++j)cout << maze[i][j] << " ";
		cout << endl;
	}
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)cout << zq[i][j] << " ";
		cout << endl;
	}
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)cout << hq[i][j] << " ";
		cout << endl;
	}*/

	int **d = new int*[n + 2];
	for (int i = 0; i < n + 2; i++)
		d[i] = new int[m + 2];
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j)d[i][j] = INF;

	que.push(zt{ sx, sy });
	d[sx][sy] = 0;//以上初始化迷宫
	while (que.size())
	{
		int ts = 0; 3 5 2
			3 2 3 4
			.....
			.@..@
			..@..
		zt p = que.front();
		if (p.x == gx && p.y == gy)break;//搜索到终点了停止搜索，至于为什么队列里最先到终点的一定是最优解，见白皮书。如果k处理的不好有可能导致最先到终点的状态不是最优解，这个设计要自己想明白
		que.pop();
		for (int i = 0; i < 4; ++i)
		{
			int nx, ny,j=0;
			int l = 1, r = k, mid;
			while (l <= r)//从小到大迈开步子看看最多能迈到哪，这里j不能从大到小循环，否则会出现“飞檐走壁翻墙”的操作
			{
				mid = (l + r) >> 1; 
				nx = p.x + mid * dx[i], ny = p.y + mid * dy[i]; //cout << nx << " " << ny << endl;
				if (i % 2 == 0 && (1 <= nx && nx <= n && 1 <= ny && ny <= m) && zq[nx][ny] - zq[p.x][p.y] == 0)l = mid + 1, j = max(j, mid);
				else if (i % 2 != 0 && (1 <= nx && nx <= n && 1 <= ny && ny <= m) && hq[nx][ny] - hq[p.x][p.y] == 0)l = mid + 1, j = max(j, mid);
				else r = mid - 1;
			}
			for (int jj = j ; jj >0; --jj)//从最多走k步的状态先入队，再k-1，k-2。因为根据队列先入先出的特性，这样做保证能最先出来的步子迈的尽可能大（即时间最短）
			{
				nx = p.x + jj * dx[i], ny = p.y + jj * dy[i];
				if (1 <= nx && nx <= n && 1 <= ny && ny <= m && maze[nx][ny] == '.'&&d[nx][ny] == INF)
				{
					d[nx][ny] = d[p.x][p.y] + 1;
					if (nx == gx && ny == gy) { ts = 1; break; }
					que.push(zt{ nx,ny });
				}
			}
			if (ts == 1)break;
		}
		if (ts == 1)break;
	}
	if (d[gx][gy] != INF)cout << d[gx][gy];
	else cout << -1;
}

int main()
{
	cin >> n >> m >> k;
	cin >> sx >> sy >> gx >> gy;
	bfs();
}

Compiler output:
a.cc:63:26: error: stray '@' in program
   63 |                         .@..@
      |                          ^
a.cc:63:29: error: stray '@' in program
   63 |                         .@..@
      |                             ^
a.cc:64:27: error: stray '@' in program
   64 |                         ..@..
      |                           ^
a.cc: In function 'void bfs()':
a.cc:60:30: error: expected ';' before numeric constant
   60 |                 int ts = 0; 3 5 2
      |                              ^~
      |                              ;
a.cc:66:21: error: 'p' was not declared in this scope
   66 |                 if (p.x == gx && p.y == gy)break;//搜索到终点了停止搜索，至于为什么队列里最先到终点的一定是最优解，见白皮书。如果k处理的不好有可能导致最先到终点的状态不是最优解，这个设计要自己想明白
      |                     ^
a.cc:75:38: error: 'p' was not declared in this scope
   75 |                                 nx = p.x + mid * dx[i], ny = p.y + mid * dy[i]; //cout << nx << " " << ny << endl;
      |                                      ^
a.cc:82:38: error: 'p' was not declared in this scope
   82 |                                 nx = p.x + jj * dx[i], ny = p.y + jj * dy[i];
      |                                      ^