http://poj.org/problem?id=1175

题目解析:

这个题因为数据的原因可以很水的过,但我因为把1e-8写成了1e-9WA了N遍,一直WA,题目意思很简单就是相似图形(就是求旋转的图形,我不会,但在discuss中看到

可以通过求图形的长度来求)另外常识性问题double数据类型不能直接比较相等,需要借助一个小数比较,那个比较的小数题目一般会提示,例如这题就是1e-8,另外就是

这题的数据是在是忒坑了!

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <queue>
#include <math.h>
#define MIN 1e-8
using namespace std;
int map[][];
char ff[][];
int n,m,tt,v[][];
struct node
{
int x,y;
};
int sum[];
double dis[];
struct node1
{
int x,y;
} s[];
int jx[]= {,,,,,-,-,-};
int jy[]= {-,,,-,,-,,};
void bfs(int xx,int yy)
{
memset(v,,sizeof(v));
queue<node>q;
struct node t,f;
int l=;
s[l].x=xx;
s[l++].y=yy;
t.x=xx;
t.y=yy;
v[t.x][t.y]=;
q.push(t);
while(!q.empty())
{
t=q.front();
q.pop();
for(int i=; i<; i++)
{
f.x=t.x+jx[i];
f.y=t.y+jy[i];
if(map[f.x][f.y]&&v[f.x][f.y]==&&f.x>=&&f.x<n&&f.y>=&&f.y<m)
{
s[l].x=f.x;
s[l++].y=f.y;
v[f.x][f.y]=;
q.push(f);
}
}
}
double count=;
for(int i=; i<l; i++)
{
for(int j=; j<l; j++)
{
count=count+sqrt((s[i].x-s[j].x)*(s[i].x-s[j].x)*1.0+(s[i].y-s[j].y)*(s[i].y-s[j].y)*1.0);
}
}
int flag=;
for(int i=; i<tt; i++)
{
if(sum[i]==l&&fabs(dis[i]-count)<MIN)
{
ff[xx][yy]=i+'a';
flag=;
}
}
if(flag==)
{
ff[xx][yy]=tt+'a';
dis[tt]=count;
sum[tt]=l;
tt++;
}
}
int main()
{
char str;
while(scanf("%d%d",&m,&n)!=EOF)
{
tt=;
for(int i=; i<n; i++)
{
getchar();
for(int j=; j<m; j++)
{
scanf("%c",&str);//不知道为什么不能直接输入map[i][j],基础知识太渣了
ff[i][j]=str;
map[i][j]=str-'';
}
}
for(int i=; i<n; i++)
{
for(int j=; j<m; j++)
{
if(map[i][j])
bfs(i,j);
}
}
for(int i=; i<n; i++)
{
for(int j=; j<m; j++)
{
printf("%c",ff[i][j]);
}
printf("\n");
}
}
return ;
}

Description

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. 

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 order of allocation of letters depends on the first appearance of different clusters, from top to bottom and from left to right. 

Input

Your program is to read from standard input. 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.

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

Output

Your program is to write to standard output. The output contains the same map as the input, except that the clusters are marked as described above.

Sample Input

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

Sample Output

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

Hint

Just to make it clearer, notice that this sample input corresponds to the following picture of the sky. 

Notice that this sample output corresponds to the following picture. 

Source

 

POJ1175:Starry Night(bfs)的更多相关文章

  1. BFS广搜题目(转载)

    BFS广搜题目有时间一个个做下来 2009-12-29 15:09 1574人阅读 评论(1) 收藏 举报 图形graphc优化存储游戏 有时间要去做做这些题目,所以从他人空间copy过来了,谢谢那位 ...

  2. [USACO5.1]夜空繁星Starry Night

    题目背景 高高的星空,簇簇闪耀的群星形态万千.一个星座(cluster)是一群连通的星组成的非空连通星系,这里的连通是指水平,垂直或者对角相邻的两个星星.一个星座不能是另一个更大星座的一部分, 星座可 ...

  3. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  4. 【BZOJ-1656】The Grove 树木 BFS + 射线法

    1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 186  Solved: 118[Su ...

  5. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  6. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  7. Sicily 1215: 脱离地牢(BFS)

    这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...

  8. Sicily 1048: Inverso(BFS)

    题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...

  9. Sicily 1444: Prime Path(BFS)

    题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...

随机推荐

  1. 安装JDK出现问题 Error opening registry key'software\Javasoft\Java Runtime Environment'

    第一次安装JDK测试是否安装成功,打开cmd输入java -version 回车的时候出现如下错误: Error opening registry key'software\Javasoft\Java ...

  2. 原生js获取元素样式

    摘要: 我们在开发过程中经常会遇到通过js获取或者改变DOM元素的样式,方法有很多,比如:通过更改DOM元素的class.现在我们讨论原生js来获取DOM元素的CSS样式,注意是获取不是设置 在开始之 ...

  3. QlikView报表显示连续若干个月内活跃用户的数量

    之前有朋友提到了这样一个需求,要计算三年中每年都有销售记录的客户量,仅仅有近期两年有销售纪录的客户量(假如某个用户2012年和2014年都有记录,在2013年没有则不计算在内).以及近期一年的新增客户 ...

  4. Android Timer的应用示例

    package com.hyzhou.timerdemo1; import java.util.Timer; import java.util.TimerTask; import android.os ...

  5. 关于直播学习笔记-002-Red5 & Sewise Player & Wirecast

    一.工具软件 [1]. 视频采集端 Red5 Demo:http://192.168.31.107:5080/demos/simpleBroadcaster.html Telestream:Wirec ...

  6. Python 文件类型

    Python的文件类型分为以下几种: 1. 源代码文件,也就是以 .py 为扩展名的文件,由 python 程序解释,不需要编译 2. 字节代码文件,python 源代码文件经过编译后生成的扩展名为 ...

  7. Binary XML file line #17<vector> tag requires viewportWidth > 0

    Android高版本对比低版本 在我的项目中更改成 //buildToolsVersion '21.1.2'buildToolsVersion '24.0.1' // 24.0.1 必须用这个否则报B ...

  8. orcale 闪回操作 已提交的修改 给还原

    delete from conf_ty_parser_title; INSERT INTO conf_ty_parser_title ( SELECT * FROM conf_ty_parser_ti ...

  9. 关于Windows下的批处理如何模拟Sleep

    好好的批处理,居然没有正式的Sleep可供调用.有时候,确实感到很无趣. 1. 方法1: ping 1.1.1.1来模拟 好不容易从stackoverflow上找到一个答案(称之为答案,是因为它被人标 ...

  10. LeetCode——Number of 1 Bits

    //求一个整数的二进制串中1的个数 public int hammingWeight(int n) { String b_str = Integer.toBinaryString(n); int b_ ...