Zlagalica

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

요약
연결 규칙에 따라 n개의 색칠된 직사각형을 순서대로 배치한 뒤 경계 상자의 크기와 결과 격자를 출력한다.
난이도

보통10점 중 5점

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

문제

Little Maja has always loved puzzles. And since everyone knew that for a long time now, it is no wonder that one sunny day, Maja received an odd puzzle as a gift..

This puzzle has nn pieces. Each piece has rectangular shape and is of a certain color. Also, each piece has 22 numbers written on its back: uu and dd. After a period of skillfully combining pieces and trying to fit them together, Maja figured out the meaning of those numbers.

She found out that number uu represents "direction", in other words, does the next piece of the puzzle connect with the current one from the upper or from the right side of the current piece. Number dd specifies the starting column/row where we connect the next piece of the puzzle with current one. In more detail:

  • If uu is equal to 00, we add next piece above the current one by connecting its bottom left corner with current piece’s top edge at column d.
  • If uu is equal to 11, we add next piece to the right by connecting its bottom left corner with current piece’s right edge at row d.

Let’s demonstrate this for pieces colored in colors "a" and "b". Picture 1 shows the case where u=0u = 0, and d=3d = 3. Picture 2 shows case when u=1u = 1 and d=3d = 3. (In both cases, numbers uu and dd represent numbers written on the back of piece colored "a").

```
. . b b b b b
. . b b b b b
a a a a . . .
a a a a . . .
a a a a . . .
``````
a a a a . . . . .
a a a a b b b b b
a a a a b b b b b
| Picture 1                                                                     | Picture 2                                                     |

Maja has grown tired of this puzzling puzzle, but her curiosity knows no bounds! That’s why she’s asking for your help. She’s interested in knowing, for a given description of every piece of the puzzle and the sequence of their placement, what will the completed puzzle look like? Write a program that prints the dimensions (height and width) of the completed puzzle, as well as its final appearance within a rectangle of the same height and width, where "." represents places where there is no part of the puzzle.

입력

In first row, there is nn (1≤n≤201 ≤ n ≤ 20), number of puzzle pieces.

In the ii-th of next nn rows there are per 11 character and 44 integers, in order: b_ib\_i, r_ir\_i, s_is\_i, u_iu\_i, d_id\_i - description of ii-th piece:

  • b_ib\_i will always be 11 lowercase letter of english alphabet, and it represents the color of the ii-th puzzle piece
  • r_ir\_i and s_is\_i (1≤r_i,s_i≤101 ≤ r\_i , s\_i ≤ 10) represent in order, number of rows and columns of ii-th puzzle piece
  • u_iu\_i and d_id\_i (0≤u_i≤10 ≤ u\_i ≤ 1, 1≤d_i≤r_i,s_i1 ≤ d\_i ≤ r\_i , s\_i (depends on u_iu\_i)) are numbers on the back of ii-th puzzle piece, same as in the task statement.

In the last row of input there are nn integers, order in which pieces are connected, where number ii (1≤i≤n1 ≤ i ≤ n) represents ii-th puzzle piece in input. Each puzzle piece will appear in the sequence exactly once.

출력

Print the height and width of the completed puzzle. After that, print the appearance of the puzzle within a rectangle of the same height and width. In the places within the rectangle where there is no part of the puzzle, print ".".

예제3

  1. 예제 1

    입력
    2
    a 3 4 0 3
    b 2 5 1 1
    1 2
    
    예상 출력
    5 7
    ..bbbbb
    ..bbbbb
    aaaa...
    aaaa...
    aaaa...
    
  2. 예제 2

    입력
    2
    a 3 4 0 3
    b 2 5 1 1
    2 1
    
    예상 출력
    4 9
    .....aaaa
    .....aaaa
    bbbbbaaaa
    bbbbb....
    
  3. 예제 3

    입력
    4
    g 9 5 0 2
    a 3 2 1 1
    c 5 10 0 2
    p 8 7 1 6
    4 3 2 1
    
    예상 출력
    18 17
    ..........ggggg..
    ..........ggggg..
    ..........ggggg..
    ..........ggggg..
    ..........ggggg..
    ..........ggggg..
    ..........ggggg..
    ..........ggggg..
    ........aaggggg..
    ........aa.......
    ppppppp.aa.......
    pppppppcccccccccc
    pppppppcccccccccc
    pppppppcccccccccc
    pppppppcccccccccc
    pppppppcccccccccc
    ppppppp..........
    ppppppp..........