hdu1175 连连看(bfs求拐弯次数)


http://acm.hdu.edu.cn/showproblem.php?pid=1175

题意:中文题不解释。


思路:核心就和hdu1728一样,思路请看我的分析。有了上一题的基础,这题就是割草小游戏了,1A~

好久木有1A了,感动哭


#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <stack>
#include <ctype.h>

using namespace std;

typedef long long LL;

const int N = 1005;
const int INF = 0x3f3f3f3f;

int G[N][N];
bool vis[N][N];
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int n, m, sx, sy, ex, ey;

struct node
{
    int x, y, step;
};

bool check(int x, int y)
{
    if(x>=1 && x<=n && y>=1 && y<=m && G[x][y]==0) return true;
    else return false;
}

void bfs(int x, int y)
{
    if(x==ex && y==ey)//这里注意特殊情况
    {
        printf("YES\n");
        return;
    }
    memset(vis, false, sizeof(vis));
    queue<node>que;
    node s;
    s.x = x;
    s.y = y;
    s.step = -1;
    vis[x][y] = true;
    que.push(s);
    while(!que.empty())
    {
        node tmp = que.front();
        que.pop();
        for(int i = 0; i < 4; i++)
        {
            node tmp2;
            tmp2 = tmp;
            tmp2.x += dir[i][0];
            tmp2.y += dir[i][1];
            while(check(tmp2.x, tmp2.y))
            {
                if(!vis[tmp2.x][tmp2.y])
                {
                    vis[tmp2.x][tmp2.y] = true;
                    tmp2.step = tmp.step+1;
                    if(tmp2.x == ex && tmp2.y == ey && tmp2.step<=2)
                    {
                        printf("YES\n");
                        return;
                    }
                    que.push(tmp2);
                }
                tmp2.x += dir[i][0];
                tmp2.y += dir[i][1];
            }
        }
    }
    printf("NO\n");
    return;
}

int main()
{
  //  freopen("in.txt", "r", stdin);
    while(~scanf("%d%d", &n, &m))
    {
        if(n==0 && m==0) break;
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++)
            {
                cin >> G[i][j];
            }
        int q;
        scanf("%d", &q);
        while(q--)
        {
            scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
            if(G[sx][sy]!=G[ex][ey] || G[sx][sy]==0 || G[ex][ey]==0) printf("NO\n");
            else
            {
                int tmp = G[sx][sy];
                G[sx][sy] = G[ex][ey] = 0;
                bfs(sx, sy);
                G[sx][sy] = G[ex][ey] = tmp;
            }
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值