uva 657 The die in cast (掷骰子) —— DFS

/**
序号:num_3
作者:MrZhang
日期:2016-5-20

题目名称: The die in cast(掷骰子)
题目来源:
uva —— 657 —— The die is cast
网址:
英文题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19322
中文翻译:http://www.cnblogs.com/scau20110726/archive/2012/11/11/2764626.html

数据样例:
30 15
..............................
..............................
...............*..............
...*****......****............
...*X***.....**X***...........
...*****....***X**............
...***X*.....****.............
...*****.......*..............
..............................
........***........******.....
.......**X****.....*X**X*.....
......*******......******.....
.....****X**.......*X**X*.....
........***........******.....
..............................
0 0

输出:
Throw 1
1 2 2 4
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#define maxn 60

using namespace std;
char G[maxn][maxn];
int visitedI[maxn][maxn];  //!遍历 以'.'为背景的整个区域,   找区域I  时 使用
int visitedII[maxn][maxn]; //!在   以'.'为背景的整个区域中, 找区域II 时 使用。
int h,w;  //!以'.'为背景的整个区域 共有 h行,w列
int numi; //!区域I中区域II的个数,每进入一个新的区域I时将numi清零。

vector<int> num; //!以'.'为背景的整个区域中有多个区域I,每个区域I中有多个区域II,num一次存储每个区域I中区域II的个数。

bool checkI(int x,int y){ //!用DFSI()遍历以'.'为背景的整个区域,找区域I时使用此函数检查。
    if(x >= 0 &&x <= h-1 && y >= 0 && y <= w-1){ //!是否越界
        if((G[x][y] == '*' || G[x][y] == 'X') && !visitedI[x][y]){
            return true;
        }
    }

    return false;
}

bool checkII(int x,int y){//!用DFSII()遍历某个区域I,找区域II时使用此函数检查。
    if(x >= 0 &&x <= h-1 && y >= 0 && y <= w-1){
        if(G[x][y] == 'X' && !visitedII[x][y]){
            return true;
        }
    }

    return false;
}

//!        上下 左右
int dx[6]={-1,1, 0,0};
int dy[6]={ 0,0,-1,1};

void DFSII(int x,int y){ //!在某个区域I中找区域II
    for(int i=0;i<4;i++){
        int u = x + dx[i],v = y + dy[i];
        if(checkII(u,v)){
            visitedII[u][v] = 1;
            DFSII(u,v);
        }
    }

}

/**
使用双重DFS()遍历该区域,找区域I中区域II的个数。
*/
void DFSI(int x,int y){//!在 以'.'为背景的整个区域 中找区域I
    for(int i=0;i<4;i++){
        int u = x + dx[i],v = y + dy[i];
        if(checkI(u,v)){
            visitedI[u][v] = 1;
            if(checkII(u,v)){//!在遍历当前区域I时,找到了一个新的区域II
                visitedII[u][v] = 1;
                numi ++; //!将计数加1
                DFSII(u,v);//!遍历这个新区域II
            }
            DFSI(u,v);//!继续遍历该区域I
        }
    }
}

int main(){
    int orderNum = 1;
    while(scanf("%d%d",&w,&h) == 2){//!h行,w列
        if(!w && !h) break;

        memset(G,'.',sizeof(G));
        memset(visitedI,0,sizeof(visitedI));
        memset(visitedII,0,sizeof(visitedII));

        num.clear();

        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin>>G[i][j];
            }
        }

        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(G[i][j] == '*' && !visitedI[i][j]){ //!说明找到了一个新的区域I
                    visitedI[i][j] = 1;
                    numi = 0;//!初始化区域II的个数
                    DFSI(i,j);//!遍历这个新的区域I,并在此函数中计算 该区域I中的 区域II 的个数
                    num.push_back(numi); //!记录下 新找到的区域I 中的区域II的个数
                }
            }
        }

        cout<<"Throw "<<orderNum ++<<endl;
        sort(num.begin(),num.end());
        for(int i=0;i<num.size();i++){
            if(!i) cout<<num[i];
            else cout<<" "<<num[i];
        }
        cout<<endl<<endl;
    }
    return 0;
}

/**
总结:

题型:
大标题:图的遍历
小标题:计算“图1中的图2”

原理:深度优先搜索(DFS)

技巧:使用双重DFS(),将DFS()嵌套使用。
用一层DFSI()找图1,用二层DFSII()找图2。

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张之海

若有帮助,客官打赏一分吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值