油田问题(java版)

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid. 

Input

The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket. 

Output

For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets. 

Sample Input

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

Sample Output

0
1
2
2

水题,遍历地图,找到'@'就进去 dfs 遍历,并且 ans++,然后将每次遍历到的'@'都标记为访问过

import java.util.*;

public class Main {
    static int n,m;
    static String[] map;//地图
    static int[][] vis;//标记是否访问过
    static int ans;//答案
    static int[][] move = {{0,1},{0,-1},{1,0},{-1,0},{-1,-1},{1,-1},{-1,1},{1,1}};
    
    static void dfs(int x,int y) {
        for(int i = 0;i < 8;i++) {
            int nx = x + move[i][0];
            int ny = y + move[i][1];
            if(check(nx,ny)) {
                vis[nx][ny] = 1;
                dfs(nx,ny);
            }
        }
    }
    static boolean check(int x,int y) {
        return x >= 0 && x < n && y >= 0 && y < m && map[x].charAt(y) == '@' && vis[x][y] == 0;
    }
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext()) {
            n = cin.nextInt();
            m = cin.nextInt();
            map = new String[n];
            vis = new int[n][m];
            ans = 0;
            if(n == 0 && m == 0)
                break;
            for(int i = 0;i < n;i++) 
                map[i] = cin.next();
            for(int i = 0;i < n;i++) 
                for(int j = 0;j < m;j++) 
                    if(map[i].charAt(j) == '@' && vis[i][j] == 0) {
                        ans++;
                        vis[i][j] = 1;
                        dfs(i,j);
                    }
            System.out.println(ans);
        }
    }
}

 

全部评论

相关推荐

认真搞学习:28小登的建议,投算法岗不要写什么物理竞赛,互联网+,多写点项目,用什么算法做了什么。还有本科算法是不可能的开发你这个也没有项目啊
点赞 评论 收藏
分享
mama3925:建议专业技能里测试移到最上面,加粗。然后适当加入些自动化测试工具。第二个项目,第三条亮点最后错别字。然后佬如果对自己很自信的话,可以项目放前面,然后项目里可以编造点测试经历,写在写在项目亮点的前两行。最后可加个自我评价,放个博客或者仓库链接
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务