计蒜客--踏青java

蒜头君和他的朋友周末相约去召唤师峡谷踏青。他们发现召唤师峡谷的地图是由一块一块格子组成的,有的格子上是草丛,有的是空地。草丛通过上下左右 4 个方向扩展其他草丛形成一片草地,任何一片草地中的格子都是草丛,并且所有格子之间都能通过上下左右连通。如果用’G’代表草丛,’.’代表空地,下面的峡谷中有 2 片草地。
GG..
..GG
处在同一个草地的 2 个人可以相互看到,空地看不到草地里面的人。他们发现有一个朋友不见了,现在需要分头去找,每个人负责一片草地,蒜头君想知道他们至少需要多少人。
输入格式
第一行输入n, m(1≤n,m≤100) 表示峡谷大小
接下来输入 n 行字符串表示峡谷的地形
输出格式
输出至少需要多少人
样例输入
5 6
.#….
..#…
..#..#
…##.
.#….
样例输出
5
————————————————
版权声明:本文为CSDN博主「Liukairui」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/liukairui/article/details/79341597

package dfs;

import java.util.Scanner;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane.IconifyAction;

public class 踏青
{
    //判断有多少个草地
    static char [][] maze = new char[100][100];
    static int m;
    static int n;
    static int [][] pdf = {{-1,0},{0,-1},{1,0},{0,1}};//代表四个方向
    static boolean [][] vis = new boolean[100][100];
    static int count=0;
    
    public static void print()
    {
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                System.out.print(maze[i][j]);
            }
            System.out.println();
        }
    }
    
    public static boolean in(int tx,int ty)//边界
    {
        return 0<=tx&&tx<m&&0<=ty&&ty<n;
    }
    
    public static void dfs(int x,int y)
    {
        if(x == m-1&&y==n-1)
        {
            return;
        }
        vis[x][y] = true;
        maze[x][y] = 'M';//标记成M
        for(int i=0;i<4;i++)
        {
            int tx = x+pdf[i][0];
            int ty = y+pdf[i][1];
            if(in(tx, ty)&&maze[tx][ty]=='#'&&!vis[tx][ty])
            {
                dfs(tx, ty);
            }
        }
        vis[x][y] = false;
    }
    
    public static void suoyou()
    {
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(maze[i][j] == '#')//找到草时
                {
                    count = count+1;
                    dfs(i, j);//将找到的草坪变成M
                }
            }
        }
    }
    
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        m = scanner.nextInt();
        n = scanner.nextInt();
        for(int i=0;i<m;i++)
        {
            maze[i] = (scanner.next()).toCharArray();
        }
        suoyou();
        System.out.println(count);
        print();

    }

}

另解

package dfs;

import java.util.Scanner;

public class 踏青2
{
    //判断有多少个草地
        static char [][] maze = new char[100][100];
        static int m;
        static int n;
        static int [][] pdf = {{-1,0},{0,-1},{1,0},{0,1}};//代表四个方向
        static boolean [][] vis = new boolean[100][100];
        static int count=0;
        
    public static void dfs(int x,int y)
    {
        if(x<0||x>=m||y<0||y>=n||maze[x][y] == '.'||vis[x][y])//越界
        {
            return;
        }
        vis[x][y] = true;
        dfs(x-1, y);
        dfs(x, y-1);
        dfs(x+1, y);
        dfs(x, y+1);
    }

    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        m = scanner.nextInt();
        n = scanner.nextInt();
        for(int i=0;i<m;i++)
        {
            maze[i] = (scanner.next()).toCharArray();
        }
        
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(!vis[i][j] && maze[i][j] == '#')
                {
                    dfs(i,j);
                    count++;
                }
            }
        }
        System.out.print(count);

    }

}

猜你喜欢

转载自www.cnblogs.com/zkw123/p/12262762.html