热门标签 | 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

 





推荐阅读
  • QUIC协议:快速UDP互联网连接
    QUIC(Quick UDP Internet Connections)是谷歌开发的一种旨在提高网络性能和安全性的传输层协议。它基于UDP,并结合了TLS级别的安全性,提供了更高效、更可靠的互联网通信方式。 ... [详细]
  • 深入理解OAuth认证机制
    本文介绍了OAuth认证协议的核心概念及其工作原理。OAuth是一种开放标准,旨在为第三方应用提供安全的用户资源访问授权,同时确保用户的账户信息(如用户名和密码)不会暴露给第三方。 ... [详细]
  • CSS 布局:液态三栏混合宽度布局
    本文介绍了如何使用 CSS 实现液态的三栏布局,其中各栏具有不同的宽度设置。通过调整容器和内容区域的属性,可以实现灵活且响应式的网页设计。 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 本文介绍如何通过Windows批处理脚本定期检查并重启Java应用程序,确保其持续稳定运行。脚本每30分钟检查一次,并在需要时重启Java程序。同时,它会将任务结果发送到Redis。 ... [详细]
  • MySQL中枚举类型的所有可能值获取方法
    本文介绍了一种在MySQL数据库中查询枚举(ENUM)类型字段所有可能取值的方法,帮助开发者更好地理解和利用这一数据类型。 ... [详细]
  • 本文探讨了如何通过最小生成树(MST)来计算严格次小生成树。在处理过程中,需特别注意所有边权重相等的情况,以避免错误。我们首先构建最小生成树,然后枚举每条非树边,检查其是否能形成更优的次小生成树。 ... [详细]
  • 国内BI工具迎战国际巨头Tableau,稳步崛起
    尽管商业智能(BI)工具在中国的普及程度尚不及国际市场,但近年来,随着本土企业的持续创新和市场推广,国内主流BI工具正逐渐崭露头角。面对国际品牌如Tableau的强大竞争,国内BI工具通过不断优化产品和技术,赢得了越来越多用户的认可。 ... [详细]
  • 深入理解 Oracle 存储函数:计算员工年收入
    本文介绍如何使用 Oracle 存储函数查询特定员工的年收入。我们将详细解释存储函数的创建过程,并提供完整的代码示例。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 如何在WPS Office for Mac中调整Word文档的文字排列方向
    本文将详细介绍如何使用最新版WPS Office for Mac调整Word文档中的文字排列方向。通过这些步骤,用户可以轻松更改文本的水平或垂直排列方式,以满足不同的排版需求。 ... [详细]
  • 理解存储器的层次结构有助于程序员优化程序性能,通过合理安排数据在不同层级的存储位置,提升CPU的数据访问速度。本文详细探讨了静态随机访问存储器(SRAM)和动态随机访问存储器(DRAM)的工作原理及其应用场景,并介绍了存储器模块中的数据存取过程及局部性原理。 ... [详细]
  • 几何画板展示电场线与等势面的交互关系
    几何画板是一款功能强大的物理教学软件,具备丰富的绘图和度量工具。它不仅能够模拟物理实验过程,还能通过定量分析揭示物理现象背后的规律,尤其适用于难以在实际实验中展示的内容。本文将介绍如何使用几何画板演示电场线与等势面之间的关系。 ... [详细]
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社区 版权所有