首页 > ACM题库 > HDU-杭电 > hdu 2487 Ugly Windows-搜索-[解题报告]C++
2014
01-26

hdu 2487 Ugly Windows-搜索-[解题报告]C++

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?

输入:

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.

输出:

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.

样例输入:

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

样例输出:

B
AD

题目: http://acm.hdu.edu.cn/showproblem.php?pid=2487

题目大意:

用一个大写字母表示一个窗口的ID,每个窗口至少要有3*3,给你一个桌面,要你输出位于最上层的窗口的ID。

解题思路:

先找到一个矩形窗口的左上角的点,记为(x, y)。然后向右搜索,遇到拐点,再向下找,又遇到拐点,记下这个点的坐标(tx, ty),再向左找,遇到拐点,向上找,遇到拐点,停止,记下这个点的坐标(dx, dy)。

判断 起点 和 终点 是否一样;

判断窗口大小是否小于3*3;

判断该窗口里面是否还套有其他窗口。

AC代码:

#include <iostream>
using namespace std;

char w[120][120];
int n, m;

bool Isok(int x, int y)
{
if (x < 0 || x >= n || y < 0 || y >= m)
   return false;
else
   return true;
}

bool find(int x, int y, char c)
{
int tx, ty, dx, dy, i, j;
for (j = y + 1;Isok(x, j); ++j)
   if (w[x][j] != c)
    break;
ty = j – 1;

for (i = x + 1; Isok(i, ty); ++i)
   if (w[i][ty] != c)
    break;
tx = i – 1;

for (j = ty; Isok(tx, j); –j)
   if (w[tx][j] != c)
    break;
dy = j + 1;

for (i = tx; Isok(i, dy); –i)
   if (w[i][dy] != c)
    break;
dx = i + 1;

if (x != dx || y != dy)//起点与终点不同  
   return false;

if (tx – x <= 1 || ty – y <= 1)//窗口小于3*3
   return false;

bool flag = true;

for (i = x + 1; i < tx && flag; ++i)
   for (j = y + 1; j < ty && flag; ++j)
    if (w[i][j] != ‘.’)//窗口内套了其他窗口
    {
     flag = false;
     break;
    }

if (flag)
   return true;
else
   return false;
}

int main()
{
while (1)
{
   scanf("%d%d", &n, &m);
   if (n == 0 && m == 0)
    break;

   for (int i = 0; i < n; ++i)
    scanf("%s", w[i]);

   int ans[30] = {0};

   for (int i = 0; i < n; ++i)
   {
    for (int j = 0; j < m; ++j)
    {
     if (w[i][j] != ‘.’ && ans[w[i][j] – ‘A’] == 0)//找一个窗口的左上角的坐标
     {
      ans[w[i][j] – ‘A’] = -1;
     
      if (find(i, j, w[i][j]))
       ans[w[i][j] – ‘A’] = 1;    
     }
    }
   }

   for (int i = 0; i < 26; ++i)
   {
    if (ans[i] == 1)
     printf("%c", (char)(i + ‘A’));
   }
   printf("\n");
}
return 0;
}

解题转自:http://hi.baidu.com/yato__/item/7807f5ba20d782ea4ec7fd69


  1. 约瑟夫也用说这么长……很成熟的一个问题了,分治的方法解起来o(n)就可以了,有兴趣可以看看具体数学的第一章,关于约瑟夫问题推导出了一系列的结论,很漂亮