Jungle Trail

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

요약
각 행과 열을 최대 한 번씩 탭해 뱀의 독 상태를 바꾸고, 독이 있는 뱀과 막힌 칸을 피해 왼쪽 위에서 오른쪽 아래로 오른쪽/아래 이동 경로를 찾는다.
난이도

어려움10점 중 8점

유형
그리디, 구현, 동적 계획법, 시뮬레이션
정답자
아직 제출이 없습니다

문제

In the mobile game "Jungle Trail", you are given a rectangular n×mn \times m board divided into n⋅mn \cdot m squares. Each square is either empty, blocked (impassable) or contains a den of snakes, either poisonous or benign (not poisonous). If a square contains a den of snakes, then either all the snakes on a given field are poisonous, or all are benign.

The game allows you to tap any column or any row of the board. If you tap a column, all poisonous snakes in this column are turned to benign, and vice versa. Similarly, if you tap any row, all snakes in the row change their state. You can tap each row/column only once. If a den is in a tapped row as well as in a tapped column, its state returns to the original one.

After performing all those operations, you must find a trail through the jungle: a path which starts at the top left corner, in every move goes either one square down or one to the right, ends at the bottom right corner and never passes through a den of poisonous snakes or a blocked field.

입력

The first line of input contains the number of test cases zz (1≤z≤5001 \le z \le 500). The descriptions of the test cases follow.

The first line contains two integers nn and mm (2≤n,m≤2,0002 \le n, m \le 2\\,000).

Each of the following nn lines contains mm characters ., #, O (capital o) and @ (at sign), meaning an empty square, blocked square, den of benign snakes and den of poisonous snakes, respectively. You may assume that the top left corner and the bottom right corner are not blocked.

Neither the sum of nn values over all test cases nor the sum of all mm values exceed 15,00015\\,000.

출력

For every test case output the solution in the following format:

The first line should contain TAK if a jungle trail is possible or NIE if it isn't.

If the answer is TAK, in the next three lines output:

  • A sequence of nn characters TT or NN, the ii-th character being TT if the ii-th row should be tapped, NN if not;
  • A sequence of mm characters TT or NN, determining in the same way whether the columns should be tapped;
  • A sequence of n+m−2n + m - 2 characters PP or DD denoting the trail: PP means a move right, DD means a move down.

힌트

After tapping the rows and columns described on the output, the board is in the following state:

..#..
OOOO@
##O#@
..O.O

Now the given path goes only through . and O squares.

예제1

  1. 예제 1

    입력
    1
    4 5
    ..#..
    @@O@@
    ##@#O
    ..@.@
    
    예상 출력
    TAK
    NTNN
    NNTNT
    DPPDDPP