#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN = 1005;
const int dx[] = { 0, 1, -1, 0, 0 };
const int dy[] = { 0, 0, 0, 1, -1 };//方向数组
int s, t, ss, tt, n;
int ans = 0x3f3f3f3f;//极大值,用于查找最小值
bool map[MAXN][MAXN];//储存地图
struct node {
int x;
int y;
int step;
};//两个坐标,一个是步数
void search_(int x, int y) {
node p, t;
p.x = x;
p.y = y;
p.step = 0;//初始化
queue<node> q;
q.push(p);
while (!q.empty()) {//搜索
p = q.front();
if (p.x == ss && p.y == tt) {
ans = min(p.step, ans);
break;
}//符合条件,找到答案并跳出循环
for (int i = 1; i <= 4; i++) {
t.x = p.x + dx[i];
t.y = p.y + dy[i];//四个方向枚举怎么走
if (t.x > 0 && t.x <= n && t.y > 0 && t.y <= n && !map[t.x][t.y]) {//此步合法
map[t.x][t.y] = 1;//就走这步
t.step = p.step + 1;//增加一步
q.push(t);//继续搜索
}
}
q.pop();//弹出队首,继续下一次循环
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("\n");
for (int j = 1; j <= n; j++) {
char x;
scanf("%c", &x);
map[i][j] = x - '0';
}
}//初始化
scanf("%d %d %d %d", &s, &t, &ss, &tt);//输入
search_(s, t);
printf("%d\n", ans);//输出答案
return 0;
}
让人春分日 哈工科教110180.营救
最新推荐文章于 2024-08-18 21:07:18 发布