热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

TZOJ5113:StarryNight

5113:StarryNight时间限制(普通Java):1000MS3000MS内存限制:65536KByte总提交:3测试通过:3描述Highupinthenightsky,t

5113: Starry Night 技术分享图片


时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte
总提交: 3            测试通过:3

描述


High up in the night sky, the shining stars appear in clusters of various shapes. A cluster is a non-empty group of neighbouring stars, adjacent in horizontal, vertical or diagonal direction. A cluster cannot be a part of a larger cluster.

Clusters may be similar. Two clusters are similar if they have the same shape and number of stars, irrespective of their orientation. In general, the number of possible orientations for a cluster is eight, as Figure 1 exemplifies.

技术分享图片 
Figure 1. Eight similar clusters

The night sky is represented by a sky map, which is a two-dimensional matrix of 0‘s and 1‘s. A cell contains the digit 1 if it has a star, and the digit 0 otherwise.

Given a sky map, mark all the clusters with lower case letters. Similar clusters must be marked with the same letter; non-similar clusters must be marked with different letters.

You mark a cluster with a lower case letter by replacing every 1 in the cluster by that lower case letter.


输入


The first two lines contain, respectively, the width W and the height H of a sky map. The sky map is given in the following H lines, of W characters each.


Constraints

0 <= W (width of the sky map) <= 100
0 <= H (height of the sky map) <= 100
0 <= Number of clusters <= 500
0 <= Number of non-similar clusters <= 26 (a..z)
1 <= Number of stars per cluster <= 160


输出


The output file contains the same map as the input file, except that the clusters are marked as described in Task.

There will generally be more than one way to label the clusters with letters. Your program should choose the labeling such that if the entire output file is read as a string, this string will be minimal in the lexicographical ordering.


样例输入

23
15
10001000000000010000000
01111100011111000101101
01000000010001000111111
00000000010101000101111
00000111010001000000000
00001001011111000000000
10000001000000000000000
00101000000111110010000
00001000000100010011111
00000001110101010100010
00000100110100010000000
00010001110111110000000
00100001110000000100000
00001000100001000100101
00000001110001000111000

样例输出

 

a000a0000000000b0000000
0aaaaa000ccccc000d0dd0d
0a0000000c000c000dddddd
000000000c0b0c000d0dddd
00000eee0c000c000000000
0000e00e0ccccc000000000
b000000e000000000000000
00b0f000000ccccc00a0000
0000f000000c000c00aaaaa
0000000ddd0c0b0c0a000a0
00000b00dd0c000c0000000
000g000ddd0ccccc0000000
00g0000ddd0000000e00000
0000b000d0000f000e00e0b
0000000ddd000f000eee000

提示


In this case, the sky map has width 23 and height 15. Just to make it clearer, notice that this input file corresponds to the following picture of the sky.

技术分享图片
Figure 2. Picture of the sky

This is one possible result for the sample input above. Notice that this output file corresponds to the following picture.

技术分享图片
Figure 3. Picture with the clusters marked

题意:有多个连通块,如果两个连通块 通过旋转90度,对称翻转后形状相同,则两个连通块归属为相同,按照行列的顺序用字符编号


技术分享图片技术分享图片

#include
using namespace std;
int w,h;
int up,l;
int bottom,r;
struct point{
int x,y;
};
char s[105][105],f[505];
bool vis[105][105];
vector
v[505],vk[505];
int dtx[]={-1,-1,-1,0,0,1,1,1};
int dty[]={0,1,-1,1,-1,1,0,-1};
bool check(int x,int y){
if(x<0||x>=h||y<0||y>=w)return false;
return true;
}
void bfs(int x,int y,int id){
queue
q;
l
=w;
up
=h;
q.push((point){x,y});
vis[x][y]
=1;
while(!q.empty()){
point u
=q.front();
v[id].push_back(u);
q.pop();
l
=min(l,u.y);
up
=min(up,u.x);
for(int i=0;i<8;i++){
point v
=u;
v.x
+=dtx[i];
v.y
+=dty[i];
if(!check(x,y))continue;
if(vis[v.x][v.y]==0&&s[v.x][v.y]==1){
q.push(v);
vis[v.x][v.y]
=1;
}
}
}
}
void get_vk(int id){
for(int i=0;i){
vk[id].push_back((point){v[id][i].x-up,v[id][i].y-l});
}
}
bool cmp(point a,point b){
return a.xb.y;
}
bool check_equal(vector a,vector b){
sort(a.begin(),a.end(),cmp);
sort(b.begin(),b.end(),cmp);
for(int i=0;i){
if(a[i].x!=b[i].x||a[i].y!=b[i].y)
return false;
}
return true;
}
void set_data(vector &a){//获得图形的边界
bottom=-1,r=-1;
for(int i=0;i){
bottom=max(a[i].x,bottom);
r
=max(a[i].y,r);
}
}
void Reverse(vector &a){ //左右翻转
set_data(a);
for(int i=0;i){
a[i].y=(r-a[i].y);
}
}
void spin(vector &a){//逆时针旋转
set_data(a);
for(int i=0;i){
int z=a[i].y;
a[i].y
=a[i].x;
a[i].x
=(r-z);
}
}
/*/char shape[105][105];
void out_shape(vector &a){
set_data(a);
for(int i=0;i<=bottom;i++)
for(int j=0;j<=r;j++)
shape[i][j]=‘0‘;
for(int i=0;i shape[a[i].x][a[i].y]=‘1‘;
}
printf("%d\n",a.size());
for(int i=0;i<=bottom;i++)
{for(int j=0;j<=r;j++)
printf("%c",shape[i][j]);
printf("\n");
}
printf("\n");
}/
*/
bool judge(int x,int y){//判断两个图形是否相似
vector vr,vt;
vr
=vk[x];vt=vk[y];
for(int i=1;i<=4;i++){
spin(vr);
//out_shape(vr);
if(check_equal(vr,vt))return true;
Reverse(vr);
//out_shape(vr);
if(check_equal(vr,vt))return true;
Reverse(vr);
//out_shape(vr);
}
return false;
}
int main(){
scanf(
"%d%d",&w,&h);
for(int i=0;i){
scanf("%s",s[i]);
}
int k=0;
for(int i=0;i){
for(int j=0;j){
if(s[i][j]==1&&vis[i][j]==0){
bfs(i,j,
++k);//bfs 查找连通块
get_vk(k); //图像左移上移
}
}
}
char ch=a;
f[
1]=a; //第一个连通块为a
for(int i=2;i<=k;i++){
int flag=0;
for(int j=1;j//和已判断连通块进行比较
if(vk[i].size()!=vk[j].size())continue;//点数不同直接跳过
if(judge(i,j)){ //图像相似
flag=1;
f[i]
=f[j];
break;
}
}
if(!flag)f[i]=++ch;//未有相似图像,新增图像字符
}
for(int i=1;i<=k;i++){
for(int j=0;j){
s[v[i][j].x][v[i][j].y]=f[i];
}
}
//字符设置
for(int i=0;i){
printf("%s\n",s[i]);
}
}


View Code

 





推荐阅读
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 本文内容为asp.net微信公众平台开发的目录汇总,包括数据库设计、多层架构框架搭建和入口实现、微信消息封装及反射赋值、关注事件、用户记录、回复文本消息、图文消息、服务搭建(接入)、自定义菜单等。同时提供了示例代码和相关的后台管理功能。内容涵盖了多个方面,适合综合运用。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文讲述了作者通过点火测试男友的性格和承受能力,以考验婚姻问题。作者故意不安慰男友并再次点火,观察他的反应。这个行为是善意的玩人,旨在了解男友的性格和避免婚姻问题。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • 本文介绍了如何使用PHP向系统日历中添加事件的方法,通过使用PHP技术可以实现自动添加事件的功能,从而实现全局通知系统和迅速记录工具的自动化。同时还提到了系统exchange自带的日历具有同步感的特点,以及使用web技术实现自动添加事件的优势。 ... [详细]
author-avatar
k3as0n_701
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有