uva 1103 dfs染色+dfs判断围块

题意:

给一张16进制的图,然后给6种象形文字,你来判断图中存在几个象形文字,将结果按照字典序输出。


Ankh:A

Wedjat:J

Djed:D

Scarab:S

Was:W

Akhet:K


解析:

小紫上已经提示到了,用象形文字的“空洞”数来判断是哪个象形文字,所以,现在的关键在于,如何判断一个1围成的图形的内部,有多少个0连通块。

首先先把16进制转成二进制的,然后先按图把边上的0全染上色,这样之后就不会计算到最边框的0了;

然后在染1的时候,顺带就找到内部连通块并且染上色了。


开始没看到字典序输出。。然后改成char又错了一波  '\0'。。

不是第一次犯这种错了。。。妈蛋


代码:

#pragma comment(linker, "/STACK:1677721600")
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cassert>
#include <iostream>
#include <algorithm>
#define pb push_back
#define mp make_pair
#define LL long long
#define lson lo,mi,rt<<1
#define rson mi+1,hi,rt<<1|1
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define FIN freopen("in.txt", "r", stdin)
#define FOUT freopen("out.txt", "w", stdout)
#define rep(i,a,b) for(int i=(a); i<=(b); i++)
#define dec(i,a,b) for(int i=(a); i>=(b); i--)

using namespace std;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double ee = exp(1.0);
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);

int readT()
{
    char c;
    int ret = 0,flg = 0;
    while(c = getchar(), (c < '0' || c > '9') && c != '-');
    if(c == '-') flg = 1;
    else ret = c ^ 48;
    while( c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c ^ 48);
    return flg ? - ret : ret;
}

LL readTL()
{
    char c;
    int flg = 0;
    LL ret = 0;
    while(c = getchar(), (c < '0' || c > '9') && c != '-');
    if(c == '-') flg = 1;
    else ret = c ^ 48;
    while( c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c ^ 48);
    return flg ? - ret : ret;
}

const int maxh = 200 + 10;
const int maxw = 50 + 10;
char name[] = "WAKJSD";
int dir[][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
int charToNum[][4] =
{
    {0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 1, 0}, {0, 0, 1, 1},
    {0, 1, 0, 0}, {0, 1, 0, 1}, {0, 1, 1, 0}, {0, 1, 1, 1},
    {1, 0, 0, 0}, {1, 0, 0, 1}, {1, 0, 1, 0}, {1, 0, 1, 1},
    {1, 1, 0, 0}, {1, 1, 0, 1}, {1, 1, 1, 0}, {1, 1, 1, 1}
};

int h, w;
char tmp[maxh][maxw];
int maze[maxh << 2][maxw << 2];
int cnt;

void trans(char c, int x, int y)
{
    int index;
    if ('0' <= c && c <= '9')
    {
        index = c - '0';
    }
    else
    {
        if (c == 'a')
            index = 10;
        if (c == 'b')
            index = 11;
        if (c == 'c')
            index = 12;
        if (c == 'd')
            index = 13;
        if (c == 'e')
            index = 14;
        if (c == 'f')
            index = 15;
    }
    for (int i = 0; i < 4; i++)
    {
        maze[x][(y << 2) + i] = charToNum[index][i];
    }
}

void dfsColor(int x, int y)
{
    if (0 <= x && x < h && 0 <= y && y < w && maze[x][y] == 0)
    {
        maze[x][y] = 2;
        for (int i = 0; i < 4; i++)
        {
            int nx = x + dir[i][0];
            int ny = y + dir[i][1];
            dfsColor(nx, ny);
        }
    }
    return ;
}

void dfsBlock(int x, int y)
{
    if (0 <= x && x < h && 0 <= y && y < w && maze[x][y] == 1)
    {
        maze[x][y] = 2;
        for (int i = 0; i < 4; i++)
        {
            int nx = x + dir[i][0];
            int ny = y + dir[i][1];
            if (0 <= nx && nx < h && 0 <= ny && ny < w && maze[nx][ny] == 0)
            {
                cnt++;
                dfsColor(nx, ny);
            }
            dfsBlock(nx, ny);
        }
    }
    return ;
}

int main()
{
#ifdef LOCAL
    FIN;
#endif // LOCAL
    int ca = 1;
    while (~scanf("%d%d", &h, &w))
    {
        if (!h && !w)
            break;
        for (int i = 0; i < h; i++)
        {
            scanf("%s", tmp[i]);
            for (int j = 0; j < w; j++)
            {
                trans(tmp[i][j], i, j);
            }
        }
        w <<= 2;

//        for (int i = 0; i < h; i++)
//        {
//            for (int j = 0; j < w; j++)
//            {
//                cout << maze[i][j];
//            }
//            puts("");
//        }
//        puts("");

        for (int i = 0; i < w; i++)
        {
            if (maze[0][i] == 0)
            {
                dfsColor(0, i);
            }
            if (maze[h - 1][i] == 0)
            {
                dfsColor(h - 1, i);
            }
        }
        for (int i = 0; i < h; i++)
        {
            if (maze[i][0] == 0)
            {
                dfsColor(i, 0);
            }
            if (maze[i][w - 1] == 0)
            {
                dfsColor(i, w - 1);
            }
        }

//        for (int i = 0; i < h; i++)
//        {
//            for (int j = 0; j < w; j++)
//            {
//                cout << maze[i][j];
//            }
//            puts("");
//        }
//        puts("");

        char ans[maxh * maxw];
        int index = 0;
        for (int i = 0; i < h; i++)
        {
            for (int j = 0; j < w; j++)
            {
                if (maze[i][j] == 1)
                {
                    cnt = 0;
                    dfsBlock(i, j);
                    ans[index++] = name[cnt];
                }
            }
        }

//        for (int i = 0; i < h; i++)
//        {
//            for (int j = 0; j < w; j++)
//            {
//                cout << maze[i][j];
//            }
//            puts("");
//        }
//        puts("");
        ans[index] = '\0';
        sort(ans, ans + index);
        printf("Case %d: %s\n", ca++, ans);
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值