HDU 2487 Ugly Windows(暴力)(2008 Asia Regional Beijing)

 

Description

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 (1 <= n, m <= 100), 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.  

 

题目大意:给一个n*m的矩阵,每一个字母代表一个窗口的边框,问那些窗口在最顶端(这题太难描述了不管了……)

思路:这题数据范围很小,就不用那些很难写但速度可能比较快的算法了,因为是水题写得快和准更重要。首先枚举每一个字母,找出每一个字母最大和最小的x、y坐标,检查一下这个字母的窗口边框是不是都是那个字母,再检查一下这个窗口里面有没有被其他奇怪的东西覆盖(就是都要是'.'),就能AC了。

PS:把一个max_y打成了max_x又WA了一个>_<

 

代码(0MS):

 1 #include <cstdio>

 2 #include <iostream>

 3 #include <algorithm>

 4 #include <cstring>

 5 using namespace std;

 6 

 7 const int MAX = 110;

 8 

 9 char mat[MAX][MAX];

10 int n, m;

11 

12 bool top(char c) {

13     int max_x = 0, min_x = MAX, max_y = 0, min_y = MAX;

14     for(int i = 1; i <= n; ++i) {

15         for(int j = 1; j <= m; ++j) {

16             if(mat[i][j] != c) continue;

17             max_x = max(max_x, j);

18             min_x = min(min_x, j);

19             max_y = max(max_y, i);

20             min_y = min(min_y, i);

21         }

22     }

23     if(max_x - min_x < 2 || max_y - min_y < 2) return false;

24     for(int i = min_y; i <= max_y; ++i)

25         if(mat[i][max_x] != c || mat[i][min_x] != c) return false;

26     for(int i = min_x; i <= max_x; ++i)

27         if(mat[max_y][i] != c || mat[min_y][i] != c) return false;

28     for(int i = min_y + 1; i < max_y; ++i) {

29         for(int j = min_x + 1; j < max_x; ++j)

30             if(mat[i][j] != '.') return false;

31     }

32     return true;

33 }

34 

35 int main() {

36     while(scanf("%d%d", &n, &m) != EOF) {

37         if(n == 0 && m == 0) break;

38         for(int i = 1; i <= n; ++i) scanf("%s", &mat[i][1]);

39         for(char c = 'A'; c <= 'Z'; ++c)

40             if(top(c)) printf("%c", c);

41         puts("");

42     }

43 }
View Code

 

你可能感兴趣的:(windows)