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中古老的字符,按字母表顺序输出这些字符的标号. 分析: 首先图像是被 ...
随机推荐
- EF 中多个数据库迁移
Entity Framework 允许用户基于实体来创建和更新数据库,当项目中只有一个DBContext 时使用默认的参数就可以实现数据库的一些操作.当有多个DbContext 时需要制定参数才能完成 ...
- soapui 自动化教程
本教程主要讲述对接口的自动化测试,略过压力测试.安全测试. 最终目标是通过groovy脚本执行一个文件,发送多个任务请求.验证返回值是否符合期望. 教程从soapui入门到groovy实现回传参数.生 ...
- 如何将一个HTML页面嵌套在另一个页面中
一 在原页面嵌入其他页面 1.使用iframe框架 客户端页面嵌套可以使用iframe的方法,弊端是必须事先想好被嵌套的页面在首页中要占多大的位置. 如果被嵌套页面太大,超过事先定义的宽度或高度,则首 ...
- WebForm 【发送邮件】
C#实现简单的SmtpClient发送邮件 分析 需要什么 发送邮件 -- 发送内容 -- 接收邮件 流程(各功能都适用) 创建对象 -- ...
- jquery 获得下拉框的值《转》
获取Select : 获取select 选中的 text : $("#ddlRegType").find("option:selected").text(); ...
- 阿里CentOS 7 卸载mysql5.6
查看当前安装mysql情况 rpm -qa|grep -i mysql 执行 yum remove mysql rpm -e mysql-community-release-el7-5.noarch ...
- 140 - The 12th Zhejiang Provincial Collegiate Programming Contest(浙江省赛2015)
Ace of Aces Time Limit: 2 Seconds Memory Limit: 65536 KB There is a mysterious organization c ...
- Python 操作文件
open() 函数 模式 说明 r 只读模式 w 只写模式,文件不存在自动创建:存在则清空再写 a 只追加写,在文件最后追加写 r+ 打开一个文件用于读写.文件指针将会放在文件的开头. w+ 打开一个 ...
- 关于<checkbox>checked默认问题
这个问题困扰我有一段时间了,今天终于解决了. 接手现在的项目半个月了,我看之前的人写的<checkbox checked="false" />一直没有生效,但是需求上没 ...
- SFTP 文件配置
sftp_config_file SFTP配置文件(Sublime Text 3 .VS Code) VS Code 的版本 { "host": "120.01.01.1 ...