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. 记录下Lambda常用的表现形式

    纯粹记录下Lambda的表现形式: (x, y) => x * y;//多参数,隐式类型=>表达式 x => x * ;//单参数,隐式类型=>表达式 x => { ; ...

  2. dedecms的arclist循环中判断第一个li添加css,否则不加

    dedecms的arclist循环中,判断如果是第一个li,则添加固定的css,否则不加   写法如下: {dede:arclist row=4 flag='p'} <li [field:glo ...

  3. C语言函数參数传递原理

    C语言中參数的传递方式一般存在两种方式:一种是通过栈的形式传递.还有一种是通过寄存器的方式传递的. 这次.我们仅仅是具体描写叙述一下第一种參数传递方式,第二种方式在这里不做具体介绍. 首先,我们看一下 ...

  4. mybatis由浅入深day02_2一对一查询_2.3方法二:resultMap_resultType和resultMap实现一对一查询小结

    2.3 方法二:resultMap 使用resultMap,定义专门的resultMap用于映射一对一查询结果. 2.3.1 sql语句 同resultType实现的sql SELECT orders ...

  5. linux--解决oracle sqlplus 中上下左右backspace不能用

    1.  解决不能backspace 方法1: stty erase ^h 在oracle用户下:在用户环境配置文件.bash_profile中加入如下语句 stty erase ^h 方法2:在sec ...

  6. ch4-持久存储

    1.处理数据和打印 man = [] other = [] try: data = open('sketch.txt') for each_line in data: try: (role, line ...

  7. ORA-20000的解决过程

    今天在用impdp导数据时出现卡住的现象,开始我以为是数据量过大导致的,过了两个小时还是卡住,看警告日志得到下面的错误信息: GATHER_STATS_JOB encountered errors.  ...

  8. linux系统usb挂载

    本次例程的环境是在FC6下,通过终端操作的. 注意要挂载U盘需要有管理员的权限. 切换成管理员,输入: su root 然后输入管理员密码,进行密码认证: 成功后,先在 /mnt 下建立一个名叫USB ...

  9. 《C#高级编程》学习笔记------抗变和协变

    1.协变和抗变 在.NET 4之前,泛型接口是不变的..NET 4通过协变和抗变为泛型接口和泛型委托添加了一个重要的扩展.协变和抗变指对参数和返回值的类型进行转换.例如,可以给一个需要Shape参数的 ...

  10. Vue基础---->vue-router的使用(一)

    用 Vue.js + vue-router 创建单页应用,是非常简单的.使用 Vue.js 时,我们就已经把组件组合成一个应用了,当你要把 vue-router 加进来,只需要配置组件和路由映射,然后 ...