逃离迷宫HDU-1728

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
struct node {
    int x;
    int y;
    int k;
}b;
queue<node> q;
int n, m, y11, x1, y2, x2, f;
int dx[] = { 0,1,0,-1 };
int dy[] = { 1,0,-1,0 };
int vis[110][110];
char map[110][110];

void bfs()
{
    while (!q.empty())
    {
        q.pop();
    }
    vis[x1][y11] = 1;
    q.push({ x1,y11,-1 });
    while (!q.empty())
    {
        node a;
        a = q.front();
        q.pop();
        if (a.x == x2 && a.y == y2 && a.k <= f)
        {
            cout << "yes" << endl;
            return;
        }
        b.k = a.k + 1;
        for (int i = 0; i < 4; i++)
        {
            int nx = a.x + dx[i];
            int ny = a.y + dy[i];
            while (nx >= 0 && nx < n && ny >= 0 && ny < m && map[nx][ny] != '*')
            {
                if (!vis[nx][ny])
                {
                    vis[nx][ny] = 1;
                    b.x = nx; b.y = ny;
                    q.push(b);
                }
                nx = nx + dx[i];
                ny = ny + dy[i];
            }
        }
    }
    cout << "no" << endl;
}
int main()
{
    int T;
    cin >> T;
    while (T--) {
        cin >> n >> m;

        memset(vis, 0, sizeof(vis));
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
                cin >> map[i][j];
        }
        cin >> f >> y11 >> x1 >> y2 >> x2;
        y2 = y2 - 1; x1 = x1 - 1;
        x2 = x2 - 1; y11 = y11 - 1;
        bfs();

    }
    return 0;
}//s

不只 周围的四个加入队列 而是从这个点 向上下左右所有不为障碍物的点加入队列

 for (int i = 0; i < 4; i++)
 {
     int nx = a.x + dx[i];
     int ny = a.y + dy[i];
     while (nx >= 0 && nx < n && ny >= 0 && ny < m && map[nx][ny] != '*')
     {
         if (!vis[nx][ny])
         {
             vis[nx][ny] = 1;
             b.x = nx; b.y = ny;
             q.push(b);
         }
         nx = nx + dx[i];
         ny = ny + dy[i];
     }
 }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值