您的位置:首页 > 其它

Sicily 13061. Block Party

2015-03-17 15:47 169 查看


13061. Block Party


Constraints

Time Limit: 1 secs, Memory Limit: 256 MB


Description

There are a variety of games such as Bejeweled and Shape Shift that are played on a grid of tiles, each having a color and sometimes another image. A move is a swap of two tiles that have the same shape on them. When a swap results in a chain 4 or more of the
same color, then those tiles are removed from the board and the tiles above them slide down to fill the gaps. (Two tiles are in a chain if they share a side.) After tiles have slid down, more chains might form. Note: No tiles slide until all chains in the
current configuration of the board have been removed.

A board can be represented by a rectangular arrangement of letters. One such arrangement is shown in the figure below. On that board, there are two chains of length 4, one of the character "R" and one of the character "B". Note that to be a chain, the characters
must be an exact match; case is significant. The start of a sequence of reactions is shown in the figure. Reactions continue until there are no more chains. In real games, new tiles replace open spaces in the grid, but we will not be concerned with replacement
tiles.

Write a program that processes the sequence of chain reactions in a board.




Input

The input is a sequence of test cases, each representing a board. The first line of each test case contains two nonnegative integer values w (0 < w < 256) and h (0 < h < 256) separated by a space giving the width and height of the board (in tiles). The line
containing the dimensions is followed by h lines of w non-blank characters each. The end of input is indicated by a line containing 0 0 for w and h. This case should not be processed.


Output

The output for each test case is the number of the test case (where the first test case is numbered 1) followed by a colon and a space followed by an integer value that shows the number of tiles remaining after all reactions complete.


Sample Input


7 9
YOOGYBY
GRGYBbB
OGRGOBY
BBGBRRB
YRYRROR
BOYBOBO
YGBYBBG
OOBYBRG
BBYRRBO
3 2
YBY
BYB
0 0



Sample Output


1: 51
2: 6



Problem Source

2014年每周一赛第十五场暨“指点传媒杯”第六届中山大学ICPC新手赛模拟赛

// Problem#: 13061
// Submission#: 3412393
// 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 = 260;

char G[MAX][MAX];
int colSize[MAX];
int W, H;
vector<pair<int, int> > block;
bool vis[MAX][MAX];
int dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};

void DFS(int nowi, int nowj) {
    vis[nowi][nowj] = true;
    block.push_back(make_pair(nowi, nowj));
    for (int i = 0; i < 4; i++) {
        int nexti = nowi + dir[i][0];
        int nextj = nowj + dir[i][1];
        if (0 <= nextj && nextj < W && 0 <= nexti && nexti < colSize[nextj] && G[nexti][nextj] == G[nowi][nowj] && !vis[nexti][nextj])
            DFS(nexti, nextj);
    }
}

void Slide() {
    for (int i = 0; i < W; i++) {
        queue<char> q;
        for (int j = 0; j < colSize[i]; j++) {
            if (G[j][i] != ' ') q.push(G[j][i]);
        }
        colSize[i] = q.size();
        for (int j = 0; j < colSize[i]; j++) {
            G[j][i] = q.front();
            q.pop();
        }
    }
}

int main() {

    std::ios::sync_with_stdio(false);

    int counter = 1;

    while (1) {
        cin >> W >> H;
        if (!W && !H) break;
        for (int i = H - 1; i >= 0; i--) cin >> G[i];
        for (int i = 0; i < W; i++) colSize[i] = H;
        bool isEnd;
        while (1) {
            isEnd = true;
            for (int i = 0; i < W; i++) {
                for (int j = 0; j < colSize[i]; j++) {
                    vis[j][i] = false;
                }
            }
            for (int i = 0; i < W; i++) {
                for (int j = 0; j < colSize[i]; j++) {
                    block.clear();
                    if (G[j][i] != ' ') DFS(j, i);
                    if (block.size() >= 4) {
                        isEnd = false;
                        int s = block.size();
                        for (int k = 0; k < s; k++) G[block[k].first][block[k].second] = ' ';
                    }
                }
            }
            if (isEnd) break;
            Slide();
        }
        int ans = 0;
        for (int i = 0; i < W; i++) ans += colSize[i];
        cout << counter++ << ": " << ans << endl;
    }

    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: