让人春分日 哈工科教110180.营救

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值