Carl's Maze-Solving Algorithm

시간 제한1초메모리 제한2048 MB

요약
격자에서 왼손 법칙으로 움직이는 개미를 시뮬레이션해 목적지에 도달하는지 판정한다.
난이도

쉬움10점 중 3점

유형
시뮬레이션, 구현
정답자
아직 제출이 없습니다

문제

Carl the ant is back! After traveling around some pyramids, Carl has decided to study some algorithms and has invented a novel algorithm for solving grid mazes. It works as follows:

  • Carl starts somewhere in the maze facing to the right and wants to get to a destination square.

  • While Carl is not yet in the destination square.

    • If Carl can turn left by 90 degrees and face an empty square, he will turn left 90 degrees and then move forward by one square.
    • Otherwise, if Carl can move forward by one square, he will do so.
    • Otherwise, he will turn right 90 degrees.

Carl wants to know if this algorithm works. Help him check!

입력

The first line of input contains two integers, rr and cc (1≤r,c≤50)(1 \le r, c \le 50), indicating the size (rows, columns) of the maze. The cell at (1,1)(1,1) is the top left corner of the maze.

The next line of input contains two integers, i_starti\_{start} and j_startj\_{start} (1≤i_start≤r,1≤j_start≤c)(1 \le i\_{start} \le r, 1 \le j\_{start} \le c), the starting location for Carl in row i_starti\_{start}, column j_startj\_{start}.

The next line of input contains two integers, i_endi\_{end} and j_endj\_{end} (1≤i_end≤r,1≤j_end≤c)(1 \le i\_{end} \le r, 1 \le j\_{end} \le c), the desired ending location for Carl in row i_endi\_{end}, column j_endj\_{end}. It is guaranteed the starting location and desired ending location for Carl are different.

Each of the next rr lines contains a string of cc characters, consisting only of 0 or 1. If the character is 1, then that square has an obstacle in it and cannot be traversed, otherwise it is empty. It is guaranteed that Carl’s starting location and desired ending location are empty.

출력

Output a single integer, which is 11 if it is possible for Carl to get from the starting location to the ending location, and 00 otherwise.

예제2

  1. 예제 1

    입력
    4 5
    1 1
    4 5
    00111
    10100
    10111
    10000
    
    예상 출력
    1
    
  2. 예제 2

    입력
    3 3
    1 1
    3 3
    001
    001
    110
    
    예상 출력
    0