open System let () = let h, w = stdin.ReadLine () |> (fun l -> l.Split ()) |> (fun arr -> (int arr.[0], int arr.[1])) let sx, sy, gx, gy = stdin.ReadLine () |> (fun l -> l.Split ()) |> (fun arr -> (int arr.[0], int arr.[1], int arr.[2], int arr.[3])) let mapArr = Array2D.zeroCreateBased 1 1 w h let readB (x, line:string) = seq {1..w} |> Seq.iter (fun y -> mapArr.[y, x] <- int line.[y - 1] - int '0') seq { for x in 1..h -> (x, stdin.ReadLine ()) } |> Seq.iter readB let searchPath startY startX = let dxy = [(1, 0); (-1, 0); (0, 1); (0, -1)] let canMove1 (cy, cx) (dy, dx) = let y, x = cy + dy, cx + dx y >= 1 && y <= w && x >= 1 && x <= h && abs(mapArr.[y, x] - mapArr.[cy, cx]) <= 1 let canMove2 (cy, cx) (dy, dx) = let y1, y2 = cy + dy, cy + dy * 2 let x1, x2 = cx + dx, cx + dx * 2 y2 >= 1 && y2 <= w && x2 >= 1 && x2 <= h && mapArr.[cy, cx] = mapArr.[y2, x2] && mapArr.[cy, cx] > mapArr.[y1, x1] let rec searchPath' lst chkedSet = match lst with | [] -> false | pos::rest -> let cy, cx = pos let cand1 = List.filter (canMove1 (cy, cx)) dxy let cand2 = List.filter (canMove2 (cy, cx)) dxy |> List.map (fun (dy, dx) -> (dy * 2, dx * 2)) let cand = List.append cand1 cand2 let candLst = List.map (fun (dy, dx) -> (cy + dy, cx + dx)) cand if List.contains (gy, gx) candLst then true else let nextLst = List.append candLst rest |> List.filter (fun pos -> not (Set.contains pos chkedSet)) |> List.sortBy (fun (y, x) -> abs(gy - y) + abs(gx - x)) let nextChkedSet = Set.add (cy, cx) chkedSet searchPath' nextLst nextChkedSet if startY = gy && startX = gx then true else searchPath' [(startY, startX)] Set.empty searchPath sy sx |> (function | true -> "YES" | false -> "NO") |> printfn "%s"