test

//hdu 1728
#include<cstdio>
#include<cstring>
#include<queue>
#define INF 10000000;
using namespace std;
int d1[4] = {0,0,1,-1};
int d2[4] = {1,-1,0,0};
int turn1[101][101];
char map[101][101];
int n,m,k;
int ok;
int sx,sy,ex,ey;
struct state
{
    int x,y;
    int turn;
    int dir;
}cur,next1;
void bfs(state temp)
{
    temp.dir = -1;
    temp.turn = 0;
    turn1[temp.x][temp.y] = 0;
    queue<state> q;
    q.push(temp);
    while(!q.empty())
    {
        cur = q.front();
        q.pop();  //之后最好将队列的头元素取出之后就将其弹出,避免之后忘记。。不要像以前那样写在下面。。防止剪枝的时候出错。。 
        if(cur.x == ex-1 && cur.y == ey-1)
        {
            if(cur.turn <= k)
            {
                ok = 1;
                printf("yes\n");
                return ;
            }
        }
        if(cur.turn > k)
        {
            //q.pop();  //剪枝后没有写这句然后死循环 
            continue;
        }
        for(int i = 0;i < 4;i++)
        {
            next1.x = cur.x + d1[i];
            next1.y = cur.y + d2[i];
            if(next1.x>=0&&next1.x<n&&next1.y>=0&&next1.y<m&&map[next1.x][next1.y]!='*')
            {
                if(cur.dir!=i &&cur.dir!=-1)
                {
                    next1.dir = i;
                    next1.turn = cur.turn+1;
                    if(next1.turn<=turn1[next1.x][next1.y])//此处必须是等于号
                    {
                        turn1[next1.x][next1.y] = next1.turn;
                        q.push(next1);   //一定要保证当前的转弯数是该点转弯数最小的才入队。。不然直接爆空间。。
                    }
                }
                else 
                {
                    next1.dir = i;
                    next1.turn = cur.turn;
                    if(next1.turn<=turn1[next1.x][next1.y])
                    {
                        turn1[next1.x][next1.y] = next1.turn;
                        q.push(next1);  
                    }
                }
            }
        }
        //q.pop();
    }
}
int main ()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ok = 0;
        scanf("%d %d",&n,&m);
        for(int i = 0;i < n;i++)
        for(int j = 0;j < m;j++)
        turn1[i][j] = INF;
        for(int i = 0;i < n;i++)
        scanf("%s",map[i]);
        scanf("%d%d%d%d%d",&k,&sy,&sx,&ey,&ex);
        cur.x = sx-1;
        cur.y = sy-1;
        bfs(cur);
        if(!ok) printf("no\n");
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值