例题6-13Ancient Messages UVA1103 dfs求连通块+16进制转2进制
来源:互联网 发布:数据库中union all 编辑:程序博客网 时间:2024/05/22 14:25
题目链接:https://vjudge.net/problem/UVA-1103
题意:给出几种象形符号,然后给出16进制的像素描述,让你根据描述字典序输出描述中的象形符号。
思路:几种象形符号可以转化成不同的连通块个数。先转换16进制为2进制,然后给图形外面加一层“边框”0,第一次dfs遍历将外面多余的0全部变成 ‘ - ’ ,然后再进行dfs遇到‘1’就找它是否包围0,包围了几个就对应相应的象形符号。最后字典序输出。
Code:
#include <iostream>#include <cstdio>#include <map>#include <string.h>using namespace std;const int maxn = 1e3+6;char G[maxn][maxn];char s[8] = { 'W' , 'A' , 'K' , 'J' , 'S' , 'D' };//0 1 2 3 4 5const char t[8] = { 'A' , 'D' , 'J' ,'K' ,'S','W' };map<char,int>mp;int H , W , ans ;int dir[8][2] = {{1,0},{0,1},{0,-1},{-1,0},};string dic[16] = {"0000" , "0001" , "0010", "0011","0100" , "0101" , "0110", "0111","1000" , "1001" , "1010", "1011","1100" , "1101" , "1110", "1111",};bool judge( int r, int c){return r >= 0 && r <= H + 1 && c >= 0 && c <= W + 1;}void DFS1( int r, int c){if (!judge(r, c) || G[r][c]!='0') return;G[r][c] = '-';for (int i = 0; i < 4; ++i)DFS1(r + dir[i][0], c + dir[i][1]);}void DFS( int r , int c ){if(!judge(r,c) || G[r][c] != '1' ) return ;G[r][c] = '-';for( int i = 0 ; i < 4 ; i++ ){int rr = r + dir[i][0] , cc = c + dir[i][1];if( G[rr][cc] == '0' ){ans ++ ;DFS1(rr,cc);}else{DFS(rr,cc);}}}int main(){int Case = 0;;while( ~scanf("%d%d",&H,&W) && H || W ){memset(G,'0',sizeof(G));mp.clear();W *= 4;for( int i = 1 ; i <= H ; i++ ){string line , tmp ;cin >> line;int len = line.size();for( int j = 0 ; j < len ; j++ ){tmp += dic[ isalpha(line[j]) ? line[j] - 'a' +10 : line[j] - '0' ];}memcpy( G[i]+1 , tmp.c_str() , W );}DFS1( 0 , 0 );/*for( int i = 0 ; i <= H + 1 ; i++ ){for( int j = 0 ; j <= W +1; j++ ){cout << G[i][j] ;}cout << endl;}*/for( int i = 1 ; i <= H ; i++ ){for( int j = 1 ; j <= W ; j++ ){if( G[i][j] == '1' ){ans = 0 ;DFS( i , j );mp[s[ans]]++;}}}cout << "Case " << ++Case << ": ";for( int i = 0 ; i < 6 ; i++ ){while( mp[t[i]] ){mp[t[i]] --;cout << t[i] ;}}cout << endl;}return 0;}一题写了一上午。。。要加油了。。
阅读全文
0 0
- 例题6-13Ancient Messages UVA1103 dfs求连通块+16进制转2进制
- Uva1103 Ancient Messages【dfs连通块】【例题6-13】
- uva1103 Ancient Messages(dfs)
- UVa1103 - Ancient Messages
- UVA1103 || hdu3839 (Ancient Messages)
- uva1103 Ancient Messages
- Uva572 Oil Deposits 【dfs求连通块】【例题6-12】
- 例题6-13 UVA 1103 Ancient Messages 古老象形符号
- 例题 6-12 油田 UVa 572 用dfs求连通块
- 经典第六章 例题 6-12 UVA 572 Oil Deposits(用DFS求连通块)
- 例题6-12 UVA572 Oil Deposits 基础dfs求连通块
- HDU 3839Ancient Messages(BFS + 连通块,判图形)
- ccpc Ancient Go(dfs求连通块周围'.'的数目)
- 例题6-13 古代象形符号 UVa1103
- 例题6-13 古代象形符号 UVa1103
- Uva1103 World finals 2011——Ancient Messages
- 用DFS求连通块
- 用DFS求连通块
- 增加linux service 系统服务
- Theano介绍及简单应用
- 【bzoj4756】[Usaco2017 Jan]Promotion Counting
- 在Android手机中搭建Dnsmasq后续
- eclipse编译.class并打成war包
- 例题6-13Ancient Messages UVA1103 dfs求连通块+16进制转2进制
- Ubuntu 16.04 安装Jenkins
- iOS Block原理和内存中位置
- 发现Servlet 404报错
- 大型网站架构模式
- NOIP2015 斗地主 (搜索剪枝)
- php include require include_once require_once 的区别
- 提高网站性能的常见方法
- [Mysql必知必会系列]·过滤数据·WHERE子句