CF1185E.Polycarp and Snakes

普及+/提高

通过率:0%

时间限制:4.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

After a hard-working week Polycarp prefers to have fun. Polycarp's favorite entertainment is drawing snakes. He takes a rectangular checkered sheet of paper of size n×mn \times m (where nn is the number of rows, mm is the number of columns) and starts to draw snakes in cells.

Polycarp draws snakes with lowercase Latin letters. He always draws the first snake with the symbol 'a', the second snake with the symbol 'b', the third snake with the symbol 'c' and so on. All snakes have their own unique symbol. There are only 2626 letters in the Latin alphabet, Polycarp is very tired and he doesn't want to invent new symbols, so the total number of drawn snakes doesn't exceed 2626.

Since by the end of the week Polycarp is very tired, he draws snakes as straight lines without bends. So each snake is positioned either vertically or horizontally. Width of any snake equals 11, i.e. each snake has size either 1×l1 \times l or l×1l \times 1, where ll is snake's length. Note that snakes can't bend.

When Polycarp draws a new snake, he can use already occupied cells for drawing the snake. In this situation, he draws the snake "over the top" and overwrites the previous value in the cell.

Recently when Polycarp was at work he found a checkered sheet of paper with Latin letters. He wants to know if it is possible to get this sheet of paper from an empty sheet by drawing some snakes according to the rules described above. If it is possible, he is interested in a way to draw snakes.

经过一周的辛勤工作后,Polycarp 喜欢放松娱乐。他最喜欢的娱乐活动是画蛇。他取一张大小为 n×mn \times m 的矩形方格纸(其中 nn 表示行数,mm 表示列数),然后在方格中开始画蛇。

Polycarp 使用小写拉丁字母来画蛇。他总是用字符 'a' 画第一条蛇,用 'b' 画第二条蛇,用 'c' 画第三条蛇,依此类推。每条蛇都有其唯一的字符。由于拉丁字母表中仅有 2626 个字母,而 Polycarp 已经非常疲惫,不愿发明新字符,因此所画蛇的总数不超过 2626。

由于周末结束时 Polycarp 极度疲惫,他所画的蛇均为无弯曲的直线段。因此,每条蛇只能是竖直或水平放置。任何蛇的宽度均为 11,即每条蛇的尺寸为 1×l1 \times l 或 l×1l \times 1,其中 ll 是该蛇的长度。注意:蛇不能弯曲。

当 Polycarp 绘制一条新蛇时,他可以复用已被占据的格子来绘制该蛇。此时,他将蛇“覆盖”在原有内容之上,并覆写该格子中原有的字符。

最近,Polycarp 在工作中发现了一张写有拉丁字母的方格纸。他想知道:能否从一张空白方格纸出发,严格按照上述规则画若干条蛇,从而得到这张纸?如果可能,他还希望知道一种具体的画蛇方案。

输入格式

The first line of the input contains one integer tt (1≤t≤1051 \le t \le 10^5) — the number of test cases to solve. Then tt test cases follow.

The first line of the test case description contains two integers nn, mm (1≤n,m≤20001 \le n,m \le 2000) — length and width of the checkered sheet of paper respectively.

Next nn lines of test case description contain mm symbols, which are responsible for the content of the corresponding cell on the sheet. It can be either lowercase Latin letter or symbol dot ('.'), which stands for an empty cell.

It is guaranteed that the total area of all sheets in one test doesn't exceed 4⋅1064\cdot10^6.

输入的第一行包含一个整数 tt(1≤t≤1051 \le t \le 10^5),表示待处理的测试用例数量。随后是 tt 个测试用例。

每个测试用例的第一行包含两个整数 nn 和 mm(1≤n,m≤20001 \le n,m \le 2000),分别表示方格纸的长度与宽度。

接下来的 nn 行,每行包含 mm 个字符,表示方格纸上对应单元格的内容。每个字符要么是一个小写拉丁字母,要么是点号('.'),后者表示该单元格为空。

保证单个测试中所有方格纸的总面积不超过 4⋅1064\cdot10^6。

输出格式

Print the answer for each test case in the input.

In the first line of the output for a test case print YES if it is possible to draw snakes, so that you can get a sheet of paper from the input. If it is impossible, print NO.

If the answer to this question is positive, then print the way to draw snakes in the following format. In the next line print one integer kk (0≤k≤260 \le k \le 26) — number of snakes. Then print kk lines, in each line print four integers r1,ir_{1,i}, c1,ic_{1,i}, r2,ir_{2,i} and c2,ic_{2,i} — coordinates of extreme cells for the ii-th snake (1≤r1,i,r2,i≤n1 \le r_{1,i}, r_{2,i} \le n, 1≤c1,i,c2,i≤m1 \le c_{1,i}, c_{2,i} \le m). Snakes should be printed in order of their drawing. If there are multiple solutions, you are allowed to print any of them.

Note that Polycarp starts drawing of snakes with an empty sheet of paper.

对输入中的每个测试用例,输出其答案。

对于每个测试用例,在输出的第一行中:若能画出蛇形图案,使得最终得到的纸面与输入一致,则输出 YES;否则输出 NO。

若答案为肯定(即输出了 YES),则需按以下格式输出一种可行的画蛇方案。在下一行输出一个整数 kk(0≤k≤260 \le k \le 26),表示蛇的数量。随后输出 kk 行,每行包含四个整数 r1,ir_{1,i}、c1,ic_{1,i}、r2,ir_{2,i} 和 c2,ic_{2,i},分别表示第 ii 条蛇两个端点的坐标(满足 1≤r1,i,r2,i≤n1 \le r_{1,i}, r_{2,i} \le n,1≤c1,i,c2,i≤m1 \le c_{1,i}, c_{2,i} \le m)。蛇必须按照绘制顺序输出。若存在多种可行方案,输出任意一种即可。

注意:Polycarp 始终从一张空白纸开始绘制蛇。

输入输出样例

  • 输入#1

    1
    5 6
    ...a..
    ..bbb.
    ...a..
    .cccc.
    ...a..

    输出#1

    YES
    3
    1 4 5 4
    2 3 2 5
    4 2 4 5
  • 输入#2

    3
    3 3
    ...
    ...
    ...
    4 4
    ..c.
    adda
    bbcb
    ....
    3 5
    ..b..
    aaaaa
    ..b..

    输出#2

    YES
    0
    YES
    4
    2 1 2 4
    3 1 3 4
    1 3 3 3
    2 2 2 3
    NO
  • 输入#3

    2
    3 3
    ...
    .a.
    ...
    2 2
    bb
    cc

    输出#3

    YES
    1
    2 2 2 2
    YES
    3
    1 1 1 2
    1 1 1 2
    2 1 2 2

输入解题思路,AI测评打分。不知道怎么写?

首页