pointteleport(int x, int y, int k) { int hk = k >> 1; if (k & 1) return {y + hk + 1, x + hk}; return {x + hk, y + hk}; }
voidadd_bfs(int x, int y, int depth, queue<pair<point, int>> &bfs) { if (!bfsed[x][y] && pass[x][y]) { bfsed[x][y] = true; bfs.push({{x, y}, depth}); } }
intmain() { cin >> n >> k; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { char sym; cin >> sym; if (sym == '.') pass[i][j] = true; else pass[i][j] = false; } queue<pair<point, int>> bfs; add_bfs(1, 1, 0, bfs); int ans = -1; while (!bfs.empty()) { point np = bfs.front().first; int dep = bfs.front().second; bfs.pop(); int x = np.first, y = np.second; if (x == n && y == n) { ans = dep; break; } int maxk = min(min(2 * (n - x) + 1, 2 * (n - y)), min(n + n - x - y, k)); for (int i = maxk; i > 0; --i) { point tp = teleport(x, y, i); int tx = tp.first, ty = tp.second; if (teled[tx][ty]) break; teled[tx][ty] = true; add_bfs(tx, ty, dep + 1, bfs); } add_bfs(x + 1, y, dep + 1, bfs); add_bfs(x - 1, y, dep + 1, bfs); add_bfs(x, y + 1, dep + 1, bfs); add_bfs(x, y - 1, dep + 1, bfs); } cout << ans; }