카테고리 없음
백준 1303번 전쟁-전투 (JAVA)
박카스마시며코딩
2022. 1. 9. 23:41
https://www.acmicpc.net/problem/1303
1303번: 전쟁 - 전투
첫째 줄에는 전쟁터의 가로 크기 N, 세로 크기 M(1 ≤ N, M ≤ 100)이 주어진다. 그 다음 두 번째 줄에서 M+1번째 줄에는 각각 (X, Y)에 있는 병사들의 옷색이 띄어쓰기 없이 주어진다. 모든 자리에는
www.acmicpc.net
저는 해당 문제를 DFS를 통해서 문제를 해결하였습니다. DFS를 통해 해당 연결된 개수가 몇개인지를 확인하고 이를 제곱하여 각각의 결과값에 더해주었습니다.
package BOJ.DFS;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.function.Function;
public class BOJ_1303 {
static final int[] dy = {-1,0,1,0};
static final int[] dx = {0,1,0,-1};
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String command = br.readLine();
String[] str = command.split(" ");
Function<String,Integer> stoi = Integer::parseInt;
int m = stoi.apply(str[0]);
int n = stoi.apply(str[1]);
char map[][] = new char[n][m];
for(int i = 0 ; i < n ; i++){
command = br.readLine();
for(int j = 0 ; j < m ; j++){
map[i][j] = command.charAt(j);
}
}
int[] result = new int[2];
boolean[][] visited = new boolean[n][m];
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < m ; j++){
if(!visited[i][j] && map[i][j] == 'W'){
result[0] += Math.pow(dfs(map,n,m,i,j,visited),2);
}else if(!visited[i][j] && map[i][j] == 'B'){
result[1] += Math.pow(dfs(map,n,m,i,j,visited),2);
}
}
}
System.out.println(result[0]+" "+result[1]);
}
private static int dfs(char[][] map, int n, int m, int y, int x, boolean[][] visited) {
visited[y][x] = true;
int result = 1;
for(int i = 0 ; i < 4 ; i++){
int ny = y + dy[i];
int nx = x + dx[i];
if(ny >= 0 && ny < n && nx >= 0 && nx < m && !visited[ny][nx] && map[ny][nx] == map[y][x]){
result += dfs(map,n,m,ny,nx,visited);
}
}
return result;
}
}