#include<iostream>
#include<queue>
using namespace std;
const int N = 1e3 + 10;
int n, k;
char mp[N][N];
bool vis[N][N][11];
int ans;
int dix[] = { 0,1,0,-1 };
int diy[] = { 1,0,-1,0 };
struct node
{
int x, y,k ,step ;
node(int cx, int cy, int ck, int cs)
{
x = cx,y = cy, k = ck, step = cs;
}
};
int bfs()
{
queue<node>q;
vis[0][0][0] = 1;
q.push(node(0, 0, 0, 0));
while (!q.empty())
{
node now = q.front();
q.pop();
if (now.x == n - 1 && now.y == n - 1)
{
return now.step;
}
for (int i = 0; i < 4; i++)
{
int tox = now.x + dix[i];
int toy = now.y + diy[i];
if (tox < 0 || toy < 0 || tox >= n || toy >= n || mp[tox][toy] == '#')
{
continue;
}
//无敌
if (mp[tox][toy] == '%' && !vis[tox][toy][k])
{
vis[tox][toy][k] = 1;
q.push(node(tox, toy, k, now.step + 1));
}
else
{
if (now.k && !vis[tox][toy][k - 1])
{
vis[tox][toy][k-1] == 1;
q.push(node(tox, toy, k-1, now.step + 1));
}
else
{
if (mp[tox][toy] == '.' && !now.k && !vis[tox][toy][0])
{
vis[tox][toy][0] = 1;
q.push(node(tox, toy, 0, now.step + 1));
}
}
}
}
}
return -1;
}
int main()
{
cin >> n >> k;
for (int i = 0; i < n; i++)
{
cin >> mp[i];
}
ans = bfs();
cout << ans;
return 0;
}
[蓝桥杯]迷宫于陷阱
最新推荐文章于 2023-11-29 08:00:00 发布