hdu1728逃离迷宫 bfs

//bfs用vis[i][x][y][k] 表示从i的方向到x,y且剩下的转弯数是k
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 110 ;
int vis[4][maxn][maxn][maxn] ;
char map[maxn][maxn] ;
struct node
{
    int k ;
    int pre ;
    int x , y;
};
int dx[4] = {0,1,0,-1} ;
int dy[4] = {-1,0,1,0} ;
int st_x , st_y ;
int en_x , en_y ;
int m ,n ; int  k;
int  bfs()
{
    queue<struct node> que ;
    memset(vis , 0 , sizeof(vis)) ;
    struct node first = {k , -1 , st_x ,st_y} ;
    que.push(first) ;
    for(int i = 0;i < 4;i++)
    vis[i][k][st_x][st_y] = 1;
    while(que.size())
    {
        struct node now = que.front() ;
        que.pop();
        if(now.x == en_x && now.y == en_y && now.k >= 0)
        {
            printf("yes\n") ;
            return 1;
        }
        for(int i = 0;i < 4;i++)
        {
            struct node next ;
            next.x = now.x + dx[i] ;
            next.y = now.y + dy[i] ;
            if(next.x > m || next.x < 1 || next.y > n ||next.y < 1  || map[next.x][next.y] == '*')
            continue ;
            next.k = now.k ;
            next.pre = i ;
            if(now.pre != -1 && now.pre != i)
            {
                next.k-- ;
                if(next.k < 0)
                continue;
            }
            if(vis[i][next.k][next.x][next.y])
            continue ;
            que.push(next) ;
            vis[i][next.k][next.x][next.y] = 1 ;
        }
    }
     printf("no\n");
     return 0;
}
int main()
{
    int T ;
   // freopen("in.txt","r",stdin) ;
    scanf("%d" , &T) ;
    while(T--)
    {
        scanf("%d%d" ,&m ,&n) ;
        for(int i = 1;i <= m;i++)
        scanf("%s",&map[i][1]) ;
        scanf("%d%d%d%d%d" ,&k, &st_y , &st_x ,&en_y ,&en_x) ;
        bfs() ;
    }
    return 0 ;
}





































































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值