题目链接: 点击打开链接
题目大意: 输入几个窗口,判断在最顶上的窗口是哪些
思路: 字符串遍历
分析:
思路应该没问题.poj上是AC了,但是uva上提交了半天都是wa,上网找了几个代码交上去也是wa...从uva上找了几道之前AC的题,原本可以的代码也都wa了...是不是出了什么问题?
1. 第一步先把不可能在顶端的窗口过滤掉.观察在边界上的字母,如果没被覆盖,周围一定有两个相同字母,找不到说明有窗口overlap了...
2. 还有一种可能是一个小窗口完全叠加在一个大窗口上.这种情况下只要扫描大窗口内部,发现一个不是'.'的字母就可以排除了.
代码:
#include <cstdio>
#include <memory.h>
using namespace std;
const int maxn = 100 + 10;
char ui[maxn][maxn];
int n, m;
bool possible[27], input[27];
struct area
{
int x1, y1, x2, y2;
} areas[27];
bool inside(int x, int y)
{
return 0 <= x && x < n && 0 <= y && y < m;
}
bool is_cutted(int x, int y)
{
char c = ui[x][y];
int total = 0;
if (inside(x-1, y) && ui[x-1][y] == c)
++total;
if (inside(x+1, y) && ui[x+1][y] == c)
++total;
if (inside(x, y-1) && ui[x][y-1] == c)
++total;
if (inside(x, y+1) && ui[x][y+1] == c)
++total;
return total < 2;
}
bool is_upper_left(int x, int y)
{
char c = ui[x][y];
return inside(x, y+1) && ui[x][y+1] == c &&
inside(x+1, y) && ui[x+1][y] == c;
}
bool is_lower_right(int x, int y)
{
char c = ui[x][y];
return inside(x, y-1) && ui[x][y-1] == c &&
inside(x-1, y) && ui[x-1][y] == c;
}
void init()
{
for (int i = 0; i < n; ++i)
scanf("%s", ui[i]);
//for (int j = 0; j < m; ++j) {
// scanf("%c", &ui[i][j]);
// printf("%c", ui[i][j]);
//}
memset(possible, 1, sizeof(possible));
memset(input, 0, sizeof(input));
}
void find_cutted()
{
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
char c = ui[i][j];
if (c!= '.')
{
input[c - 'A'] = true;
if (possible[c - 'A']) {
if (is_upper_left(i, j)) {
areas[c - 'A'].x1 = i;
areas[c - 'A'].y1 = j;
}
else if (is_lower_right(i, j)) {
areas[c - 'A'].x2 = i;
areas[c - 'A'].y2 = j;
}
if (is_cutted(i, j))
possible[c - 'A'] = false;
}
}
}
}
bool scan_inside(int p)
{
char c = p + 'A';
int x1 = areas[p].x1, y1 = areas[p].y1, x2 = areas[p].x2, y2 = areas[p].y2;
for (int i = x1 + 1; i < x2; ++i)
for (int j = y1 + 1; j < y2; ++j)
if (ui[i][j] != '.')
return false;
return true;
}
void solve()
{
for (int i = 0; i < 26; ++i)
if (input[i] && possible[i] && scan_inside(i))
printf("%c", i + 'A');
printf("\n");
}
int main()
{
while (scanf("%d %d", &n, &m) && n != 0 &&& m != 0)
{
init();
find_cutted();
solve();
}
return 0;
}
本文探讨并实现了一个用于筛选窗口顶端元素的算法,详细解释了思路与步骤,并通过代码实例展示了具体应用。
505

被折叠的 条评论
为什么被折叠?



