hdu 3839 Ancient Messages (dfs )
题目大意:给出一幅画,找出里面的象形文字。
要你翻译这幅画,把象形文字按字典序输出。
思路:象形文字有一些特点,分别有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 )的更多相关文章
- HDU 3839 Ancient Messages(DFS)
In order to understand early civilizations, archaeologists often study texts written in ancient lang ...
- 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 ...
- hdu 1716 排序2(dfs)
排列2 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 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 ...
- HDU 4414 Finding crosses(dfs)
Problem Description The Nazca Lines are a series of ancient geoglyphs located in the Nazca Desert in ...
- hdu 1241:Oil Deposits(DFS)
Oil Deposits Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- UVA - 1103Ancient Messages(dfs)
UVA - 1103Ancient Messages In order to understand early civilizations, archaeologists often study te ...
- HDU 6351 Beautiful Now(DFS)多校题解
思路:一开始对k没有理解好,题意说交换k次,如果我们不需要交换那么多,那么可以重复自己交换自己,那么k其实可以理解为最多交换k次.这道题dfs暴力就行,我们按照全排列最大最小去找每一位应该和后面哪一位 ...
- HDU 5952 Counting Cliques(dfs)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
随机推荐
- 10.this关键字
①在类的方法定义中使用的this关键字代表使用该方法的对 象的引用 ②当必须指出当前使用方法的对象是谁时要使用this ③有时使用this处理方法中成员变量和参数重名的情况 ④this可以看做是一个变 ...
- navicat使用
navicat我觉得做程序的基本上都会用,它方便,快捷,直观等,优点很多,这也是我写这篇文章的原因.以前我基本上都是用phpmyadmin,也挺好用,不过也有不少缺点,比如数据库备份文件太大,根本 ...
- jqgrid no url reset
如果发现 jqgrid 在运行中出现次错误,可能是以下原因 $('#@(ViewBag.tabcid + "_grid")').jqGrid('navGrid', '#@(View ...
- html5新特性
这一篇博文不会告诉你怎么去使用html5的新特性,只会给你总结一下新特性------对于好学的人可以把这篇文章当做一个目录 对于初接触的人来说是一个导向 对于已经接触过的人来说是一个检测你掌握程度的检 ...
- 【emWin】例程十:bmp图片显示
实验指导书及代码包下载: 链接:http://pan.baidu.com/s/1i5fr2Ep 密码:vlvt 实验现象:
- 【emWin】例程二:显示“hello,world”
实验指导书及代码包下载: http://pan.baidu.com/s/1c1Csx48
- 神奇的 CURL 命令
CURL? 嗯,说来话长了~~~~ 这东西现在已经是苹果机上内置的命令行工具之一了,可见其魅力之一斑 1) 二话不说,先从这里开始吧! curl http: //www.yahoo.com 回车之 ...
- quicksort
快排.... void quicksort(int *a,int left,int right){ if(left >= right){ return ; } int i = left; int ...
- OpenGL中实现双缓冲技术
在OpenGL中实现双缓冲技术的一种简单方法: 1.在调用glutInitDisplayMode函数时, 开启GLUT_DOUBLE,即glutInitDisplayMode(GLUT_RGB | G ...
- easyui datagrid 编辑模式详解
一,建立编辑器 从api得知,扩展一种新的编辑器类型,需要提供以上几个方法.项目中正好需要一个checkbox 类型编辑器,但在easyui中并没提供这样的编辑器,那我们可以通过扩展编辑器来解决 ...