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

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

思路:象形文字有一些特点,分别有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. pushState与ajax实现无刷新加载

    一.JS代码: $(document).ready(function() { getContent();//初始化页面 $("nav a").click(function() { ...

  2. css实现 当鼠标移到input的时候,input框出现阴影,当移动到button的时候,input框的阴影消失,button框出现阴影

    <meta charset="utf-8" /> <style type="text/css"> div{overflow: hidde ...

  3. 人工神经网络NN

    [面向代码]学习 Deep Learning系列 http://blog.csdn.net/coolluyu/article/details/20214617 正则化的最小二乘法 深入浅出LSTM神经 ...

  4. stl循环删除

    struct st_data { st_data(int i) : id(i) {} int id; }; 对于STL标准序列容器vector/deque/list(以vector为例) 当我们需清空 ...

  5. InnoDB与Myisam的六大区别

    MyISAM     InnoDB   构成上的区别:   每个MyISAM在磁盘上存储成三个文件.第一个文件的名字以表的名字开始,扩展名指出文件类型. .frm文件存储表定义. 数据文件的扩展名为. ...

  6. matlab函数大全

    Matlab 图像处理相关函数命令大全 一.通用函数: colorbar  显示彩色条 语法:colorbar \ colorbar('vert') \ colorbar('horiz') \ col ...

  7. php 截取中文字符串 - ord()函数 0xa0...

    在ASCII中,0xa0表示汉字的开始 其中php中的一个函数ord()函数 此函数功能返回一个字符的askii码值: 如ord('A')=65; <?php function GBsubstr ...

  8. Autofac

    程序集准备 Assembly: Autofac/Autofac.Integration.Mvc/System.Web.Mvc/System.Web.Helpers/System.Web.WebPage ...

  9. C# 控制datagridview的combox属性的列绑定数据

    //datagridvie列绑定list的数据 List<User> listChange = GetChange();//查询数据库内容,保存到list this.datagridvie ...

  10. iOS WKWebView的javascript alert 不弹的解决方案

    - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiate ...