Ugly Windows HDU2487 ACM算法设计

Ugly Windows

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1104    Accepted Submission(s): 306

Problem 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.
 

 

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
 

 

Source
 

 

Recommend
gaojie
 

模拟题

#include <iostream>
#include <string>
#include <algorithm>
//#include <fstream>
using namespace std;

const int MAXN = 110;
string matrix[MAXN];
string ans;
int n,m;

int main()
{
    //ifstream cin("in.txt");
    while (cin>>n>>m,m)
    {        
        ans.clear();
        int point = 0;
        //Input
        for(int i = 0;i < n;i++)
            cin>>matrix[i];
        //找到每个窗口的左上角的点
        for (int i = 0;i < n-1;i ++) for(int j = 0;j < m - 1;j ++)
        {
            //find it
            if('.' != matrix[i][j] && matrix[i][j] == matrix[i+1][j] && matrix[i][j] == matrix[i][j+1])
            {
                int k;
                //去找右上角的点
                int ru = 0;
                for(k = j + 2;k < m;k++)
                {
                    if(matrix[i][k] != matrix[i][j])
                        break;
                    else if(matrix[i+1][k] == matrix[i][j])
                    {
                        ru = k;
                        break;
                    }
                    else if(matrix[i+1][k] == '.')
                        continue;
                    else break;
                }
                if(!ru)
                    continue;
                //找左下角的点
                int ld = 0;
                for(k = i + 2;k < n;k ++)
                {
                    if(matrix[k][j] != matrix[i][j])
                        break;
                    else if(matrix[k][j+1] == matrix[i][j])
                    {
                        ld = k;
                        break;
                    }
                    else if(matrix[k][j+1] == '.')
                        continue;
                    else break;
                }
                if(!ld)
                    continue;
                //检测下面的边
                int flag = 0;
                for(k = j + 2;k <= ru;k ++) if(matrix[ld][k] != matrix[i][j])
                {
                    flag = 1;
                    break;                    
                }
                if(flag)
                    continue;
                //检测右边
                for(k = i + 2;k < ld;k ++) if(matrix[k][ru] != matrix[i][j])
                {
                    flag = 1;
                    break;
                }
                if(flag)
                    continue;
                //检测当前窗口是否嵌套了窗口
                for(k = i + 2;k < ld;k ++) for(int t = j + 2;t < ru; t ++) if(matrix[k][t] != '.')
                {
                    flag = 1;
                    break;
                }
                if(flag)
                    continue;
                else 
                    ans.push_back(matrix[i][j]);
            }
        }
        sort(ans.begin(),ans.end());
        cout<<ans<<endl;
    }
    return 0;
}

 

posted @ 2013-04-23 20:14  iFinVer  阅读(278)  评论(0编辑  收藏  举报