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. centos7下git的使用和配置

    1.下载git,使用命令: yum install git 2.配置git: git config --global user.name "Your Name" git confi ...

  2. 创建ros的程序包--3

    创建ros的程序包(原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/) 1.一个catkin程序包由什么组成? 一个程序包要想称为catkin程序 ...

  3. Java精选笔记_会话技术

    会话及其会话技术 会话概述 指的是一个客户端(浏览器)与Web服务器之间连续发生的一系列请求和响应过程. 会话:从浏览器开启到浏览器关闭.会话技术:用来保存在会话期间 浏览器和服务器所产生的数据. 在 ...

  4. (DCloud)用这个来写H5,好像好厉害的样子哦

    HBuilder: http://www.dcloud.io MUI: http://dev.dcloud.net.cn/mui/getting-started/ http://dev.dcloud. ...

  5. details和summary标签

    用于文档说明,有自带收缩.展开功能 <!DOCTYPE HTML> <html> <body> <details> <summary>HTM ...

  6. 【RF库Collections测试】Get From Dictionary

    Name:Get From DictionarySource:Collections <test library>Arguments:[ dictionary | key ]Returns ...

  7. C++11新特性之九——function、bind以及lamda表达式总结

    本文是C++0x系列的第四篇,主要是内容是C++0x中新增的lambda表达式, function对象和bind机制.之所以把这三块放在一起讲,是因为这三块之间有着非常密切的关系,通过对比学习,加深对 ...

  8. C++11新特性之七——final/override控制

    重载和重写的区别参见: C++继承中重载.重写.重定义的区别: 在了解C++11中的final/override关键字之前,我们先回顾一下C++关于重载的概念.简单地说,一个类A中声明的虚函数fun在 ...

  9. (转)关于android设备管理器的一些分析

    转自http://bbs.pediy.com/showthread.php?t=183692 想必很多人都知道轰动一时android木马OBAD,该木马利用android设备管理器的漏洞,当用户激活设 ...

  10. Android 设置wifi共享电脑服务器资源

    其实win7本身就自带无线热点的功能,按下面的方法设置. 开启windows 7的隐藏功能:虚拟WiFi和SoftAP(即虚拟无线AP),就可以让电脑变成无线路由器,实现共享上网,节省网费和路由器购买 ...