[uva]AncientMessages象形文字识别 (dfs求连通块)
非常有趣的一道题目,大意是给你六种符号的16进制文本,让你转化成二进制并识别出来
代码实现上参考了//http://blog.csdn.net/u012139398/article/details/39533409
#include<cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set> using namespace std; const int MAXH = ;
const int MAXW = ;
int H,W;
int dx[] = {, , -, };
int dy[] = {, -, , };
char* bin[]= {"","","","","","","",""
,"","","","","","","",""};
char code[] = "WAKJSD"; char pic1[MAXH][MAXW];
char pic[MAXH][MAXW<<];
int color[MAXH][MAXW<<]; void dfs(int x,int y,int col)
{
color[x][y] = col;
for(int i = ; i < ; ++i){
int nx = x + dx[i], ny = y + dy[i];
if(nx >= && nx < H && ny >= && ny < W
&& pic[x][y] == pic[nx][ny] && !color[nx][ny])
dfs(nx,ny,col);
}
}
int main()
{
// freopen("in.txt","r",stdin);// FE
int cas = ;
while(scanf("%d%d",&H,&W),H){ memset(pic,,sizeof(pic));
memset(color,,sizeof(color)); for(int i = ; i < H; ++i)
scanf("%s",pic1[i]);
for(int i = ; i < H; ++i)
for(int j = ; j < W; ++j){
if(pic1[i][j]<=''&&''<=pic1[i][j])
pic1[i][j] -= '';
else
pic1[i][j] -= 'a' - ;
for(int k = ; k < ; ++k){//decode
pic[i+][+(j<<)+k] = bin[pic1[i][j]][k] - '';
}
} H += ;
W = (W<<)+;
vector<int> cc;
int cnt = ;//cnt = 1一定是白色背景
for(int i = ; i < H; ++i)
for(int j = ; j < W; ++j){
if(!color[i][j]) {
dfs(i,j,++cnt);
if(pic[i][j] == ) cc.push_back(cnt);//记录黑色所在的连通块 //!!没放到括号里
} } vector< set<int> > neigh(cnt+);
for(int i = ; i < H; ++i)
for(int j = ; j < W; ++j) {
if(pic[i][j]){
for(int k = ; k < ; ++k){
int x = i + dx[k], y = j + dy[k];
if(x >= && x < H && y >= && y < W
&& pic[x][y] == && color[x][y] != )
neigh[color[i][j]].insert(color[x][y]);
}
}
} vector<char> ans;
for(int i = ; i < cc.size(); ++i)
ans.push_back(code[neigh[cc[i]].size()]);
sort(ans.begin(),ans.end()); printf("Case %d: ",++cas);
for(int i = , sz = ans.size(); i < sz; ++i)
printf("%c",ans[i]);
puts("");
}
return ;
}
[uva]AncientMessages象形文字识别 (dfs求连通块)的更多相关文章
- UVa 572 油田(DFS求连通块)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 572 Oil Deposits油田(DFS求连通块)
UVA 572 DFS(floodfill) 用DFS求连通块 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format: ...
- UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)
UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...
- DFS入门之二---DFS求连通块
用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所 ...
- [C++]油田(Oil Deposits)-用DFS求连通块
[本博文非博主原创,均摘自:刘汝佳<算法竞赛入门经典>(第2版) 6.4 图] [程序代码根据书中思路,非独立实现] 例题6-12 油田(Oil Deposits,UVa572) 输入一个 ...
- HDU1241 Oil Deposits —— DFS求连通块
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 Oil Deposits Time Limit: 2000/1000 MS (Java/Othe ...
- 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。
这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...
- UVA 572 dfs求连通块
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...
- 用DFS求连通块(种子填充)
[问题] 输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块.如果两个字符“@”所在的格子相邻(横.竖或者对角线方向),就说它们属于同一个八连块.例如,图6-9中有两个八连块. 图6-9 [分 ...
随机推荐
- Java正则表达式之Matcher介绍
Matcher方法如下: Matcher方法如下: Matcher appendReplacement(StringBuffer sb, String replacement) 将当前匹配子串替换为指 ...
- C# 移除string[] 数组中的指定元素
本文转载自 http://www.cnblogs.com/jcdd-4041/p/3279531.html 第一步:先把string[] 转换成 ArrayList 第二步:移除指定元素 第三步 ...
- WPF后台修改内容界面不显示问题
通知3部曲:1.Model继承并实现 INotifyPropertyChanged 接口:2.数据集合使用ObservableCollection<T>集合:3.View使用Binding ...
- 【Java面试题系列】:Java基础知识常见面试题汇总 第二篇
文中面试题从茫茫网海中精心筛选,如有错误,欢迎指正! 第一篇链接:[Java面试题系列]:Java基础知识常见面试题汇总 第一篇 1.JDK,JRE,JVM三者之间的联系和区别 你是否考虑过我们写的x ...
- Automake使用(高级)
工程地址 automake语言国际化 最初工程目录结构 $ ls -l total 16 drwxrwxr-x. 2 fedora fedora 4096 May 10 10:38 build-aux ...
- js中的原型以及原型链
在js中原型是每个构造函数的属性: 这个算 js 核心概念的一部分 var f1 = new Foo(); 对象 f1 的构造函数就是 Foo , f1的原型 __proto__ 就指向构造函数 Fo ...
- 非常实用的Sublime插件集合 – sublime推荐必备插件
插件介绍 ***PackageControl*** 功能:安装包管理 简介:sublime插件控制台,提供添加.删除.禁用.查找插件等功能 使用方法: 1.安装好控制台,如有不能正常调用 Packag ...
- 微信小程序-工具无法加载本地模拟开发服务的解决办法
微信小程序开发工具出现如下问题: 因为网络代理软件或者 VPN 影响,工具无法加载本地模拟开发服务 请尝试以下任一解决方案1.关闭相关网络代理软件,重新编译成功后,再启动相关网络代理软件: 2.配置 ...
- Jeasyui的datagrid前端分页要点
Jeasyui的分页有两种方式: 1. 服务器端分页,是真正的分页,datagridview的pager会自动把pageSize和pageNum传到后台,后台根据根据pageSize和pageNum构 ...
- php删除文件
unlink() 函数删除文件. 若成功,则返回 true,失败则返回 false. unlink里的参数需要文件的绝对路径