Fork Copy package ngay6; import java.util.Scanner; public class bai2 { static int a[][] = new int [16][16]; static boolean visited[][] = new boolean[16][16]; static int[] dx = {0, 0, 1, -1}; static int[] dy = {1, -1, 0, 0}; static boolean dfs(int x, int y) { int[][] stack = new int[16*2][2]; int top = -1; stack[++top][0] = x; stack[top][1] = y; visited[x][y] = true; while(top >= 0) { int cx = stack[top][0]; int cy = stack[top][1]; top--; for(int i = 0; i < 4; i++) { int nx = cx + dx[i]; int ny = cy + dy[i]; if(nx >= 0 && nx < 16 && ny >= 0 && ny < 16 && !visited[nx][ny] && a[nx][ny] != 1){ if(a[nx][ny] == 3) return true; visited[nx][ny] = true; stack[++top][0] = nx; stack[top][1] = ny; } } } return false; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); for(int t = 1; t <= 10; t++){ int sad = Integer.parseInt(sc.nextLine()); for(int i = 0; i < 16; i++) { String s = sc.nextLine(); for(int j = 0; j < 16; j++) { a[i][j] = s.charAt(j) - '0'; } } int sx = -1, sy = -1; int ex = -1, ey = -1; for(int i = 0; i < 16; i++) { for(int j = 0; j < 16; j++) { if(a[i][j] == 2){ sx = i; sy = j; break; } } } for(int i = 0; i < 16; i++) { for(int j = 0; j < 16; j++) { if(a[i][j] == 3){ ex = i; ey = j; break; } } } if(sx == -1 || sy == -1 || ex == -1 || ey == -1) { System.out.println("#" + t + " " + 0); }else{ boolean result = dfs(sx, sy); System.out.println("#" + t + " " +(result ? 1 : 0)); } } } }