POJ1175:Starry Night(bfs)

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[102][102];
char ff[102][102];
int n,m,tt,v[102][102];
struct node
{
    int x,y;
};
int sum[501];
double dis[501];
struct node1
{
    int x,y;
} s[501];
int jx[]= {1,1,1,0,0,-1,-1,-1};
int jy[]= {-1,0,1,-1,1,-1,0,1};
void bfs(int xx,int yy)
{
    memset(v,0,sizeof(v));
    queue<node>q;
    struct node t,f;
    int l=0;
    s[l].x=xx;
    s[l++].y=yy;
    t.x=xx;
    t.y=yy;
    v[t.x][t.y]=1;
    q.push(t);
    while(!q.empty())
    {
        t=q.front();
        q.pop();
        for(int i=0; i<8; i++)
        {
            f.x=t.x+jx[i];
            f.y=t.y+jy[i];
            if(map[f.x][f.y]&&v[f.x][f.y]==0&&f.x>=0&&f.x<n&&f.y>=0&&f.y<m)
            {
                s[l].x=f.x;
                s[l++].y=f.y;
                v[f.x][f.y]=1;
                q.push(f);
            }
        }
    }
    double count=0;
    for(int i=0; i<l; i++)
    {
        for(int j=0; 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=0;
    for(int i=0; i<tt; i++)
    {
        if(sum[i]==l&&fabs(dis[i]-count)<MIN)
        {
            ff[xx][yy]=i+‘a‘;
            flag=1;
        }
    }
    if(flag==0)
    {
        ff[xx][yy]=tt+‘a‘;
        dis[tt]=count;
        sum[tt]=l;
        tt++;
    }
}
int main()
{
    char str;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        tt=0;
        for(int i=0; i<n; i++)
        {
            getchar();
            for(int j=0; j<m; j++)
            {
                scanf("%c",&str);//不知道为什么不能直接输入map[i][j],基础知识太渣了
                ff[i][j]=str;
                map[i][j]=str-‘0‘;
            }
        }
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(map[i][j])
                    bfs(i,j);
            }
        }
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                printf("%c",ff[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}

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

IOI 1998

时间: 2024-10-25 08:06:33

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

[hdu 2102]bfs+注意INF

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 感觉这个题非常水,结果一直WA,最后发现居然是0x3f3f3f3f不够大导致的--把INF改成INF+INF就过了. #include<bits/stdc++.h> using namespace std; bool vis[2][15][15]; char s[2][15][15]; const int INF=0x3f3f3f3f; const int fx[]={0,0,1,-1};

BFS+康托展开(洛谷1379 八数码难题)

在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给出一种初始布局(初始状态)和目标布局(为了使题目简单,设目标状态为123804765),找到一种最少步骤的移动方法,实现从初始布局到目标布局的转变. 输入格式: 输入初试状态,一行九个数字,空格用0表示 输出格式: 只有一行,该行只有一个数字,表示从初始状态到目标状态需要的最少移动次数(测试数据中无特殊无法到达目标状态数据) 输入样例#1: 2831

Sicily 1444: Prime Path(BFS)

题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 bool isPrime(int n){//素数判断 5 if(n == 2 || n == 3) return true; 6 else{ 7 int k = sqrt(n) + 1; 8 for(int i = 2; i < k; i

HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)

Coconuts Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 524    Accepted Submission(s): 151 Problem Description TanBig, a friend of Mr. Frog, likes eating very much, so he always has dreams abou

POJ3967Ideal Path[反向bfs 层次图]

Ideal Path Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 1754   Accepted: 240 Description New labyrinth attraction is open in New Lostland amusement park. The labyrinth consists of n rooms connected by m passages. Each passage is colo

胜利大逃亡(续)(状态压缩bfs)

胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7357    Accepted Submission(s): 2552 Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)……这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带

邻接表实现Dijkstra算法以及DFS与BFS算法

//============================================================================ // Name : ListDijkstra.cpp // Author : fffff // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //==========================

图的两种遍历-DFS&amp;BFS

DFS和BFS在图中的应用: 图连通性判定:路径的存在性:图中是否存在环:求图的最小生成树:求图的关键路径:求图的拓扑排序. DFS:简单的说,先一直往深处走,直到不能再深了,再从另一条路开始往深处走,直到所有路都走完: struct node { int next; //E[i].next指向图中与i同父的下一个结点 int to; //E[i].to指向图中i的子结点 }E[110]; int N; int fa[110]; //记录各点的父结点 bool vis[110]; //记录这个点

POJ 2251 Dungeon Master (bfs)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> using namespace std; char mat[50][50][50]; int vis[50][50][50]; int op[6][3]={0,-1,0, 0,1,0, 1,0,0, -1,0,0 ,0,0,1, 0,0,-1 }; int ok; in