Created
July 24, 2022 06:34
-
-
Save ManduTheCat/127bf1ea8aa3953bc70a2ec9fc16515b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ssafy.ws.step3; | |
import java.io.*; | |
import java.util.*; | |
import java.util.stream.Collectors; | |
/** | |
* B구획의 빌딩 최고 높이 구하기 | |
*/ | |
public class BuildingTest { | |
/* | |
* 메인에서는 입력을 받으며 | |
* 최대값을 구하는 함수고 전달 | |
* findMaxBuilding | |
* */ | |
public static void main(String[] args) throws Exception{ | |
// 코드를 작성해주세요. | |
System.setIn(new FileInputStream("input.txt")); | |
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); | |
int inputCount = Integer.parseInt(bf.readLine()); | |
for(int i = 0; i< inputCount; i++){ | |
int N = Integer.parseInt(bf.readLine()); | |
String [][] city = new String[N][N]; | |
for(int row = 0; row < N; row++){ | |
String[] r = bf.readLine().split(" "); | |
for (int col = 0; col < N; col++){ | |
city[row][col] = r[col]; | |
} | |
} | |
System.out.printf("#%d %d\n",i + 1, findMaxBuilding(city,N)); | |
} | |
} | |
/* | |
* 원소를 순회하면서 주변이 B 로 이루어진 B 의 위치를 찾아내어 checkColumnRowBuilding 로 넘깁니다 | |
* | |
* @argument res 는 B 를 제외한 원소를 반환합니다 이는 list 의 size()를 활용해 주변 G 가 없는 B 를 찾기 위해서입니다 | |
* */ | |
public static int findMaxBuilding(String[][] city, int N){ | |
int [] dx = new int [] {-1,-1, 0, 1, 1, 1, 0,-1}; | |
int [] dy = new int [] { 0, 1, 1, 1, 0,-1,-1,-1}; | |
int dArrLen = dx.length; | |
//주변에 G 가 있는 경우 최소 2의 높이를 가질수 있습니다. | |
// 로직상 B 로만 이루어진 원소는 checkColumnRowBuilding 로 처리하기에 최소는 2가 맞습니다. | |
int maxCount = 2; | |
for (int i = 0; i < N; i++){ | |
for(int j = 0; j < N; j++){ | |
if(Objects.equals(city[i][j], "B")){ | |
List<String> checkCandidate = new ArrayList<>(); | |
for(int di = 0; di < dArrLen; di++){ | |
if(i + dx[di] >=0 && j + dy[di] >= 0 && i + dx[di]< N && j + dy[di] < N){ | |
checkCandidate.add(city[i + dx[di]][j+ dy[di]]); | |
} | |
} | |
List<String> res = checkCandidate.stream().filter(e->e.equals("G")).collect(Collectors.toList()); | |
if(res.size() == 0){ | |
maxCount = Math.max(maxCount, checkColumnRowBuilding(city, i, j)); | |
} | |
} | |
} | |
} | |
return maxCount; | |
} | |
/* | |
* 주변이 B 로 이루어진 B 의 column low 를 순회하여 B 의 갯수를 count 합니다 | |
* */ | |
public static int checkColumnRowBuilding(String[][] city, int i, int j ){ | |
int len = city.length; | |
int buildingCount = 0; | |
for(int idx = 0; idx < len; idx++){ | |
if(Objects.equals(city[i][idx], "B")){ | |
buildingCount++; | |
} | |
if(Objects.equals(city[idx][j], "B")){ | |
buildingCount++; | |
} | |
} | |
// 자기 자신을 중복해서 count 하는 부분 -1 로 처리 | |
return buildingCount - 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment