728x90
https://www.acmicpc.net/problem/2178
2178번: 미로 탐색
첫째 줄에 두 정수 N, M(2 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 M개의 정수로 미로가 주어진다. 각각의 수들은 붙어서 입력으로 주어진다.
www.acmicpc.net
// 코드
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
|
import java.util.*;
import java.io.*;
public class Main{
// 상, 하, 좌, 우
static int[][] dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
static int N, M;
static int[][] arr;
static int[][] result;
static void BFS() {
Deque<int[]> deq = new ArrayDeque<>();
deq.add(new int[]{0, 0});
int cnt = 1;
result[0][0] = cnt++;
while(!deq.isEmpty()) {
int[] getV = deq.pop();
for(int i=0; i<4; i++) {
// 상, 하, 좌, 우
int ny = dir[i][0] + getV[0];
int nx = dir[i][1] + getV[1];
if((0 <= ny && ny < N) && (0 <= nx && nx < M) && (result[ny][nx] == 0) && (arr[ny][nx] == 1)) {
result[ny][nx] = Math.max(result[getV[0]][nx], result[ny][getV[1]]) + 1;
deq.add(new int[]{ny, nx});
}
}
}
}
// main
public static void main(String[] args) throws Exception {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
arr = new int[N][M];
result = new int[N][M];
for(int i=0; i<N; i++) {
arr[i] = Arrays.stream(br.readLine().split("")).mapToInt(Integer::parseInt).toArray();
}
BFS();
bw.write(String.valueOf(result[N-1][M-1]));
br.close();
bw.flush();
bw.close();
}
}
|
cs |
// 복습
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
|
import java.util.*;
import java.io.*;
public class Main {
static final int[][] DELTAS = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
static int N, M;
static int[][] arr, visited;
// main
public static void main( String[] args ) throws Exception {
// Please Enter Your Code Here
BufferedWriter bw = new BufferedWriter( new OutputStreamWriter( System.out ) );
BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
StringTokenizer st = new StringTokenizer( br.readLine() );
N = Integer.parseInt( st.nextToken() );
M = Integer.parseInt( st.nextToken() );
arr = new int[N][M];
visited = new int[N][M];
for ( int i = 0; i < N; ++i ) {
String getStr = br.readLine();
for ( int j = 0; j < M; ++j ) arr[i][j] = Integer.parseInt( String.valueOf( getStr.charAt(j) ) );
}
bfs();
int ret = visited[N-1][M-1] + 1;
bw.write( String.valueOf(ret) );
bw.flush();
bw.close();
br.close();
}
public static void bfs() {
Deque< int[] > deq = new ArrayDeque<>();
deq.offer( new int[] { 0, 0 } );
while ( !deq.isEmpty() ) {
int[] pos = deq.poll();
int posY = pos[0], posX = pos[1];
for ( int dir = 0; dir < 4; ++dir ) {
int ny = posY + DELTAS[dir][0], nx = posX + DELTAS[dir][1];
if ( ny < 0 || ny >= N || nx < 0 || nx >= M || ( ny == 0 && nx == 0 ) || arr[ny][nx] == 0 || visited[ny][nx] != 0 ) continue;
deq.offer( new int[] { ny, nx } );
visited[ny][nx] = visited[posY][posX] + 1;
}
}
}
}
|
cs |
반응형
'CS > 알고리즘_문제풀이(자바)' 카테고리의 다른 글
1, 2, 3 더하기 5 (0) | 2021.10.07 |
---|---|
토마토 (0) | 2021.10.06 |
N과 M (7) (0) | 2021.10.05 |
N과 M (8) (0) | 2021.10.05 |
차이를 최대로 (0) | 2021.10.04 |