HDOJ 1175 连连看 (bfs)

题目链接:~( ̄▽ ̄~)(~ ̄▽ ̄)~


思路:用bfs一次把这个方向上能到的点入队


code:

#include <stdio.h>
int m = 0, n = 0, front = 0, rear = 0, map[1002][1002], used[1002][1002];
int X1 = 0, Y1 = 0, X2 = 0, Y2 = 0;
int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
typedef struct
{
    int x, y, count;
}node;
node step, quene[1000*1000+1];
int bfs()
{
    int i = 0, x = 0, y = 0, fx = 0, fy = 0, count = 0;
    quene[rear++] = step;
    while(front<rear)
    {
        x = quene[front].x; y = quene[front].y; count = quene[front++].count;
        for(i = 0; i<4; i++)
        {
            fx = x+dir[i][0]; fy = y+dir[i][1];
            while(!map[fx][fy] && count<3 && fx>0 && fx<=n && fy>0 &&fy<=m && !used[fx][fy])//这方向上能到的点入队
            {
                used[fx][fy] = 1;
                step.x = fx; step.y = fy; step.count = count+1;
                quene[rear++] = step;
                fx += dir[i][0]; fy += dir[i][1];
            }
            if(fx == X2 && fy == Y2 && count<3)
                return 1;
        }
    }
    return 0;
}
int main()
{
    int i = 0,  j= 0, q = 0;
    while(scanf("%d %d",&n, &m) , n && m)
    {
        for(i = 1; i<=n; i++)
            for(j = 1; j<=m; j++)
                scanf("%d",&map[i][j]);
        scanf("%d",&q);
        while(q--)
        {
            for(i = 1; i<=n; i++)
                for(j = 1; j<=m; j++)
                    used[i][j] = 0;
            scanf("%d %d %d %d",&X1, &Y1, &X2, &Y2);
            if((map[X1][Y1] != map[X2][Y2] || !map[X1][Y1] || !map[X2][Y2]) || (X1==X2 && Y2 == Y1))
                printf("NO\n");
            else
            {
                front = rear = 0;
                step.x = X1; step.y = Y1;step.count = 0;
                if(bfs())
                    printf("YES\n");
                else
                    printf("NO\n");
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值