728x90
300x250
import java.util.Scanner;
public class Solution {
public static int[][] map;
public static boolean[][] visit;
public static int res = 0;
public static int fin[] = new int[2]; //도착 지점 x,y좌표
public static int dx[] = {0,0};
public static int dy[] = {-1,1};
public static int dir[]; // 나아갈 방향
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for (int tc = 0; tc < 10; tc++) {
int TC = sc.nextInt();
map = new int[100][100];
visit = new boolean[100][100];
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
map[i][j] = sc.nextInt();
if(map[i][j]==2)
fin= new int[]{i,j};
}
}
dfs(fin[0],fin[1]);
System.out.println("#"+TC+" "+res);
}
}
public static void dfs(int x, int y) {
//종료조건
// 0행인가?
if(x==0) {
res = y;
return;
}
visit[x][y] =true;
dir = new int[] {-1,0}; //위로 올라가는 방향(디폴트)
//양옆 방향 check
for (int i = 0; i < dx.length; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
//범위를 벗어나는가?
if(nx >= 100 || nx < 0 || ny >= 100 || ny < 0)
continue;
//사다리가 없는가?
if(map[nx][ny]==0)
continue;
//방문한 곳인가?
if(visit[nx][ny])
continue;
dir = new int[] {dx[i],dy[i]};
}
dfs(x+dir[0],y+dir[1]);
}
}
728x90
'알고리즘 > DFS & BFS' 카테고리의 다른 글
SWEA - 2819. 격자판의 숫자 이어 붙이기 [D4] (0) | 2020.08.09 |
---|---|
SWEA - 1226. [S/W 문제해결 기본] 7일차 - 미로1 (0) | 2020.08.09 |
프로그래머스[Level2] - 카카오프렌즈 컬러링북 [2017 카카오코드 예선] (0) | 2020.04.04 |
SWEA - 2814. 최장 경로 (1) | 2020.04.04 |
백준 14501번: 퇴사 (0) | 2020.03.21 |