zoukankan      html  css  js  c++  java
  • 深搜基础题目 杭电 HDU 1241

    HDU 1241 是深搜算法的入门题目,递归实现。

    原题目传送门: 

    http://acm.hdu.edu.cn/showproblem.php?pid=1241

    代码仅供参考,c++实现:

    #include <iostream>
    using namespace std;
    
    char land[150][150];
    int p,q;
    
    void dfs(int x,int y){
        land[x][y] = '*';
        if(land[x-1][y]!= '@' && land[x+1][y] != '@' && land[x][y-1] != '@' && land[x][y+1] != '@' && land[x-1][y+1]!= '@' && land[x+1][y-1] != '@' && land[x-1][y-1] != '@' && land[x+1][y+1] != '@')
            return ;
        
        if(x+1 > 0 && y+1 > 0 && x+1 <= p && y+1 <= q && land[x+1][y+1]== '@') { dfs(x+1,y+1);  }
        if(x+1 > 0 && y-1 > 0 && x+1 <= p && y-1 <= q && land[x+1][y-1]== '@') { dfs(x+1,y-1);  }
        if(y-1 > 0 && x-1 > 0 && x-1 <= p && y-1 <= q && land[x-1][y-1]== '@') { dfs(x-1,y-1);  }
        if(y+1 > 0 && x-1 > 0 && x-1 <= p && y+1 <= q && land[x-1][y+1]== '@') { dfs(x-1,y+1);  }
        
        if(x+1 > 0 && y > 0 && x+1 <= p && y <= q && land[x+1][y]== '@') { dfs(x+1,y);  }
        if(x-1 > 0 && y > 0 && x-1 <= p && y <= q && land[x-1][y]== '@') { dfs(x-1,y);  }
        if(y-1 > 0 && x > 0 && x <= p && y-1 <= q && land[x][y-1]== '@') { dfs(x,y-1);  }
        if(y+1 > 0 && x > 0 && x <= p && y+1 <= q && land[x][y+1]== '@') { dfs(x,y+1);  }
       
        return ;
    }
    int main(int argc, const char * argv[]) {
        
        int num;
        cin>>p>>q;
        while(p>0 && q>0){
        
                num = 0;
                for(int i = 1; i <= p ;i++){
                    for(int j = 1; j<= q; j++){
                        cin>>land[i][j];
                    }
                }
                for(int i = 1; i <= p ;i++){
                    for(int j = 1; j<= q; j++){
                        if(land[i][j] == '@'){
                            num++;
                            dfs(i,j);
                        }
                        
                    }
                }
                cout<< num<<endl;
            cin>>p>>q;
        }
        return 0;
    }
  • 相关阅读:
    Redhat 7使用CentOS 7的Yum网络源
    指定YUM安装包的体系结构或版本
    CURL常用命令
    VIM技巧之去除代码行号并缩进代码
    VIM 中鼠标选择不选中行号
    linux服务器性能优化
    阻塞,非阻塞,同步,异步
    WEB三层架构与MVC
    mvc与三层结构
    Centos环境下Tomcat启动缓慢
  • 原文地址:https://www.cnblogs.com/sweetiemelody/p/4296824.html
Copyright © 2011-2022 走看看