题目大意:给出一幅画,找出里面的象形文字。

要你翻译这幅画,把象形文字按字典序输出。

思路:象形文字有一些特点,分别有0个圈、1个圈、2个圈...5个圈。然后dfs或者bfs,就像油井问题一样,找出在同一块的0,找出在同一块的1,分别标上记号。

对于每个同一块的1,如果它们只和1个‘0’的块相邻,就表明这个象形文字没有圈。如果和2个‘1’的块相邻,就说明这个象形文字有一个圈。依此类推...和6个‘1’块相邻的就有五个圈。

最后统计一下每个象形文字和多少不同的块相邻,排一个序,输出。 要注意的是,处理输入的时候,给读入的图的周围留一个‘0’构成的圈。为什么呢?这个自己想。

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <map>
using namespace std;
int H,W;
char Map[][];
int vis[][];
int chr[][];
int wblc_cnt,bblc_cnt;
char hie[]={'W','A','K','J','S','D'};
int dx[]={,,,-};
int dy[]={,-,,};
map<int,bool> Hsh[];
int bccnt[];
char ans[];
int comp(const void *_a,const void *_b)
{
char a=*(char *)_a;
char b=*(char *)_b;
return a-b;
} void dfs1(int x,int y)
{
vis[x][y]=wblc_cnt;
int nx,ny;
for(int i=;i<;i++){
nx=dx[i]+x;
ny=dy[i]+y;
if(nx>=&&nx<H&&ny>=&&ny<W && !vis[nx][ny] && Map[nx][ny]=='')dfs1(nx,ny);
}
}
void dfs2(int x,int y)
{
chr[x][y]=bblc_cnt;
int nx,ny;
for(int i=;i<;i++){
nx=dx[i]+x;
ny=dy[i]+y;
if(nx>=&&nx<H&&ny>=&&ny<W&&!chr[nx][ny]){
if(Map[nx][ny]==''){
if(Hsh[bblc_cnt].find(vis[nx][ny]) != Hsh[bblc_cnt].end())continue;
Hsh[bblc_cnt].insert(pair<int,bool>(vis[nx][ny],true));
}else dfs2(nx,ny);
}
}
}
int main()
{
char str[];
int kase=;
while(~scanf("%d%d",&H,&W)&&(H+W)){
printf("Case %d: ",kase++);
getchar();
memset(Map,,sizeof(Map));
memset(vis,,sizeof(vis));
memset(chr,,sizeof(chr));
for(int i=;i<=*W+;i++)Map[][i]=Map[H+][i]='';
for(int i=;i<=H;i++){
gets(str);
for(int j=;j<;j++)
Map[i][j]=Map[i][+W*+j]='';
for(int j=;j<W;j++){
int x;
if(''<=str[j] && str[j]<='')
x=str[j]-'';
else
x=+str[j]-'a';
for(int k=;k<;k++){
if(x & <<(-k)) Map[i][+j*+k]='';
else Map[i][+j*+k]='';
}
}
}
W+=;W*=;
H+=;
wblc_cnt=;
for(int i=;i<H;i++)for(int j=;j<W;j++)
if(vis[i][j]== && Map[i][j]==''){
wblc_cnt++;
dfs1(i,j);
}
bblc_cnt=;
for(int i=;i<H*W;i++) Hsh[i].clear();
for(int i=;i<H;i++)for(int j=;j<W;j++)
if(chr[i][j]== && Map[i][j]==''){
bblc_cnt++;
dfs2(i,j);
}
for(int i=;i<=bblc_cnt;i++)
bccnt[i]=Hsh[i].size();
for(int i=;i<=bblc_cnt;i++)
ans[i]=hie[bccnt[i]-];
qsort(ans+,bblc_cnt,sizeof(char),comp);
ans[bblc_cnt+]='\0';
puts(ans+);
}
return ;
}

hdu 3839 Ancient Messages (dfs )的更多相关文章

  1. HDU 3839 Ancient Messages(DFS)

    In order to understand early civilizations, archaeologists often study texts written in ancient lang ...

  2. 2017ACM暑期多校联合训练 - Team 1 1003 HDU 6035 Colorful Tree (dfs)

    题目链接 Problem Description There is a tree with n nodes, each of which has a type of color represented ...

  3. hdu 1716 排序2(dfs)

    排列2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. hdu 2660 Accepted Necklace(dfs)

    Problem Description I have N precious stones, and plan to use K of them to make a necklace for my mo ...

  5. HDU 4414 Finding crosses(dfs)

    Problem Description The Nazca Lines are a series of ancient geoglyphs located in the Nazca Desert in ...

  6. hdu 1241:Oil Deposits(DFS)

    Oil Deposits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  7. UVA - 1103Ancient Messages(dfs)

    UVA - 1103Ancient Messages In order to understand early civilizations, archaeologists often study te ...

  8. HDU 6351 Beautiful Now(DFS)多校题解

    思路:一开始对k没有理解好,题意说交换k次,如果我们不需要交换那么多,那么可以重复自己交换自己,那么k其实可以理解为最多交换k次.这道题dfs暴力就行,我们按照全排列最大最小去找每一位应该和后面哪一位 ...

  9. HDU 5952 Counting Cliques(dfs)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

随机推荐

  1. Screen Orientation for Windows Phone

    http://msdn.microsoft.com/en-us/library/windows/apps/jj207002(v=vs.105).aspx

  2. unity3d的GUI元素的界面坐标系统总结(有公式)

    GUIText 和GUITexture 1.GUIText 锚点(Anchor)的概念我就不介绍了.像NGUI和tookit2d还有 Cocos2d中都有这个重要的概念,对于图片我们可以认为是图片自身 ...

  3. 对拍老是忘记的看这里:bat代码

    需要写三个程序,makedata.exe 产生测试数据, program1.exe 是你要检测的程序,program2.exe 往往是一个正确但效率不高(暴力的居多)的程序. 代码很简单,稍作解释:l ...

  4. finally类

    finally叫做最后的执行快,什么是最后的执行快?他的意思是这样的 他是写在try catch 的后面但是只能写一个,他设计这个finally的意思就是,如果try里面出错肯定会往陷阱里 面跑.没有 ...

  5. 第一个Spring Boot Web程序

    需要的环境和工具: 1.Eclipse2.Java环境(JDK 1.7或以上版本)3.Maven 3.0+(Eclipse已经内置了) 写个Hello Spring: 1.新建一个Maven项目,项目 ...

  6. tomcatPluginV321.zip

    下载地址:http://www.eclipsetotale.com/tomcatPlugin/tomcatPluginV321.zip 或者  http://files.cnblogs.com/fil ...

  7. 【php】命名空间 和 自动加载的关系

    目的 本文的目的主要是说明 命名空间的 use 关键词 和 new ClassName 这两个步骤,哪个步骤才会执行自动加载,这是逻辑有点混乱的表现,这种想法也是很正常的,让我们来解密吧 命名空间(n ...

  8. php 后端开发学习

    hello.php: <?php //设置cookies setcookie("user", "runoob", time()+3600); ?> ...

  9. 使用Ajax异步加载页面时,怎样调试该页面的Js

    前言-本人不是干前端的,所以有的名词不专业 在前端中,有时候会遇到这样的框架,http://172.17.11.151:8060/frontend/backend.html#1.html (通过解析U ...

  10. vs2010 2013 2015+ 必备插件精选(15个)

    转 http://www.spersky.com/post/vsPlugins.html 我目前主要用的是Hide Main Page——公司配给的电脑屏幕分辨率好小,还是1366*768的,去掉头可 ...