题意:识别图中的象形文字。但是,图形可以任意的拉伸,但不能拉断。

分析:这种题如果图形没有特征是不可做类型的题,不过观察图形可以发现每个图形中的洞的数量是一定的,我们只需要数出每一个封闭图形的洞数就能知道这是哪个图形.

虽然知道了原理,但是并不是特别好做,首先我们需要一次dfs将所有图形旁边的点全都变为“不可访问”,然后从每个黑点开始枚举,向四周扩展,遇到白色的块就用第一次的dfs函数覆盖,否则继续第二次dfs,两次dfs交错使用,思路比较巧妙.

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. //0表示空白位置,-1表示不能访问了.
  9.  
  10. const int maxn = ;
  11.  
  12. int n, m,kase,a[maxn][maxn],flag[maxn][maxn],cnt,num[maxn];
  13. char s16[] = { '', '', '', '', '', '', '', '', '', '', 'a', 'b', 'c', 'd', 'e', 'f' };
  14. char fuhao[] = { 'A', 'D', 'J', 'K', 'S', 'W' };
  15. int s2[][] =
  16. {
  17. { , , , }, { , , , }, { , , , }, { , , , },
  18. { , , , }, { , , , }, { , , , }, { , , , },
  19. { , , , }, { , , , }, { , , , }, { , , , },
  20. { , , , }, { , , , }, { , , , }, { , , , }
  21. };
  22.  
  23. void dfs1(int x,int y)
  24. {
  25. if (x < || x > n + || y < || y > m + || a[x][y] != )
  26. return;
  27. a[x][y] = -;
  28. dfs1(x - , y);
  29. dfs1(x + , y);
  30. dfs1(x, y - );
  31. dfs1(x, y + );
  32. }
  33.  
  34. void dfs2(int x, int y)
  35. {
  36. if (x < || x > n + || y < || y > m + || a[x][y] == -)
  37. return;
  38. if (a[x][y] == )
  39. {
  40. cnt++;
  41. dfs1(x, y);
  42. return;
  43. }
  44. a[x][y] = -;
  45. dfs2(x - , y);
  46. dfs2(x + , y);
  47. dfs2(x, y - );
  48. dfs2(x, y + );
  49. }
  50.  
  51. int main()
  52. {
  53. while (scanf("%d%d", &n, &m) == && (n || m))
  54. {
  55. memset(a, , sizeof(a));
  56. memset(num, , sizeof(num));
  57. for (int i = ; i <= n; i++)
  58. {
  59. getchar();
  60. char ch;
  61. int tot = ;
  62. for (int j = ; j <= m; j++)
  63. {
  64. scanf("%c", &ch);
  65. for (int k = ; k < ; k++)
  66. {
  67. if (ch == s16[k])
  68. {
  69. for (int l = ; l < ; l++)
  70. a[i][++tot] = s2[k][l];
  71. break;
  72. }
  73. }
  74. }
  75. }
  76. m *= ;
  77. dfs1(, );
  78. for (int i = ; i < n; i++)
  79. for (int j = ; j < m; j++)
  80. if (a[i][j] == )
  81. {
  82. cnt = ;
  83. dfs2(i, j);
  84. if (cnt == )
  85. num[]++;
  86. if (cnt == )
  87. num[]++;
  88. if (cnt == )
  89. num[]++;
  90. if (cnt == )
  91. num[]++;
  92. if (cnt == )
  93. num[]++;
  94. if (cnt == )
  95. num[]++;
  96. }
  97. printf("Case %d: ", ++kase);
  98. for (int i = ; i <= ; i++)
  99. {
  100. while (num[i]--)
  101. printf("%c", fuhao[i]);
  102. }
  103. printf("\n");
  104. }
  105.  
  106. return ;
  107. }

Uva1103 Ancient Messages的更多相关文章

  1. K - Ancient Messages(dfs求联通块)

    K - Ancient Messages Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Subm ...

  2. UVA1103 古代象形符号 Ancient Messages 题解

    题目链接: https://www.luogu.org/problemnew/show/UVA1103 题目分析: 我们可以先进行矩阵的还原 for(int k=1;k<=4;k++) { a[ ...

  3. UVa 1103 Ancient Messages(二重深搜)

    In order to understand early civilizations, archaeologists often study texts written in ancient lang ...

  4. Ancient Messages UVA - 1103

    题目链接:https://vjudge.net/problem/UVA-1103 题目大意:每组数据包含H行W列的字符矩阵(H<=200,W<=50) 每个字符为为16进制  你需要把它转 ...

  5. HDU 3839 Ancient Messages(DFS)

    In order to understand early civilizations, archaeologists often study texts written in ancient lang ...

  6. hdu 3839 Ancient Messages (dfs )

    题目大意:给出一幅画,找出里面的象形文字. 要你翻译这幅画,把象形文字按字典序输出. 思路:象形文字有一些特点,分别有0个圈.1个圈.2个圈...5个圈.然后dfs或者bfs,就像油井问题一样,找出在 ...

  7. UVa 1103 (利用连通块来判断字符) Ancient Messages

    本题就是灵活运用DFS来求连通块来求解的. 题意: 给出一幅黑白图像,每行相邻的四个点压缩成一个十六进制的字符.然后还有题中图示的6中古老的字符,按字母表顺序输出这些字符的标号. 分析: 首先图像是被 ...

  8. Uva 1103 Ancient Messages

    大致思路是DFS: 1. 每个图案所包含的白色连通块数量不一: Ankh : 1 ;  Wedjat : 3  ; Djed : 5   ;   Scarab : 4 ; Was : 0  ;  Ak ...

  9. 【例题 6-13 UVA - 1103】Ancient Messages

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每个图案里面的"洞"的个数都是不同的. 则可以根据这个判别每个图像是什么. 先用dfs确定轮廓之后. 再从每个白 ...

随机推荐

  1. 洛谷 P2142 高精度减法(模板)

    题目描述 高精度减法 输入输出格式 输入格式: 两个整数a,b(第二个可能比第一个大) 输出格式: 结果(是负数要输出负号) 输入输出样例 输入样例#1: 2 1 输出样例#1: 1 说明 20%数据 ...

  2. laravel 模型 $table $guarded $hidden

     首先以App\User模型为例 1.$table属性 表名,对应数据库中的表名 2.guarded)属性 guarded表示在create()方法中不能被赋值的字段 3.$hidden属性 $hid ...

  3. 三分 POJ 2420 A Star not a Tree?

    题目传送门 /* 题意:求费马点 三分:对x轴和y轴求极值,使到每个点的距离和最小 */ #include <cstdio> #include <algorithm> #inc ...

  4. Cesium加载影像

    注意:使用自定义数据源时,Cesium.Viewer类参数必须设置为 baseLayerPicker:false A. 使用天地图数据源 //天地图var provider=new Cesium.We ...

  5. Spring Boot (28) actuator与spring-boot-admin

    在上一篇中,通过restful api的方式查看信息过于繁琐,也不直观,效率低下.当服务过多的时候看起来就过于麻烦,每个服务都需要调用不同的接口来查看监控信息. SBA SBA全称spring boo ...

  6. Angular——$http

    基本介绍 $http用于向服务端发起异步请求,同时还支持多种快捷方式如$http.get().$http.post().$http.jsonp.$hhtp也是属于内置服务的一种,这里特意提出来写一篇用 ...

  7. HTML meta信息含义

    <meta name="viewport" content="width=device-width,initial-scale=1.0"> cont ...

  8. C++调用Com

    需求:1.创建myCom.dll,该COM只有一个组件,两个接口:   IGetRes--方法Hello(),   IGetResEx--方法HelloEx() 2.在工程中导入组件或类型库  #im ...

  9. centos7服务器安装fail2ban配合Firewalld防护墙防止SSH爆破与防护网站CC攻击

    centos7服务器安装fail2ban配合Firewalld防护墙防止SSH爆破与防护网站CC攻击 1.检查firewalld是否启用 #如果您已经安装iptables建议先关闭 service i ...

  10. CAD在一个点构造选择集

    主要用到函数说明: IMxDrawSelectionSet::SelectAtPoint 在一个点构造选择集.详细说明如下: 参数 说明 [in] IMxDrawPoint* point 点坐标 [i ...