uva 657 - The die is cast

// uva 657 - The die is cast
// 题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=598
// 题目大意: 求每个区域内连通X的块数,然后排序输出
// 一开始没注意到要排序,然后wa了,后来在poj找的测试数据,才发现要排序T_T
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
int row, column;
struct Node{
    int x, y;
} now_node;
queue<Node> node_que;
char map[100][100];
bool visit[100][100];
int record[100][100];
int cnt_X, cnt_case, save_pos;
int direction[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int save_ans[1300];
bool Judge(char);
void Init();
bool Inmap(Node);
void BFS();
void DFS(Node);
int main(){
    while(scanf("%d %d", &column, &row), row || column){
        getchar();
        Init();
        cout << "Throw " << ++cnt_case << endl;
        for(int i = 0; i < row; i++)
            gets(map[i]);
        for(int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++){
                if (Judge(map[i][j]) && !visit[i][j]){
                    now_node.x = i;
                    now_node.y = j;
                    BFS(); 
                }
            }
        }
        sort(save_ans, save_ans + save_pos);
        for(int i = 0; i < save_pos; i++){
            if(i)
                cout << " ";
            cout << save_ans[i];
        }
        cout << endl << endl;
    }
    return 0;
}
void Init(){
    memset(map, 0, sizeof(map));
    memset(visit, 0, sizeof(visit));
    memset(save_ans, 0, sizeof(save_pos));
    memset(record, 0, sizeof(record));
    save_pos = 0;
}
bool Inmap(Node node){
    return node.x >= 0 && node.y >= 0 && node.x < row && node.y < column;
}
bool Judge(char element){
    return element == '*' || element == 'X';
}
void BFS(){
    Node tmp_node;
    cnt_X = 0;
    visit[now_node.x][now_node.y] = true;
    node_que.push(now_node);
    while(node_que.size()) {
        now_node = node_que.front();
        node_que.pop();
        for(int i = 0; i < 4; i++){
            tmp_node.x = now_node.x + direction[i][0];
            tmp_node.y = now_node.y + direction[i][1];
            if(Inmap(tmp_node) && Judge(map[tmp_node.x][tmp_node.y]) && !visit[tmp_node.x][tmp_node.y]){
                visit[tmp_node.x][tmp_node.y] = true;
                node_que.push(tmp_node);
                if(map[tmp_node.x][tmp_node.y] == 'X' && !record[tmp_node.x][tmp_node.y]){
                    record[tmp_node.x][tmp_node.y] = ++cnt_X;
                    DFS(tmp_node);
                }
            }
        }
    }
    save_ans[save_pos++] = cnt_X;
}
void DFS(Node node){
    Node tmp_node;
    for(int i = 0; i < 4; i++) {
        tmp_node.x = node.x + direction[i][0];
        tmp_node.y = node.y + direction[i][1];
        if(Inmap(tmp_node) && !record[tmp_node.x][tmp_node.y]  && map[tmp_node.x][tmp_node.y] == 'X'){
            record[tmp_node.x][tmp_node.y] = cnt_X;
            DFS(tmp_node);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值