package kn_10_02; import java.util.Scanner; public class Task_2 { static boolean [][] maze; static int eX, eY; static int aX, aY; static int zX, zY; static int cX, cY; static int dX, dY; static Scanner sc = new Scanner(System.in); public static void main(String[] args) { System.out.print("Enter the length of maze: "); int n = sc.nextInt(); maze = new boolean[n][n]; getMaze(); int bX, bY; System.out.print("Ax = "); aX = bX = sc.nextInt(); System.out.print("Ay =: "); aY = bY = sc.nextInt(); System.out.print("Bx = "); zX = sc.nextInt(); System.out.print("By = "); zY = sc.nextInt(); System.out.print("Cx = "); cX = sc.nextInt(); System.out.print("Cy =: "); cY = sc.nextInt(); System.out.print("Dx = "); dX = sc.nextInt(); System.out.print("Dy = "); dY = sc.nextInt(); eX = cX; eY = cY; boolean result = findPath(bX, bY); if(!result){ System.out.println("No"); return; } eX = dX; eY = dY; result = result & findPath(bX, bY); if(!result){ System.out.println("No"); return; } bX = cX; bY = cY; eX = zX; eY = zY; result = result & findPath(bX, bY); if(!result){ System.out.println("No"); return; } bX = dX; bY = dY; result = result & findPath(bX, bY); System.out.println(result); } static boolean findPath(int bX, int bY) { try { if(!isFree(bX, bY)) return false; else if(bX == eX && bY == eY) return true; else { fill(bX, bY); boolean result = findPath(bX-1, bY) || findPath(bX+1, bY) || findPath(bX, bY-1) || findPath(bX, bY+1); undo(bX, bY); return result; } } catch(Exception e){ return false; } } static void getMaze() { for (int i = 0; i<maze.length; i++) for (int j = 0; j<maze.length; j++) maze[i][j] = sc.nextInt()!=0; } static boolean isFree(int x, int y) { return maze[x][y]; } static void fill(int x, int y) { maze[x][y] = false; } static void undo(int x, int y) { maze[x][y] = true; } }