티스토리 뷰
10966. 물놀이를 가자[SWEA] 10966. 물놀이를 가자
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
bfs는 여러개의 지점에서 동시에 탐색을 수행할 수 있다.
따라서 물의 위치를 큐에 넣어놓고, 큐가 빌 때 까지 bfs 탐색을 한다.
import java.io.*;
import java.util.*;
// 210315
public class Solution_D4_10966_물놀이를가자 {
static int N, M;
static char[][] map;
static int[][]visited;
static int[] dr = {-1, 0, 1, 0};
static int[] dc = {0, -1, 0, 1};
static Queue<Position> q;
static int stoi(String str) {
return Integer.parseInt(str);
}
static class Position {
int r;
int c;
public Position(int r, int c) {
super();
this.r = r;
this.c = c;
}
@Override
public String toString() {
return "Position [r=" + r + ", c=" + c + "]";
}
}
static void bfs() {
while (!q.isEmpty()) {
Position p = q.poll();
for(int d=0; d<4; d++) {
int nr = p.r + dr[d];
int nc = p.c + dc[d];
if(nr<0 || nr>=N || nc<0 || nc>=M || visited[nr][nc]>0 || map[nr][nc]=='W') continue;
visited[nr][nc] = visited[p.r][p.c] + 1;
q.offer(new Position(nr, nc));
}
}
}
public static void main(String[] args) throws Exception {
//System.setIn(new FileInputStream("res/input_D4_10966_물놀이를가자.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
StringBuilder sb = new StringBuilder();
int T = stoi(br.readLine());
for(int tc=1; tc<=T; tc++) {
st = new StringTokenizer(br.readLine(), " ");
N = stoi(st.nextToken());
M = stoi(st.nextToken());
map = new char[N][M];
visited = new int[N][M];
q = new ArrayDeque<>();
for(int i=0; i<N; i++) {
map[i] = br.readLine().toCharArray();
for(int j=0; j<M; j++) {
if(map[i][j]=='W') {
q.offer(new Position(i, j));
}
}
}
bfs();
int ans = 0;
sb.append("visited 배열 \n");
for(int i=0; i<N; i++) {
for(int j=0; j<M; j++) {
ans += visited[i][j];
sb.append(visited[i][j] + " ");
}
sb.append("\n");
}
sb.append("#" + tc + " " + ans + "\n");
}
System.out.println(sb.toString());
br.close();
} //
}
'코딩테스트 > SW Expert' 카테고리의 다른 글
[SWEA] 8382. 방향 전환 (0) | 2021.03.22 |
---|---|
[SWEA] 1767. [SW Test 샘플문제] 프로세서 연결하기 (0) | 2021.03.17 |
[SWEA] 4012. [모의 SW 역량테스트] 요리사 (0) | 2021.03.14 |
[SWEA] 7991. 줄 세우기 (0) | 2021.02.21 |
[SWEA] 4789. 성공적인 공연 기획 (0) | 2021.02.21 |
댓글