dfs
1. maps의 방문여부(isVisited), map의 크기 정보를 담은 N,M 정보, dx, dy(이동 방향 정보)를 초기화한다.
2. 이중 for문을 통해 위에서부터 오른쪽으로 한칸씩 이동하며 방문하지 않은 섬을 찾는다.
(maps[i].charAt(j) != 'X' && !isVisited[i][j])
3. 방문했다는 표시를 해준다. 또한 couter로 해당 섬의 며칠동안 머물 수 있는지에 대한 정보를 저장한다.
import java.util.*;
class Solution {
public int N, M;
public boolean [][] isVisited;
public Map<Integer, Integer> counter;
// 상, 우, 하, 좌
public int[] dx = {-1, 0, 1, 0};
public int[] dy = {0, 1, 0, -1};
public int[] solution(String[] maps) {
int[] answer = { -1 };
N = maps.length;
M = maps[0].length();
isVisited = new boolean[N][M];
counter = new HashMap<>();
int cntSeom = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (maps[i].charAt(j) != 'X' && !isVisited[i][j]) {
isVisited[i][j] = true;
counter.put(cntSeom, maps[i].charAt(j) - '0');
dfs(i, j, maps, cntSeom++);
}
}
}
List<Integer> list = new ArrayList<>(counter.values());
Collections.sort(list);
if (!counter.isEmpty()) {
answer = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
answer[i] = list.get(i);
}
}
return answer;
}
public void dfs(int x, int y, String[] maps, int cntSeom) {
for (int d = 0; d < 4; d++) {
int testX = x + dx[d];
int testY = y + dy[d];
if (!(0 <= testX && testX < N && 0 <= testY && testY < M)) continue;
if (maps[testX].charAt(testY) != 'X' && !isVisited[testX][testY]) {
isVisited[testX][testY] = true;
counter.put(cntSeom, counter.get(cntSeom) + maps[testX].charAt(testY) - '0');
dfs(testX, testY, maps, cntSeom);
}
}
}
}