일반적인 bfs와 달리 각 정점 ( 위 문제에선 L) 을 기준으로 다 bfs를 진행주면 된다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
public class Main {
static int n,m;
static int max = 0;
static int dist[][];
static int dx[] = {0,0,1,-1};
static int dy[] = {1,-1,0,0};
static Character map [][];
static boolean visited[][];
static ArrayList<Node>list = new ArrayList<>();
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] t = br.readLine().split(" ");
n = Integer.parseInt(t[0]);
m = Integer.parseInt(t[1]);
map = new Character [n][m];
visited = new boolean[n][m];
dist = new int[n][m];
for(int i=0; i<n; i++) {
String str = br.readLine();
for(int j=0; j<m; j++) {
map[i][j]= str.charAt(j);
if(map[i][j]=='L') {
list.add(new Node(i,j));
}
}
}
Node a = list.get(i);
visited = new boolean[n][m];
bfs(a);
}
System.out.println(max);
}
public static void bfs(Node o) {
Queue<Node> q = new LinkedList<>();
q.add(o);
visited[o.x][o.y] = true;
dist = new int[n][m];
while(!q.isEmpty()) {
Node a = q.poll();
for(int i=0; i<4; i++) {
int nx = a.x+dx[i];
int ny = a.y+dy[i];
if(nx>=0 && ny>=0 && nx<n && ny<m) {
if(map[nx][ny]=='L' && !visited[nx][ny]) {
visited[nx][ny]= true;
dist[nx][ny] = dist[a.x][a.y]+1;
q.add(new Node(nx,ny));
}
}
}
}
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
}
}
}
}
class Node{
int x,y;
Node(int x, int y){
this.x=x;
this.y=y;
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
'알고리즘' 카테고리의 다른 글
[백준 9328] 열쇠 -JAVA //le_effort// (0) | 2020.03.04 |
---|---|
[백준 9376] 탈옥 -JAVA // le_effort// (0) | 2020.03.04 |
[백준 12851] 숨바꼭질2 -JAVA // le_effort// (0) | 2020.03.03 |
[백준 2251] 물통 -JAVA // le_effort// (1) | 2020.03.03 |
[백준9019] DSLR -JAVA // le_effort// (0) | 2020.03.03 |