728x90
300x250
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Solution {
public static int[][] map;
public static boolean[][] visit;
public static int[] start;
public static int[] dx = { -1, 0, 1, 0 };
public static int[] dy = { 0, 1, 0, -1 };
public static int res;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for (int T = 0; T < 10; T++) {
int tc = sc.nextInt();
map = new int[16][16];
visit = new boolean[16][16];
res = 0;
for (int i = 0; i < 16; i++) {
String str = sc.next();
for (int j = 0; j < 16; j++) {
map[i][j] = Integer.parseInt(String.valueOf(str.charAt(j)));
if (map[i][j] == 2)
start = new int[] { i, j };
}
}
visit[start[0]][start[1]]=true;
dfs(start[0], start[1]);
System.out.println("#"+tc+" "+res);
}
}
public static void dfs(int x, int y) {
for (int i = 0; i < dx.length; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
// 범위 벗어나는가?
if (nx >= 16 || nx < 0 || ny >= 16 || ny < 0)
continue;
//이미 들린 곳인가?
if (visit[nx][ny])
continue;
// 길이 아닌가?
if (map[nx][ny] == 1)
continue;
// 도착지점인가?
if(map[nx][ny]==3) {
res = 1;
return;
}
visit[nx][ny] = true;
dfs(nx,ny);
}
}
}
기본에 충실하자
728x90
'알고리즘 > DFS & BFS' 카테고리의 다른 글
백준 17143번: 낚시왕 (0) | 2020.08.10 |
---|---|
SWEA - 2819. 격자판의 숫자 이어 붙이기 [D4] (0) | 2020.08.09 |
SWEA - 1210. [S/W 문제해결 기본] 2일차 - Ladder1 (0) | 2020.08.09 |
프로그래머스[Level2] - 카카오프렌즈 컬러링북 [2017 카카오코드 예선] (0) | 2020.04.04 |
SWEA - 2814. 최장 경로 (1) | 2020.04.04 |