UVA 1103 Ancient Messages DFS

题意:识别图中的象形文字。但是,图形可以任意的拉伸,但不能拉断。

思路:图形任意可以拉伸,但不能拉断。这是个很蛋疼的条件。我们必须找到易于计算,满足上述条件的特征量。仔细观察后,可以发现,特征量就是每个字符中间有几个白洞。

           我们就可以用DFS来对不同的联通块染成不同的颜色。(这个叫做Floodfill)

           但是,如果区分外面的白洞和里面的白洞呢?我们可以这样,将整个图向外再扩一行(为了保证连通),全填为0。所以,我们就可以先染最外围的色(一定可以)。

           同时,我们记录那些染色标号是对应文字的。然后,我们对于每个文字再次BFS,判断里面有多少白洞。

           重复统计可以用bool数组,或者用set。

代码如下:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <vector>
#include <set>

using namespace std;

const int MAXH = 210;
const int MAXW = 100;

int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
char* bin[]= {"0000","0001","0010","0011","0100","0101","0110","0111",
            "1000","1001","1010","1011","1100","1101","1110","1111"};
char code[] = "WAKJSD";

int W,H;
char pic1[MAXH][MAXW];
char pic[MAXH][MAXW<<2];
int color[MAXH][MAXW<<2];

void dfs(int x, int y, int col)
{
    color[x][y] = col;
    for(int i = 0; i < 4; ++i){
        int xx = x + dx[i], yy = y + dy[i];
        if(xx >= 0 && xx < H && yy >= 0 && yy < W
           && pic[x][y] == pic[xx][yy] && !color[xx][yy]){
            dfs(xx,yy,col);
           }
    }
}

int main(void)
{
    //freopen("input.txt","r",stdin);
    int cas = 1;
    while(scanf("%d %d", &H, &W), H || W){

        memset(pic,0,sizeof(pic));
        memset(color,0,sizeof(color));

        for(int i = 0; i < H; ++i)
            scanf("%s",pic1[i]);
        for(int i = 0 ; i < H; ++i){
            for(int j = 0; j < W; ++j){
                if(isdigit(pic1[i][j]))
                    pic1[i][j] -= '0';
                else
                    pic1[i][j] -= 'a' - 10;
                for(int k = 0; k < 4; ++k)
                    pic[i+1][1 + 4 * j + k] = bin[pic1[i][j]][k] - '0';
            }
        }

        H += 2;
        W = 4 * W + 2;
        int cnt = 0;
        vector<int> cc;
        for(int i = 0; i < H; ++i){
            for(int j = 0; j < W; ++j){
                if(!color[i][j]){
                    dfs(i,j,++cnt);
                    if(pic[i][j] == 1) cc.push_back(cnt);
                }
            }
        }

        vector<set<int> > neigh(cnt+1);
        for(int i = 0; i < H; ++i){
            for(int j = 0; j < W; ++j){
               if(pic[i][j]){
                    for(int k = 0; k < 4; ++k){
                        int x = i + dx[k], y = j + dy[k];
                        if(x >= 0 && x < H && y >= 0 && y < W &&
                           pic[x][y] == 0 && color[x][y] != 1)
                            neigh[color[i][j]].insert(color[x][y]);
                    }
                }
            }
        }

        vector<char> ans;
        for(int i = 0, sz = cc.size(); i < sz; ++i)
            ans.push_back(code[neigh[cc[i]].size()]);
        sort(ans.begin(),ans.end());

        printf("Case %d: ",cas++);
        for(int i = 0, sz = ans.size(); i < sz; ++i)
            printf("%c",ans[i]);
        puts("");

    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值