uva 1103

14 篇文章 0 订阅

弄懂题意后  其实就是一个dfs 

/*************************************************************************
    > Author: xlc2845       > Mail: xlc2845@gmail.com
    > Created Time: 2013年10月22日 星期二 16时20分43秒
 ************************************************************************/

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#define maxn 210

using namespace std;

int g[maxn][maxn];
int n, m, cnt;
int num[6];
char st[6] = {'A', 'D', 'J', 'K', 'S', 'W'};
char str[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
int s[16][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}
};
bool is_in(int x, int y)
{
    if(x >= 0 && x <= n+1 && y >= 0 && y <= m+1)
        return true;
    return false;
}
void dfs(int x, int y)
{
    if(!is_in(x, y) || g[x][y] != 0)
        return;
    g[x][y] = -1;
    dfs(x + 1, y);
    dfs(x - 1, y);
    dfs(x, y - 1);
    dfs(x, y + 1);
}

void dfs2(int x, int y)
{
    if(!is_in(x, y) || g[x][y] == -1)
        return;
    if(!g[x][y])
    {
        dfs(x, y);
        cnt++;
        return;
    }
    g[x][y] = -1;
    dfs2(x + 1, y);
    dfs2(x - 1, y);
    dfs2(x, y - 1);
    dfs2(x, y + 1);
}

int main()
{
    int ca = 1;
    while(scanf("%d%d", &n, &m) == 2 && n+m)
    {
        memset(g, 0, sizeof(g));
        memset(num, 0, sizeof(num));
        for(int i = 1; i <= n; i++)
        {
            getchar();
            char ch;
            int len = 1;
            for(int j = 0; j < m; j++)
            {
                scanf("%c", &ch);
                for(int k = 0; k < 16; k++)
                    if(ch == str[k])
                    {
                        for(int l = 0; l < 4; l++)
                            g[i][len++] = s[k][l];
                        break;
                    }
            }
        }
        m *= 4;
        dfs(0, 0);
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++)
                if(g[i][j] == 1)
                {
                    cnt = 0;
                    dfs2(i, j);
                    if(!cnt) num[5]++;
                    else if(cnt == 1) num[0]++;
                    else if(cnt == 2) num[3]++;
                    else if(cnt == 3) num[2]++;
                    else if(cnt == 4) num[4]++;
                    else num[1]++;
                }
        printf("Case %d: ", ca++);
        for(int i = 0; i < 6; i++)
            while(num[i]--)
                printf("%c", st[i]);
        puts("");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值