Superknight

면접 대비

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

요약
막힌 칸이 있는 격자에서 최대 한 번 긴 슈퍼 이동을 허용해 최소 이동으로 목적지에 도달하는 경로를 출력한다.
난이도

보통10점 중 6점

유형
BFS, 그래프, 최단 경로, 구현
정답자
아직 제출이 없습니다

문제

A superknight is located on some square of a chessboard and wants to move to another square, and do so in the least possible number of moves. It is hindered by the fact that some squares are blocked and the knight is not allowed to visit them on its path.

A regular knight can move either by two rows and one column or by two columns and one row in each move (the leftmost figure). A knight can jump over blocked squares, only landing on them is forbidden (the middle figure).

A superknight differs from a regular one by its ability to perform a supermove on its path (but at most once), in which it moves either by three rows and one column or by three columns and one row (the rightmost figure).

Write a program to find the path of a superknight from a given starting square to a given destination square with the least possible number of moves.

입력

The first line of input contains RR, the number of rows, and VV, the number of columns of the board (3≤R≤1003 \le R \le 100, 3≤V≤1003 \le V \le 100). Each of the following RR lines contains exactly VV characters, where '@' denotes the starting square of the knight, '*' the destination square, '.' a free square, and '#' a blocked square.

출력

The first line of output should contain KK, the minimal number of moves needed to reach the destination square. Each of the following K+1K + 1 lines should contain two integers r_ir\_i and v_iv\_i: the row and column numbers of the squares that the knight visits on its path, in the order they are visited. The rows of the board are numbered 1…R1 \ldots R from top to bottom and the columns 1…V1 \ldots V from left to right. You may assume that the knight can reach the destination square in all test cases. If there are several paths with the minimal number of moves, output any one of them.

예제1

  1. 예제 1

    입력
    4 5
    *....
    .....
    ..#..
    @....
    
    예상 출력
    4
    4 1
    2 2
    4 3
    2 4
    1 1