Topic Links:
pku:http://poj.org/problem?id=3923
hdu:http://acm.hdu.edu.cn/showproblem.php?pid=2487
Description
Sheryl works for a software company in the country of Brada. Her job was to develop a Windows operating system. People in Brada is incredibly conservative. They even never use graphical monitors! So Sheryl's operating system has the to run in text mode and Windows on that system is formed by characters. Sheryl decides that every window have an ID which are a capital 中文版 (' a ' to ' Z '). Because every window had a unique ID, there can ' t be more than-windows at the same time. And as you know, all windows is rectangular.
On the screens of that ugly Windows system, a window's frame is formed by its ID letters. Fig-1 shows that there was only one window in the screen, and that window's ID is ' A '. Windows may overlap. Fig-2 shows the situation that Windows B is on the top of window A. And Fig-3 gives a more complicated overlapping. Of course, if some parts of a window is covered by other windows, you can ' t see those parts on the screen.
............................. Aaaaaaaaaaaaa ....... A........... A............ A........... A............ A........... A............ Aaaaaaaaaaaaa ........ ................. Fig-1 ......... ............. Aaaaaaaaaaaaa ....... A........... A............ A....... Bbbbbbbbbb ..... A....... B........ B....... Aaaaaaaab ..... B............... Bbbbbbbbbb ......... ............ Fig-2 ......... .............. Aaaaaaaaaaaaa ........ A........... A............. A....... Bbbbbbbbbb ..... A....... B........ BCCC ..... Aaaaaaaab ..... B.. C........ C.... BBBBBBBBBB. C........ CCCCCCCCCCCCCCCCCC ......... ............ Fig-3
If A window has no parts covered by other windows, we call it a "top window" (The frame is also considered as a part of a window). Usually, the top Windows is the Windows interact with the user most frequently. Assigning top Windows more CPU time and higher the priority would result in better user experiences. Given the screen presented as Figs above, can-tell Sheryl which Windows is top Windows?
Input
The input contains several test cases.
Each test case is begins with a integers, N and m (1 <= N, M <=), indicating that the screens has n lines, and EAC H line consists of M characters.
The following n lines describe the whole screen you see. Each line contains m characters. For characters which is not on any window frame, we just replace them with '. '.
The input ends with a line of zeros.
It's Guaranteed that:
1) There is at least one window in the screen.
2) Any window ' s frame are at least 3 characters wide and 3 characters high.
3) No part of any window was outside the screen.
Output
For each test case, output the IDs of any top windows in a line without blanks and in Alphabet order.
Sample Input
9 26..............................AAAAAAAAAAAAA ....... A........... A............. A....... Bbbbbbbbbb ..... A....... B........ BCCC ..... Aaaaaaaab ..... B.. C........ C.... BBBBBBBBBB. C........ CCCCCCCCCCCCCCCCCC ......... ............ 7 25.............................DDDDDDDDDDDDD ....... D........... D............ D........... D............ D........... D.. Aaa....... Ddddddddddddd. A.a ........ ......... Aaa... 0 0
Sample Output
Bad
Source
Beijing 2008
PS: Note:
7 25
... Bbbbbbbbbbbbbbb .....
... Bdddddddddddddb .....
... Bdaaaaaaaaaaadb .....
... Bda......... ADB .....
... Bdaaaaaaaaaaadb .....
... Bdddddddddddddb .....
... Bbbbbbbbbbbbbbb .....
6 6
aaabbb
A.ab. B
aaabbb
Cccddd
C.cd. D
Cccddd
The code is as follows:
#include <cstdio> #include <cstring>const int maxn = 217;char mp[maxn][maxn];int vis[maxn];int n,m;int ok (int A,int b) {int i, J; for (i = b; i <= m; i++) {if (mp[a][i]!=mp[a][b]) break; } i--; for (j = a+1; J <= N; j + +)//line {for (int k = b; k <= i; k++)//column {if (k==b| | k==i)//Edge not identical {if (Mp[j][k]!=mp[a][b]) return 0; } else if (mp[j][k]!= '. ') Bottom edge {for (int l = b; l <= i; l++) {if (Mp[j][l]!=mp[a][b]) return 0; } return 1; }}} return 0;} int main () {int i,j; while (~SCANF ("%d%d", &n,&m)) {if (n==0 && m = = 0) break; memset (vis,0,sizeof Vis); for (int i = 0; i < n; i++) {scanf ("%s", Mp[i]); } for (int i = 0; i < n; i++) {for (int j = 0; J < m; J + +) {if (mp[i][j]!= '. ') &&vis[mp[i][j]-' A ']==0) {vis[mp[i][j]-' a ']=1; if (OK (i,j)) vis[mp[i][j]-' A ']=2; }}} int flag=1; for (int i = 0; i < i++) {if (vis[i] = = 2) {if (flag) printf ("%c", i+ ' A '); else printf ("%c", i+ ' A '); }} puts (""); }}
POJ 3923 & HDU 2487 Ugly Windows (analog)