import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static int[][] matrix;
static boolean[][] visited;
static int[] dx = {0,1,0,-1};
static int[] dy = {1,0,-1,0};
static int N,M;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //선언
//입력 값 받기
StringTokenizer stringTokenizer = new StringTokenizer(br.readLine());
N = Integer.parseInt(stringTokenizer.nextToken());
M = Integer.parseInt(stringTokenizer.nextToken());
//초기화
matrix = new int[N][M];
visited = new boolean[N][M];
for(int i=0; i<N; i++){
stringTokenizer = new StringTokenizer(br.readLine());
String line = stringTokenizer.nextToken();
for(int j=0; j<M; j++){
matrix[i][j] = Integer.parseInt(line.substring(j, j+1));
}
}
BFS(0,0);
System.out.println(matrix[N-1][M-1]);
}
public static void BFS(int x, int y){
Queue<int[]> queue = new LinkedList<>();
queue.add(new int[]{x,y});
while(!queue.isEmpty()){
int[] now = queue.poll();
visited[x][y] = true;
for(int i=0; i<4; i++){
int X = now[0] + dx[i];
int Y = now[1] + dy[i];
if(X >= 0 && Y >= 0 && X < N && Y < M){
if(!visited[X][Y] && matrix[X][Y]==1){
queue.add(new int[]{X,Y});
visited[X][Y] = true;
matrix[X][Y] = matrix[now[0]][now[1]] + 1;
}
}
}
}
}
}
import java.util.*;
class Solution {
static int[][] A;
static boolean[][] visited;
static int[] dx = {0, 1, 0, -1};
static int[] dy = {1, 0, -1, 0};
static int n, m;
public int solution(int[][] maps) {
//초기화
A = maps;
n = maps.length;
m = maps[0].length;
visited = new boolean[n][m];
BFS(0,0);
return A[n-1][m-1]==1 ? -1 : A[n-1][m-1];
}
public static void BFS(int x, int y){
Queue<int[]> queue = new LinkedList<>();
queue.add(new int[]{x,y});
visited[x][y] = true;
while(!queue.isEmpty()){
int[] now = queue.poll();
for(int i=0; i<4; i++){
int X = now[0] + dx[i];
int Y = now[1] + dy[i];
if(X >= 0 && Y >= 0 && X < n && Y < m){
if(!visited[X][Y] && A[X][Y]==1){
queue.add(new int[]{X,Y});
visited[X][Y] = true;
A[X][Y] = A[now[0]][now[1]] + 1;
}
}
}
}
}
}
class Solution {
public int solution(int n) {
int left = 1;
int right = 1;
int sum = 1;
//n으로 n을 나타내는 경우 1 초기화
int count = 1;
while(right != n){
if(sum == n){
count++;
right++;
sum += right;
}
if(sum < n){
right++;
sum += right;
}
if(sum > n){
sum -= left;
left++;
}
}
return count;
}
}
밤엔 photoday project 리팩토링 아주 조금 했다.
좀 더 자주 손대야지.
BFS, DFS는 조금씩 감을 잡아가고 있다. 하다보면 늘겠지.
AOP는 읽으면 어려운데, 경험상 차라리 한 번 해보는 게 더 낫다.
(분명 프로젝트에서 AOP 구현했었는데, 글로 읽으니까 넘 복잡스)
이번주 목표는 회사에 입사 지원하기★
매달 수많은 수료생들이 나오기 전에 얼른 시작하자~
✏️ 요즘 하고있는 공부들
: 유어 클래스 다시 읽기, 코테 연습, SQL 공부, CS 공부, photoday project 리팩토링, 이력서 준비