[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 = 210; const int MAXW = 65; int H,W; int dx[] = {0, 0, -1, 1}; int dy[] = {1, -1, 0, 0}; char* bin[16]= {"0000","0001","0010","0011","0100","0101","0110","0111" ,"1000","1001","1010","1011","1100","1101","1110","1111"}; char code[7] = "WAKJSD";char pic1[MAXH][MAXW]; char pic[MAXH][MAXW<<2]; int color[MAXH][MAXW<<2];void dfs(int x,int y,int col) {color[x][y] = col;for(int i = 0; i < 4; ++i){int nx = x + dx[i], ny = y + dy[i];if(nx >= 0 && nx < H && ny >= 0 && ny < W&& pic[x][y] == pic[nx][ny] && !color[nx][ny])dfs(nx,ny,col);} } int main() {// freopen("in.txt","r",stdin);// FEint cas = 0;while(scanf("%d%d",&H,&W),H){memset(pic,0,sizeof(pic));memset(color,0,sizeof(color));for(int i = 0; i < H; ++i)scanf("%s",pic1[i]);for(int i = 0 ; i < H; ++i)for(int j = 0; j < W; ++j){if(pic1[i][j]<='9'&&'0'<=pic1[i][j])pic1[i][j] -= '0';elsepic1[i][j] -= 'a' - 10;for(int k = 0; k < 4; ++k){//decodepic[i+1][1+(j<<2)+k] = bin[pic1[i][j]][k] - '0';}}H += 2;W = (W<<2)+2;vector<int> cc;int cnt = 0;//cnt = 1一定是白色背景for(int i = 0 ; i < H; ++i)for(int j = 0; j < W; ++j){if(!color[i][j]) {dfs(i,j,++cnt);if(pic[i][j] == 1) cc.push_back(cnt);//记录黑色所在的连通块 //!!没放到括号里 }}vector< set<int> > neigh(cnt+1);for(int i = 0 ; i < H; ++i)for(int j = 0; j < W; ++j) {if(pic[i][j]){for(int k = 0; k < 4; ++k){int x = i + dx[k], y = j + dy[k];if(x >= 0 && x < H && y >= 0 && y < W&& pic[x][y] == 0 && color[x][y] != 1)neigh[color[i][j]].insert(color[x][y]); }}}vector<char> ans;for(int i = 0 ; i < cc.size(); ++i)ans.push_back(code[neigh[cc[i]].size()]);sort(ans.begin(),ans.end());printf("Case %d: ",++cas);for(int i = 0, sz = ans.size(); i < sz; ++i)printf("%c",ans[i]);puts("");}return 0; }
转载于:https://www.cnblogs.com/jerryRey/p/4596903.html
[uva]AncientMessages象形文字识别 (dfs求连通块)相关推荐
- DFS求连通块数目(深搜)
DFS求连通块数目 这里认为,连通块是包括斜对角线的路径连通的块. 测试数据 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 计算通过@相连的连通块的个数 测试输出: 2 样例代码 ...
- UVa572 Oil Deposits DFS求连通块
技巧:遍历8个方向 for(int dr = -1; dr <= 1; dr++)for(int dc = -1; dc <= 1; dc++)if(dr != 0 || dc != 0) ...
- *【ZOJ - 3781】Paint the Grid Reloaded(dfs求连通块缩点,bfs求最短路,建图技巧)
题干: Leo has a grid with N rows and M columns. All cells are painted with either black or white initi ...
- POJ 2386 dfs求连通块
题目: 由于近期的降雨,雨水汇集在农民约翰的田地不同的地方.我们用一个 的网格图表示.每个网格中有水(W) 或是旱地(.).一个网格与其周围的八个网格相连,而一组相连的网格视为一个水坑.约翰想弄清楚他 ...
- UVa 352 - The Seasonal War ( DFS求连通块 )
思路 基础DFS 和油田那题思路一毛一样 AC代码 #include <iostream> #include <cstdio> #include <cstring> ...
- 求连通块个数(使用并查集)
并查集求连通块个数的模板 #include<bits/stdc++.h>using namespace std;const int maxn = 1e5+5; vector<int& ...
- [蓝桥杯2018初赛]全球变暖-dfs,bfs,连通块
解题思路: bfs:遍历所有未遍历过的陆地,通过bfs计算出当前位置连通陆地的数量cnt,以及被淹没陆地的数量bound,若cnt == bound表示完整淹没的一个岛屿 dfs:将连通块全部标记,如 ...
- 中矿新生赛 H 璐神看岛屿【BFS/DFS求联通块/连通块区域在边界则此连通块无效】...
时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit IO Format: %lld 题目描述 璐神现在有张n*m大小的地图,地图上标明了陆地 ...
- CF1139C Edgy TreesDFS求连通块大小、思维
题目 题目链接 给一颗树,每条边都是红或黑的.问有多少种大小为kkk的序列[a1,a2...,ak][a_1,a_2...,a_k][a1,a2...,ak]满足,从a1a_1a1到a2a_2 ...
最新文章
- windows mobile设置插移动卡没反应_ipad pro外接移动硬盘ipados
- linux libcurl 库使用方法
- mac系统更新后code .命令打不开vs code
- Winsock属性 方法介绍
- 论文中要用到的SPSS基础分析
- mc用云服务器搭建_最全的云服务器架设我的世界私服教程,不看后悔哦!
- wake on lan
- 第 69 章 数据库链接
- matlab中的[~,m]=size(coord)是什么意思
- [转]我对CTO的理解
- 虚拟机不能上网以及无法ping通百度的解决方案
- 用.htaccess 禁止IP访问
- js 面向对象 继承
- php-rabbitmq结合rabbitmq_delayed_message_exchange实现延时队列
- PPT绘图导出高清图
- Rediskey的基本操作
- Excel:数据转置
- 幼儿园清明节活动设计方案
- 环形回路算法(python3)
- mysql键值相同_mysql什么是键值重复
热门文章
- css实现快速抖动效果_web前端入门到实战:CSS实现照片堆叠效果
- 机器人学习--路径规划--A*算法实现
- Python-OpenCV-- 台式机外接摄像头pyTesseract文本框实时检测
- 北斗导航 | 大规模点云地图的自动化构建(高翔:SLAM十四讲)
- matlab图像显示时间,请问怎么把样点数变成时间显示在图像了里
- 加载如下html 写出输出顺序,浏览器加载和渲染html的顺序-结论篇
- SQLite相关知识
- android小米计算器布局,小米这8个逆天小功能一定用起来!不会用,手机简直白买...
- matlab dfe 仿真,用matlab仿真DFE均衡器
- vb.net2019-下载文件