Sicily 7148. Magic Traps

来源:互联网 发布:卖家淘宝店 编辑:程序博客网 时间:2024/05/16 03:09

7148. Magic Traps

Constraints

Time Limit: 1 secs, Memory Limit: 256 MB

Description

Rikka is fighting against Priestess in a park now, but it seems that she almost fails. The only thing she can do is to launch the magic traps which were deployed before. Each trap belongs to one of the three categories: rectangle, circle and triangle. Different categories of traps need different magic spells to launch. So Rikka must quickly understand how many traps of each category are there. Unfortunately, it's hard to understand what this trap category is for Rikka, so she needs your help.

To simplify the problem, we assume that the park is a large rectangle and divided into N*M blocks. A magic trap is combined by several connected blocks. Two blocks connect each other if and only if they share either an edge or a point. A block is said to be magic if it is part of a trap.

Because of her great power, Priestess can break some magic blocks so that they are not magic any more. Priestess can break a magic block only if it shares edges with at least two other magic blocks. For example, in the following figure, assume that block 1, 2, 3, 4, and 5 are all magic, while block 6 is not. Then block 5 cannot be broken because it shares an edge with only one magic block (block 3). Priestess can break one of block 1, 2, 3, or 4; however, she cannot break both block 1 and 2, because after breaking block 1, block 2 shares an edge with only one magic block (block 4).

Moreover, Priestess cannot break a block if breaking it will divide the trap into two disconnected parts. For example, in the following figure, assume that block 1, 2, and 3 are all magic, block 2 cannot be broken because breaking it will separate block 1 and 3.

Input

There are several test cases. You should process to the end of input.

       For each case, the first line is N and M (20<=N, M<=500), indicating the size of the park. Then N lines follow. Each line contains M characters and each character is either '.' or '#'. If the j-th character is '#', it means that the j-th block of the i-th row is a part of a trap. Otherwise, it is an empty block or it is broken.

       It's guaranteed that there are at least 15 '#'s for each trap. And the number of broken blocks of each trap will be less than 10% of the whole trap. All rectangles’ sides are parallel to the park's side. All traps are perfectly included in the park. No two traps share a block, an edge or even a point. You should suppose that actually it's easy to understand what the traps categories are by people.

 

Output

You should output answers in the following format:

 

Rectangle: rectangle_count

Circle: circle_count

Triangle: triangle_count

 

where rectangle_count, circle_count and triangle_count indicate how many rectangles, circles and triangles are in the park, respectively.

Output a blank line after each case.

 

Sample Input

20 20#################################################################################################################################################################.###############################################.#######################.######################################################################################################################################################################20 20########.....####...########...########.########...########.########..##################..##################..##################..##################...########............########..............####....#....................##...................###..................####.................#####................######...............#######..............########.............#########............##########

Sample Output

Rectangle: 1Circle: 0Triangle: 0Rectangle: 1Circle: 1Triangle: 1

Problem Source

“星海通杯”第四届中山大学ICPC新手赛 by 阮永留

// Problem#: 7148// Submission#: 3406186// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <stdio.h>#include <iostream>#include <vector>#include <string>#include <stack>#include <iomanip>#include <algorithm>#include <queue>#include <functional>#include <map>#include <string.h>#include <math.h>#include <list>using namespace std;const int MAX_SIZE = 501;int R, C, T, H, W;char G[MAX_SIZE][MAX_SIZE];int dir[8][2] = {-1, 0, -1, 1, 0, 1, 1, 1, 1, 0, 1, -1, 0, -1, -1, -1};int main() {    std::ios::sync_with_stdio(false);    bool firstTime = true;    while (1) {        cin >> H >> W;        if (cin.eof()) break;        R = C = T = 0;        int mini, minj, maxi, maxj;        int broadSize, starSize;        for (int i = 0; i < H; i++) cin >> G[i];        for (int i = 0; i < H; i++) {            for (int j = 0; j < W; j++) {                if (G[i][j] == '#') {                    starSize = 1;                    maxi = mini = i;                    maxj = minj = j;                    queue<pair<int, int> > q;                    q.push(make_pair(i, j));                    G[i][j] = 'v';                    while (!q.empty()) {                        pair<int, int> p = q.front();                        q.pop();                        for (int j = 0; j < 8; j++) {                            int ii = p.first + dir[j][0];                            int jj = p.second + dir[j][1];                            if (0 <= ii && ii < H && 0 <= jj && jj < W && G[ii][jj] == '#') {                                q.push(make_pair(ii, jj));                                G[ii][jj] = 'v';                                starSize++;                                if (mini > ii) mini = ii;                                if (minj > jj) minj = jj;                                if (maxi < ii) maxi = ii;                                if (maxj < jj) maxj = jj;                            }                        }                    }                    broadSize = (maxi - mini + 1) * (maxj - minj + 1);                    if (1.0 * starSize >= broadSize * 0.88) R++;                    else if (1.0 * starSize <= broadSize * 0.75) T++;                    else C++;                }            }        }        cout << "Rectangle: " << R << endl;        cout << "Circle: " << C << endl;        cout << "Triangle: " << T << endl << endl;    }    return 0;}                                 


0 0
原创粉丝点击