1+ import java .util .*;
2+ import java .io .FileInputStream ;
3+
4+
5+ class Solution
6+ {
7+ static int [] dr = {1 ,-1 ,0 ,0 };
8+ static int [] dc = {0 ,0 ,1 ,-1 };
9+
10+ public static void main (String args []) throws Exception
11+ {
12+
13+ Scanner sc = new Scanner (System .in );
14+
15+
16+ for (int test_case = 1 ; test_case <= 10 ; test_case ++)
17+ {
18+ int T ;
19+ T =sc .nextInt ();
20+
21+ // 길 0 벽 1 시작점 2 도착점 3
22+
23+ // 도달 가능 하면 1 불가능하면 0 출력
24+ int result = 0 ;
25+
26+ char [][] map = new char [16 ][16 ];
27+
28+ for (int i =0 ; i <16 ; i ++) {
29+ map [i ] = sc .next ().toCharArray ();
30+ }
31+
32+
33+ result = bfs (map );
34+
35+
36+ System .out .println ("#" + test_case + " " + result );
37+ }
38+
39+ sc .close ();
40+ }
41+
42+ public static int bfs (char [][] map ) {
43+
44+ Queue <int []> q = new LinkedList <>();
45+ boolean [][] isVisited = new boolean [16 ][16 ];
46+ int targetRow = -1 ;
47+ int targetCol = -1 ;
48+
49+ for (int i =0 ; i <16 ; i ++) {
50+ for (int j =0 ; j <16 ; j ++) {
51+ if (map [i ][j ] == '2' ) {
52+ q .add (new int [] {i ,j });
53+ isVisited [i ][j ] = true ;
54+ }
55+ if (map [i ][j ] == '3' ) {
56+ targetRow = i ;
57+ targetCol = j ;
58+ }
59+ }
60+ }
61+
62+ while (!q .isEmpty ()) {
63+ int [] cur = q .poll ();
64+ int row = cur [0 ];
65+ int col = cur [1 ];
66+
67+ if (row == targetRow && col == targetCol ) return 1 ;
68+
69+ for (int i =0 ; i <4 ; i ++) {
70+
71+ int nr = row + dr [i ];
72+ int nc = col + dc [i ];
73+
74+ if (nr >= 0 && nr < 16 && nc >=0 && nc <16
75+ && map [nr ][nc ] != '1'
76+ && !isVisited [nr ][nc ]) {
77+ q .add (new int [] {nr , nc });
78+ isVisited [nr ][nc ] = true ;
79+ }
80+ }
81+ }
82+
83+
84+
85+ return 0 ;
86+ }
87+
88+ }
0 commit comments