HDOJ-1547 Bubble Shooter 简单搜索

本文详细介绍了如何通过仔细阅读题目,解决一个关于交错排列图的DFS问题。通过两次DFS操作,作者成功解决了这个问题,并强调了读题的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一次做的时候没有显示图片,主要还是因为我读题不仔细。谨记读题一定要仔细。

该题不是矩阵图,是一个交错排列的图。

两次dfs过。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
int mv[6][2] = {0, 2, 0, -2, 1, 1, -1, -1, -1, 1, 1, -1};
int mm[222][222];
int h, w, sh, sw;
int cnt, ans;
int check(int x, int y)
{
    return (x <= h && y <= 2 * w && x >= 1 && y >= 1);
}
void dfs(int x, int y)
{
    ans --;
    mm[x][y] = 0;
    for(int i = 0; i < 6; i++)
    {
        int nextx = x + mv[i][0];
        int nexty = y + mv[i][1];
        if(check(nextx, nexty) && mm[nextx][nexty])
            dfs(nextx, nexty);
    }
}
void destroy(int x, int y, int cl)
{
    cnt ++;
    mm[x][y] = 0;
    for(int i = 0; i < 6; i++)
    {
        int nextx = x + mv[i][0];
        int nexty = y + mv[i][1];
        if(check(nextx, nexty) && mm[nextx][nexty] == cl)
            destroy(nextx, nexty, cl);
    }
}
int main()
{
    char str[101];
    int i, j;
    while(~scanf("%d%d%d%d",&h, &w, &sh, &sw))
    {
        memset(mm, 0, sizeof(mm));
        for(i = 1; i <= h; i++)
        {
            scanf("%s",str);
            int len = strlen(str);
             for(j = 1 + !(i % 2); j <= 2 * len; j += 2)
                if(str[(j + 1) / 2 - 1] != 'E')
                    mm[i][j] = str[(j + 1) / 2 - 1] - 'a' + 1;
        }
        ans = 0;
        cnt = 0;
        sw = sw * 2 - (sh % 2);
        destroy(sh, sw, mm[sh][sw]);
        if(cnt < 3)
        {
            printf("0\n");
            continue;
        }
        for(i = 1; i <= h; i++)
            for(j = 1; j < 2 * w; j++)
                if(mm[i][j])ans ++;
        for(i = 1; i < 2 * w; i++)
            if(mm[1][i])
            dfs(1, i);
        printf("%d\n",ans + cnt);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值