Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- SWEA
- dfs
- 시뮬레이션
- 문자열
- DP
- 문제해결
- 코테
- math
- 건강
- 백준
- 이분탐색
- 러닝일지
- 알고리즘
- oracle
- 투포인터
- Java
- 프로그래머스
- BOJ
- priorityqueue
- 운동기록
- db
- 문제풀이
- greedy
- binary search
- 자바
- SQL
- COS PRO
- MySQL
- 코딩테스트
- BFS
Archives
- Today
- Total
슈콩
[BOJ] 2178 미로 탐색 본문
[문제]
https://www.acmicpc.net/problem/2178
[소스 코드]
import java.util.*;
import java.io.*;
public class Solution {
static int[] dr = {-1,1,0,0};
static int[] dc = {0,0,-1,1};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int[][] map = new int[n][m];
for(int i=0;i<n;i++) {
String s = br.readLine();
for(int j=0;j<m;j++) {
map[i][j] = s.charAt(j) - '0';
}
}
boolean[][] visit = new boolean[n][m];
Queue<int[]> q = new LinkedList<>();
visit[0][0] = true;
q.offer(new int[] {0,0,1});
while(!q.isEmpty()) {
int[] curr = q.poll();
if(curr[0]==n-1 && curr[1]==m-1) {
System.out.println(curr[2]);
return;
}
for(int d=0;d<4;d++) {
int nr = curr[0] + dr[d];
int nc = curr[1] + dc[d];
if(nr<0 || nr>=n || nc<0 || nc>=m || visit[nr][nc]) continue;
if(map[nr][nc]==1) {
visit[nr][nc] = true;
q.offer(new int[] {nr,nc,curr[2]+1});
}
}
}
}
}'Algorithms > Baekjoon' 카테고리의 다른 글
| [BOJ] 2667 단지번호붙이기 (0) | 2026.01.22 |
|---|---|
| [BOJ] 2606 바이러스 (0) | 2026.01.20 |
| [BOJ] 1260 DFS와 BFS (3) | 2026.01.08 |
| [BOJ] 19236 청소년 상어 (0) | 2025.12.17 |
| [BOJ] 17825 주사위 윷놀이 (5) | 2025.12.16 |