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;
    }
  • 相关阅读:
    CentOS 7 如何设置默认启动方式为命令行模式
    Virtual Box配置CentOS7网络
    序列化后成对象转map,再添加到dataList
    centos7 ping: www.baidu.com: Name or service not known
    协议1
    idea查看接口方法实现
    centos关闭防火墙
    myeclipse配置svn
    eas固定ip避免多次申请许可
    jvm配置
  • 原文地址:https://www.cnblogs.com/oleolema/p/9029553.html
Copyright © 2011-2022 走看看