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

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

思路:象形文字有一些特点,分别有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. ArrayList 实现删除重复元素(元素为对象类型)

    package 集合; import java.util.ArrayList;import java.util.Iterator; /* * 删除集合中的重复的元素(元素是对象形式的) *  * Li ...

  2. 第 1 章 jQuery 入门

    学习要点: 1.什么是 jQuery 2.学习 jQuery 的条件 3.jQuery 的版本 4.jQuery 的功能和优势 5.其他 JavaScript 库 6.是否兼容低版本 IE 7.下载及 ...

  3. js--敏感词屏蔽

    <!doctype html><html><head><meta charset="utf-8"><meta name=&qu ...

  4. rownum和rowid伪列

    select row employee_id,last name,salary from employees; select row employee_id,last name,salary from ...

  5. [kuangbin带你飞]专题十 匹配问题 二分匹配部分

    刚回到家 开了二分匹配专题 手握xyl模板 奋力写写写 终于写完了一群模板题 A hdu1045 对这个图进行 行列的重写 给每个位置赋予新的行列 使不能相互打到的位置 拥有不同的行与列 然后左行右列 ...

  6. Myeclipese建立servelet时出现带有javax.servlet.http.HttpServlet;变成了红色的解决方法

    如下图 上面这部分都变成了红色,说明servlet-api.ja还没有导入进来,只需要单击项目名称然后properties--java Build Path--Libraties--MyEclipse ...

  7. mysql查看锁表情况

    mysql> show status like 'Table%'; +----------------------------+----------+ | Variable_name       ...

  8. JQ第一天

    1.jQ中最常用对象是$对象,$是jQ的简写.只有将普通的Dom对象封装成jQ对象,才能使用其中方法,jQuery(document).ready()是允许多个的,$(fn)与$(document). ...

  9. centos下JDK的卸载与安装

    linux是自带JDK的,但是它自带的JDK是openJDK,我们如果需要安装ant之类的软件,使用这个JDK是不行的.所以我们需要卸载linux下自带的JDK,并安装我们准备的JDK. JDK的卸载 ...

  10. 执行命令行, 并获取输出字符(比如OSQL)

    直接贴代码了, 没什么好说的, 很简单, 也不需要注释 function DoCMD(ACommand: AnsiString; var ACmdResult: string): Boolean; v ...