Tuesday, April 28, 2015

UVa - 657 - The die is cast

#include <bits/stdc++.h>
#define scI(n) scanf("%d", &n)
#define scL(n) scanf("%lld", &n)
#define scS(n) scanf("%s", &n)
#define mn(n, m) (n > m)?m:n
#define mx(n, m) (n < m)?m:n
#define S 53
using namespace std;
typedef long long LL;

int dirX[] = {1, 0, -1, 0, -1, 1, 1, -1};
int dirY[] = {0, 1, 0, -1, -1, 1, -1, 1};

int n, m, tot;
string Die[S];
vector <int> Ans;

void dfs2(int i, int j){
    if(i < 0 || j < 0 || i >= m || j >= n)return;
    if(Die[i][j] != 'X')return;
    Die[i][j] = '.';
    for(int z = 0; z < 4; z++){
        int px = i+dirX[z];
        int py = j+dirY[z];
        dfs2(px, py);
    }
}

int dfs(int i, int j){
    if(i < 0 || j < 0 || i >= m || j >= n)return 0;
    if(Die[i][j] != '*' && Die[i][j] != 'X')return 0;
    if(Die[i][j] == '*')Die[i][j] = '.';
    if(Die[i][j] == 'X'){
        tot++;
        dfs2(i, j);
    }
    for(int z = 0; z < 4; z++){
        int px = i+dirX[z];
        int py = j+dirY[z];
        dfs(px, py);
    }
    return tot;
}

int main(){
    int cs = 0;
    while(cin >> n >> m){
        if(!n && !m)return 0;
        for(int i = 0; i < m; i++)cin >> Die[i];
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(Die[i][j] != '.'){
                    tot = 0;
                    Ans.push_back(dfs(i, j));
                }
            }
        }
        cout << "Throw " << ++cs << endl;
        sort(Ans.begin(), Ans.end());
        int sz = Ans.size();
        for(int i = 0; i < sz; i++){
            cout << Ans[i];
            if(i != sz-1)printf(" ");
        }
        puts("");
        puts("");
        Ans.clear();
    }
    return 0;
}

No comments:

Post a Comment