我的代码就是跑不出来,找不到哪里错了,逻辑上没有错误
#include<bits/stdc++.h>
using namespace std;
const int N = 1001;
int w;//小明体积
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int n, k;
char mp[N][N];//存储地图 *障碍 +空地
bool vis[N][N];
struct Node {
int x;
int y;
int t;
};
bool check(int xx, int yy) { //判断是否有障碍或者出界
for (int i = xx - w / 2; i <= xx + w / 2; i++) {
for (int j = yy - w / 2; j <= yy + w / 2; j++) {
if (mp[i][j] == '*') return false;
}
}
//越界
if (xx + w / 2 > n || xx - w / 2 < 1 || yy + w / 2 > n || yy - w / 2 < 1) return false;
return true;
}
void bfs() {
queue<Node>q;
q.push({3, 3, 0}); //(3,3)起点入队
vis[3][3] = 1;
while (!q.empty()) {
Node s = q.front();
q.pop();
if (s.x == n - 2 && s.y == n - 2) { //到达终点
cout << s.t;
return;
}
//小明体积判断
if (s.t < k) w = 5;
else if (s.t >= k && s.t < 2 * k) w = 3;
else if (s.t >= 2 * k) w = 1;
if (w != 1) q.push({s.x, s.y, s.t + 1}); //原地不动
for (int i = 0; i < 4; i++) {
int fx = s.x + dx[i], fy = s.y + dy[i];
if (!vis[fx][fy] && check(fx, fy) ) {
vis[fx][fx] = 1;
//cout<<fx<<" "<<fy<<endl;
q.push({fx, fy, s.t + 1}); //新点入队
}
}
}
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> mp[i][j];
}
}
bfs();
return 0;
}