码迷,mamicode.com
首页 > 其他好文 > 详细

【USACO 5.1.2】Starry Night

时间:2015-11-15 17:38:47      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

Starry Night
IOI 98

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.

PROGRAM NAME: starry

INPUT FORMAT

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.

SAMPLE INPUT (file starry.in)

23
15
10001000000000010000000
01111100011111000101101
01000000010001000111111
00000000010101000101111
00000111010001000000000
00001001011111000000000
10000001000000000000000
00101000000111110010000
00001000000100010011111
00000001110101010100010
00000100110100010000000
00010001110111110000000
00100001110000000100000
00001000100001000100101
00000001110001000111000
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

 

OUTPUT FORMAT

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.

SAMPLE OUTPUT (file starry.out)

a000a0000000000b0000000
0aaaaa000ccccc000d0dd0d
0a0000000c000c000dddddd
000000000c0b0c000d0dddd
00000eee0c000c000000000
0000e00e0ccccc000000000
b000000e000000000000000
00b0f000000ccccc00a0000
0000f000000c000c00aaaaa
0000000ddd0c0b0c0a000a0
00000b00dd0c000c0000000
000g000ddd0ccccc0000000
00g0000ddd0000000e00000
0000b000d0000f000e00e0b
0000000ddd000f000eee000
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

 

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

 

庞大的模拟题。

八种情况让你写得手软,所以在此要好好利用学习过的技能(Ctrl + c).

写这种题之前,要仔细想清细节,不要盲目敲代码(这会使你打一段删一段,从而使你速度大减,并且有很大的机会出错,也就是要减少更改的次数)优化判断的速度。

最后就是敲代码啦。

找一个星座出来的方法就是用floodfill。暴力判重即可。

代码200+行。

/*
TASK:starry
LANG:C++
*/
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAXM = 105;
const int d[2][8] = {{0, -1, -1, -1, 0, 1, 1, 1},
                     {-1, -1, 0, 1, 1, 1, 0, -1}};

struct Cluster
{
    char s[MAXM][MAXM];
    int numst, high, wide;
    
    bool operator == (const Cluster &b)
    {
        if (numst != b.numst) return false;
        
        if (high == b.high && wide == b.wide)
        {
            bool flag;
            
            for (int i = 0; i < high; ++i)
            {
                flag = true;
                for (int j = 0; j < wide; ++j)
                    if (s[i][j] != b.s[i][j])
                    {
                        flag = false;
                        break;
                    }
                if (!flag) break;
            }
            if (flag) return true;
            
            for (int i = 0; i < high; ++i)
            {
                flag = true;
                for (int j = 0; j < wide; ++j)
                    if (s[i][j] != b.s[high - i - 1][j])
                    {
                        flag = false;
                        break;
                    }
                if (!flag) break;
            }
            if (flag) return true;
            
            for (int i = 0; i < high; ++i)
            {
                flag = true;
                for (int j = 0; j < wide; ++j)
                    if (s[i][j] != b.s[i][wide - j - 1])
                    {
                        flag = false;
                        break;
                    }
                if (!flag) break;
            }
            if (flag) return true;
            
            for (int i = 0; i < high; ++i)
            {
                flag = true;
                for (int j = 0; j < wide; ++j)
                    if (s[i][j] != b.s[high - i - 1][wide - j - 1])
                    {
                        flag = false;
                        break;
                    }
                if (!flag) break;
            }
            if (flag) return true;
        }
        
        if (high == b.wide && wide == b.high)
        {
            bool flag;
            
            for (int i = 0; i < high; ++i)
            {
                flag = true;
                for (int j = 0; j < wide; ++j)
                    if (s[i][j] != b.s[wide - j - 1][i])
                    {
                        flag = false;
                        break;
                    }
                if (!flag) break;
            }
            if (flag) return true;
            
            for (int i = 0; i < high; ++i)
            {
                flag = true;
                for (int j = 0; j < wide; ++j)
                    if (s[i][j] != b.s[j][i])
                    {
                        flag = false;
                        break;
                    }
                if (!flag) break;
            }
            if (flag) return true;
            
            for (int i = 0; i < high; ++i)
            {
                flag = true;
                for (int j = 0; j < wide; ++j)
                    if (s[i][j] != b.s[j][high - i - 1])
                    {
                        flag = false;
                        break;
                    }
                if (!flag) break;
            }
            if (flag) return true;
            
            for (int i = 0; i < high; ++i)
            {
                flag = true;
                for (int j = 0; j < wide; ++j)
                    if (s[i][j] != b.s[wide - j - 1][high - i - 1])
                    {
                        flag = false;
                        break;
                    }
                if (!flag) break;
            }
            if (flag) return true;
            
        }
        return false;
    }

}clusters[30];

char g[MAXM][MAXM];
int h, w, mark[MAXM * MAXM], num[MAXM][MAXM], cnt1, cnt2, left, right, up, down;
bool vis[MAXM][MAXM];

Cluster create()
{
    Cluster tmp;
    memset(tmp.s, 0, sizeof(tmp.s));
    tmp.numst = 0;
    tmp.high = down - up + 1;
    tmp.wide = right - left + 1;
    for (int i = 0; i < tmp.high; ++i)
        for (int j = 0; j < tmp.wide; ++j)
        {
            if (num[i + up][j + left] == cnt1) tmp.s[i][j] = 1, tmp.numst++;
            else tmp.s[i][j] = 0;
        }
    return tmp;
}

bool test(int x, int y)
{
    return 0 <= x && x < h && 0 <= y && y < w;
}

void floodfill(int x0, int y0)
{
    vis[x0][y0] = false;
    num[x0][y0] = cnt1;
    up = min(up, x0);
    down = max(down, x0);
    left = min(left, y0);
    right = max(right, y0);
    for (int i = 0; i < 8; ++i)
    {
        int x = x0 + d[0][i], y = y0 + d[1][i];
        if (test(x, y) && g[x][y] == 1 && vis[x][y]) floodfill(x, y);
    }
}

int main()
{
    freopen("starry.in", "r", stdin);
    freopen("starry.out", "w", stdout);
    scanf("%d%d", &w, &h);
    for (int i = 0; i < h; ++i) scanf("%s", g[i]);
    memset(vis, true, sizeof(vis));
    memset(num, 0, sizeof(num));
    cnt1 = cnt2 = 0;
    for (int i = 0; i < h; ++i)
        for (int j = 0; j < w; ++j)
            if (vis[i][j] && g[i][j] == 1)
            {
                ++cnt1;
                up = down = i;
                left = right = j;
                floodfill(i, j);
                Cluster cl = create();
                bool flag = true;
                for (int k = 0; k < cnt2; ++k)
                    if (cl == clusters[k])
                    {
                        mark[cnt1] = k;
                        flag = false;
                        break;
                    }
                if (flag) clusters[mark[cnt1] = cnt2++] = cl;
            }
    for (int i = 0; i < h; ++i)
    {
        for (int j = 0; j < w; ++j)
            if (g[i][j] != 0) printf("%c", mark[num[i][j]] + a);
            else printf("0");
        printf("\n");
    }
    return 0;
}

 

【USACO 5.1.2】Starry Night

标签:

原文地址:http://www.cnblogs.com/albert7xie/p/4966837.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!