UVA1103-Ancient Messages(脑洞+dfs)
Problem UVA1103-Ancient Messages
Accept: 1176 Submit: 6103
Time Limit: 3000 mSec
Problem Description
Input
Output
Sample Input
100 25 0000000000000000000000000
0000000000000000000000000
...(50 lines omitted)...
00001fe0000000000007c0000
00003fe0000000000007c0000
...(44 lines omitted)...
0000000000000000000000000
0000000000000000000000000
150 38
00000000000000000000000000000000000000
00000000000000000000000000000000000000
...(75 lines omitted)...
0000000003fffffffffffffffff00000000000
0000000003fffffffffffffffff00000000000
...(69 lines omitted)...
00000000000000000000000000000000000000
00000000000000000000000000000000000000
0 0
Sample output
Case 1: AKW
Case 2: AAAAA
题解:这个题重在脑洞,看出来联通块的个数与字母之间的关系的确不容易(我是没看出来)。开了脑洞之后问题就简单多了,先对最外面的白色区域dfs一下,然后就是dfs各个黑色区域,先把他们包围的联通块的范围划出来,然后计数里面的联通块个数,说的时候这划区域与计数是分开的,其实实现的时候是在一起的,划区域的时候如果碰到了白格,就停下来先对这个白格所在的联通块进行dfs,顺便记个数,区域划完了,计数也就搞定了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std; const int maxn = +,maxm = +;
const int converse[][] =
{
{,,,},{,,,},{,,,},{,,,},
{,,,},{,,,},{,,,},{,,,},
{,,,},{,,,},{,,,},{,,,},
{,,,},{,,,},{,,,},{,,,}
}; char gra[maxn][maxm];
int buf[maxn][maxm<<];
int n,m,cnt;
int dir[][] = {{,},{,},{-,},{,-}}; inline bool Judge(int x,int y){
if(<=x && <=y && x<n && y<m) return true;
else return false;
} void dfsw(int x,int y,int num){
buf[x][y] = num;
int xx,yy;
for(int i = ;i < ;i++){
xx = x+dir[i][],yy = y+dir[i][];
if(Judge(xx,yy) && buf[xx][yy] == ){
dfsw(xx,yy,num);
}
}
} void dfsn(int x,int y,int num){
buf[x][y] = num;
int xx,yy;
for(int i = ;i < ;i++){
xx = x+dir[i][],yy = y+dir[i][];
if(Judge(xx,yy)){
if(buf[xx][yy] == ) dfsn(xx,yy,num);
else if(buf[xx][yy] == ){
cnt++;
dfsw(xx,yy,-);
}
}
}
} void map_print(){
for(int i = ;i < n;i++){
for(int j = ;j < m;j++){
printf("%d ",buf[i][j]);
}
printf("\n");
}
} int sum[];
char q[] = {'W','A','K','J','S','D'};
int iCase = ; int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
while(~scanf("%d%d",&n,&m) && (n||m)){
memset(buf,,sizeof(buf));
for(int i = ;i <= n;i++){
scanf("%s",gra[i]+);
for(int j = ;j <= (int)strlen(gra[i]+);j++){
int y = (j-)<<;
if('a'<=gra[i][j] && gra[i][j]<='z'){
for(int k = ;k < ;k++){
buf[i][y+k+] = converse[gra[i][j]-'a'+][k];
}
}
else{
for(int k = ;k < ;k++){
buf[i][y+k+] = converse[gra[i][j]-''][k];
}
}
}
}
n = n+,m = *m+;
//map_print();
dfsw(,,-);
int flag = ;
memset(sum,,sizeof(sum));
for(int i = ;i < n;i++){
for(int j = ;j < m;j++){
if(buf[i][j] == ){
cnt = ;
dfsn(i,j,flag);
flag++;
sum[cnt]++;
}
}
}
//map_print();
printf("Case %d: ",iCase++);
if(sum[] != ) for(int i = ;i < sum[];i++) printf("%c",'A');
if(sum[] != ) for(int i = ;i < sum[];i++) printf("%c",'D');
if(sum[] != ) for(int i = ;i < sum[];i++) printf("%c",'J');
if(sum[] != ) for(int i = ;i < sum[];i++) printf("%c",'K');
if(sum[] != ) for(int i = ;i < sum[];i++) printf("%c",'S');
if(sum[] != ) for(int i = ;i < sum[];i++) printf("%c",'W');
printf("\n");
}
return ;
}
UVA1103-Ancient Messages(脑洞+dfs)的更多相关文章
- HDU 3839 Ancient Messages(DFS)
In order to understand early civilizations, archaeologists often study texts written in ancient lang ...
- hdu 3839 Ancient Messages (dfs )
题目大意:给出一幅画,找出里面的象形文字. 要你翻译这幅画,把象形文字按字典序输出. 思路:象形文字有一些特点,分别有0个圈.1个圈.2个圈...5个圈.然后dfs或者bfs,就像油井问题一样,找出在 ...
- Uva1103 Ancient Messages
题意:识别图中的象形文字.但是,图形可以任意的拉伸,但不能拉断. 分析:这种题如果图形没有特征是不可做类型的题,不过观察图形可以发现每个图形中的洞的数量是一定的,我们只需要数出每一个封闭图形的洞数就能 ...
- K - Ancient Messages(dfs求联通块)
K - Ancient Messages Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Subm ...
- UVA1103 古代象形符号 Ancient Messages 题解
题目链接: https://www.luogu.org/problemnew/show/UVA1103 题目分析: 我们可以先进行矩阵的还原 for(int k=1;k<=4;k++) { a[ ...
- UVA - 1103Ancient Messages(dfs)
UVA - 1103Ancient Messages In order to understand early civilizations, archaeologists often study te ...
- UVa 1103 Ancient Messages(二重深搜)
In order to understand early civilizations, archaeologists often study texts written in ancient lang ...
- Ancient Messages UVA - 1103
题目链接:https://vjudge.net/problem/UVA-1103 题目大意:每组数据包含H行W列的字符矩阵(H<=200,W<=50) 每个字符为为16进制 你需要把它转 ...
- UVa 1103 (利用连通块来判断字符) Ancient Messages
本题就是灵活运用DFS来求连通块来求解的. 题意: 给出一幅黑白图像,每行相邻的四个点压缩成一个十六进制的字符.然后还有题中图示的6中古老的字符,按字母表顺序输出这些字符的标号. 分析: 首先图像是被 ...
随机推荐
- MongoDB设计系列
原创文章,如果转载请标明出处.作者. https://www.cnblogs.com/alunchen/p/9762233.html 1 前言 MongoDB作为现今流行的非关系型文档数据库,已经有很 ...
- .net Framework 源代码 · ScrollViewer
本文是分析 .net Framework 源代码的系列,主要告诉大家微软做 ScrollViewer 的思路,分析很简单 看完本文,可以学会如何写一个 ScrollViewer ,如何定义一个 ISc ...
- 【github&&git】2、github入门到上传本地项目
[在原文章的基础上,修改了描述的不够详细的地方,对内容进行了扩充,整合了网上的一些资料] [内容主要来自http://www.cnblogs.com/specter45/p/github.html#g ...
- Hash Table (youtube)
here is a link for youtube about hash table which is super good https://www.youtube.com/watch?v=h2d9 ...
- [转*译]Networking API Improvements in Windows 10
在当今,以云优先,移动优先技术为宗旨的时代下,大多数Apps都至少有一些与web服务或网络上其他设备的集成.这些包括应用程序,它获取天气在线内容,新闻或体育比赛的分数,媒体或下载的播客,甚至对 ...
- 基于python的websocket开发,tomcat日志web页面实时打印监控案例
web socket 接收器:webSocket.py 相关依赖 # pip install bottle gevent gevent-websocket argparse from bottle i ...
- 浅析requests库响应对象的text和content属性
在做爬虫时请求网页的requests库是必不可少的,我们常常会用到 res = resquests.get(url) 方法,在获取网页的html代码时常常使用res的text属性: html = re ...
- jquery弹窗时禁止body滚动条滚动
当弹出一个jq提示窗口的时候,一般窗口右边还会有进度条的情况,禁止进度条方法禁止浏览器滚动条滚动: $('body').css({ "overflow-x":"hidde ...
- layui 数据表格+分页+搜索+checkbox+缓存选中项数据
在做数据表格的时候遇到了很多坑, 今天整理一下方便以后使用. 主要功能是使用数据表格, 做分页,做搜索, 还有checkbox, 支持全选. 当选中一些数据的时候, 数据切换页面数据在切换回来后, ...
- HTML-CSS写抽屉网的置顶区域
1.在pycharm的已有工程中新建一个html文件. 2.在<body></body>标签内部写入要内容: <div class='head-box' > < ...