[UVA] 1419 - Ugly Windows
Sheryl works for a software company in the country of Brada. Her job is to develop a Windows operating system. People in Brada are incredibly conservative. They even never use graphical monitors! So Sheryl's operating system has to run in text mode and windows in that system are formed by characters. Sheryl decides that every window has an ID which is a capital English letter (`A' to `Z'). Because every window had a unique ID, there can't be more than 26 windows at the same time. And as you know, all windows are rectangular.
On the screen of that ugly Windows system, a window's frame is formed by its ID letters. Fig-1 shows that there is only one window on the screen, and that window's ID is `A'. Windows may overlap. Fig-2 shows the situation that window B is on the top of window A. And Fig-3 gives a more complicated overlapping. Of course, if some parts of a window are 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 are the windows that interact with user most frequently.
Assigning top windows more CPU time and higher priority will result in
better user experiences. Given the screen presented as Figs above, can
you tell Sheryl which windows are top windows?
Input
The input contains several test cases.
Each test case begins with two integers, n and m (1n, m100) , indicating that the screen has n lines, and each line consists of m characters.
The following n lines describe the whole screen you see. Each line contains m characters. For characters which are not on any window frame, we just replace them with `.' .
The input ends with a line of two zeros.
It is guaranteed that:
- 1)
- There is at least one window on the screen.
- 2)
- Any window's frame is at least 3 characters wide and 3 characters high.
- 3)
- No part of any window is outside the screen.
Output
For each test case, output the IDs of all 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
B AD
抓左上角點,然後把點點都拿出來判斷周圍有沒有異物。
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
char g[105][105];
int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
int n, m, i, j, p;
int check(int x, int y, int c) {
int tx, ty, i;
for(i = 0; i < 4; i++) {
tx = x+dir[i][0], ty = y+dir[i][1];
if(tx < 0 || ty < 0 || tx >= n || ty >= m)
continue;
if(g[tx][ty] != '.' && g[tx][ty] != c)
return 0;
}
return 1;
}
int main() {
while(scanf("%d %d", &n, &m) == 2) {
if(n == 0) break;
memset(g, 0, sizeof(g));
for(i = 0; i < n; i++)
scanf("%s", g[i]);
int alph[26] = {}, x, y, tx, ty;
for(i = 0; i < n; i++) {
for(j = 0; j < m; j++) {
if(g[i][j] >= 'A' && g[i][j] <= 'Z'
&& g[i][j] == g[i][j+1] && g[i][j] == g[i+1][j]) { // left upper corner
if(g[i+1][j+1] == '.') {
queue<int> X, Y;
X.push(i+1), Y.push(j+1);
int flag = 1; // true
char used[105][105] = {};
while(!X.empty() && flag) {
x = X.front(), X.pop();
y = Y.front(), Y.pop();
flag &= check(x, y, g[i][j]);
for(p = 0; p < 4; p++) {
tx = x+dir[p][0], ty = y+dir[p][1];
if(tx < 0 || ty < 0 || tx >= n || ty >= m)
continue;
if(used[tx][ty] || g[tx][ty] != '.')
continue;
used[tx][ty] = 1;
X.push(tx), Y.push(ty);
}
}
if(flag) alph[g[i][j]-'A'] = 1;
}
}
}
}
for(i = 0; i < 26; i++)
if(alph[i])
putchar(i+'A');
puts("");
}
return 0;
}