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中古老的字符,按字母表顺序输出这些字符的标号. 分析: 首先图像是被 ...
随机推荐
- [IOI2014] 假期
Description 有\(N(N\leq 10^5)\)个排列在一条线上的城市,每个城市有\(val_i\)个景点.每天你可以选择在当前城市\(i\)游览景点,或者前往城市\(i-1\)或城市\( ...
- nfs 提高传输速度
通常挂载 nfs 的命令为: mount -t nfs -o nolock 192.168.0.124:/home/admin/rootfs /mnt 之前我一直都是用这个命令来挂载,那个传输速度啊, ...
- Flask 中的 CBV 与上传文件
from flask import Flask, views, render_template, request app = Flask(__name__) app.config['DEBUG'] = ...
- 将表格导出为excel
<table id="tableExcel" border="1"> <tr> <th>零</th> <t ...
- element UI实现表格中添加开关控制按钮
我使用的是element ui V1.4.3 如下图是我要实现的效果: <template> <div> <el-button type="text" ...
- 03--css形状--css揭秘
形状 一 自适应的椭圆 1.难题 1> 圆 你可能注意到过, 给任何正方形元素设置一个足够大的border-radius, 就可以把它变成一个圆形.所用到的CSS 代码如下所示: #bd { w ...
- 使用光盘作为yum软件安装源安装X Window
使用光盘作为yum软件安装源安装X Window 挂载光驱 [root@centos6 ~]# mkdir /media/cdrom [root@centos6 ~]# mount /dev/ ...
- 【转】解决CentOS 64位系统vsftpd 530 login incorrect的问题
转自:http://www.centos.bz/2011/12/centos-64-install-vsftpd-530-login-incorredct/ 今天在centos 6 64位测试安装vs ...
- Loadrunner 脚本开发-从文件读取数据并参数化
脚本开发-从文件读取数据并参数化 by:授客 QQ:1033553122 直接上代码: char* testfn() { int count, total = 0; //char buffer[1 ...
- IP地址及子网划分
1.IP地址 2.子网掩码 网络号全转为1,主机号全转为0,之后再转化为10进制表示. 3.无分类编址