백준 6186 Best Grass 문제풀이 (JAVA)

0

문제 링크

문제


Bessie is planning her day of munching tender spring grass and is gazing out upon the pasture which Farmer John has so lovingly partitioned into a grid with R (1 <= R <= 100) rows and C (1 <= C <= 100) columns. She wishes to count the number of grass clumps in the pasture.

Each grass clump is shown on a map as either a single '#' symbol or perhaps two '#' symbols side-by-side (but not on a diagonal). No two symbols representing two different clumps are adjacent. Given a map of the pasture, tell Bessie how many grass clumps there are.

By way of example, consider this pasture map where R=5 and C=6:

.#....
..#...
..#..#
...##.
.#....

This pasture has a total of 5 clumps: one on the first row, one that spans the second and third row in column 2, one by itself on the third row, one that spans columns 4 and 5 in row 4, and one more in row 5.

입력


Line 1: Two space-separated integers: R and C
Lines 2..R+1: Line i+1 describes row i of the field with C characters, each of which is a '#' or a '.'

출력


Line 1: A single integer that is the number of grass clumps Bessie can munch

풀이


영어로 된 알고리즘 문제를 풀면 영어 실력도 늘고, 알고리즘 실력도 늘 줄 알았지만, 정작 늘어나는 것은 욕과 한숨이었다.

하찮은 영어실력으로 해석해 보자면.....

대강 풀 몇뭉탱이냐 묻는 것 같다 영문 7줄을 한글 11글자로 줄이는 클라스

반복문 돌리다가 풀 만나면 십자로 있는 풀 싸그리 태워버렸다.

이 문제를 통해 우리가 중점적으로 봐야할 점은 영어 공부는 선택이 아니라 필수라는 것이다...!

소스코드


import java.util.*;
import java.io.*;
public class Main{
    public static boolean ground[][];
    public static int ROWS;
    public static int COLS;
    public static void main(String [] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringBuilder sb = new StringBuilder();
        StringTokenizer st = new StringTokenizer(br.readLine());
        ROWS = Integer.parseInt(st.nextToken());
        COLS = Integer.parseInt(st.nextToken());
        
        ground= new boolean[ROWS][COLS];
        for(int i=0;i<ROWS;i++) {
            String temp = br.readLine();
            
            for(int j=0;j<COLS;j++) {
                if(temp.charAt(j)=='#') {
                    ground[i][j] = true;
                }
            }
        }
        int answer = 0;
        for(int i=0;i<ROWS;i++) {
             for(int j=0;j<COLS;j++) { 
                 if(ground[i][j]) {
                     answer++;
                     
                     burnGrass(i,j);
                 }
             }
        }
        
        sb.append(answer);
        sb.append("\n"); 
        
        bw.write(sb.toString());
        
        bw.flush();
        br.close();
        bw.close();
        
    }
    public static void burnGrass(int row, int col) {
        if(row<0 || row==ROWS || col<0 || col==COLS) {
            return;
        }
        if(!ground[row][col]) {
            return;
        }
        
        ground[row][col] = false;
        
        burnGrass(row-1,col);
        burnGrass(row+1,col);
        burnGrass(row,col-1);
        burnGrass(row,col+1);
        
    }

    
}

0개의 댓글

관련 채용 정보