zoukankan      html  css  js  c++  java
  • 图1(八连块)

    输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块。如果两个字符“@”所在的格子相邻(横、竖或者对角线方向),就说它们属于同一个八连块。

    Sample Input

    1 1 

    3 5 
    *@*@* 
    **@** 
    *@*@* 
    1 8 
    @@****@* 
    5 5 
    ****@ 
    *@@*@ 
    *@**@ 
    @@@*@ 
    @@**@ 
    0 0

    Sample Output




    2

    #include"iostream"
    using namespace std;
    char **map;    
    int w,h;
    bool selected(int &y,int &x,int n){
        int i[8] = {-1,0,1,1,1,0,-1,-1};
        int j[8] = {-1,-1,-1,0,1,1,1,0};
        if(i[n] + y >= 0 && i[n] + y < h && j[n] + x >= 0 && j[n] + x < w && map[i[n] + y][j[n] + x] == '@'){
            y += i[n];
            x += j[n];
            return true;
        }
        return false;
    }
    void find8(int y,int x){
        if(map[y][x] == '@'){
            map[y][x] = '*';
            int x1 = x,y1 = y;
            for(int i = 0;i < 8;i++){
                if(selected(y,x,i)){
                    find8(y,x);
                }
                x = x1;
                y = y1;
            }
        }
    }
    void create(){
        map = new char*[h];
        for(int i = 0;i < h;i++){
            map[i] = new char[w];
            for(int j = 0;j < w;j++){
                cin>>map[i][j];
            }
        }
    }
    
    int main(){
        while(cin>>w>>h && w && h){
            int a = 2,b = 2;
            int count = 0;
            create();
            for(int i = 0;i < h;i++){
                for(int j = 0;j < w;j++){
                    if(map[i][j] == '@'){
                        find8(i,j);
                        count++;
                    }
                }
            }
            cout<<count<<endl;
            for(i = 0;i < h;i++){
                delete[] map[i];
            }
        }
        return 0;
    }
  • 相关阅读:
    sql 数据库 初级 个人学习总结(一)
    parentViewController
    关于iOS9之后的loadViewIfNeeded
    判断版本号
    MagicalRecord(简化CoreData操作)
    coreData
    PureLayout(轻量级自动布局)
    MJRefresh(上拉加载下拉刷新)
    MJExtension(JSON到数据模型的自动转换)
    BaceModel
  • 原文地址:https://www.cnblogs.com/oleolema/p/9029553.html
Copyright © 2011-2022 走看看