题目大意
给定一个长为N,宽为M的屏幕,屏幕上有一些窗口,窗口之间有一些重叠,求出在最顶端的窗口。
题解
就是一个很简单的模拟,不过有一个坑爹的地方,那就是窗口的嵌套,例如这样的数据
5 5
AAAAA
ABBBA
AB. BA
ABBBA
AAAAA
答案是B,而我刚开始写的代码会输出AB,因为我只是判断是否为完整的矩形。即如果是最顶端的窗口,那么它的内部应该全部是'.'。
#include<stdio.h> #include<stdlib.h> #include<string.h> #define MAXN 105 int visit[MAXN][MAXN]; int p[30]; char map[MAXN][MAXN]; int n,m; void solve() { int i,j,a,b,c,d,x,y,lx,ly,rx,ry,flag; memset(visit,0,sizeof(visit)); memset(p,0,sizeof(p)); for(i=0; i<n; i++) for(j=0; j<m; j++) if((map[i][j]!='.')&&(!visit[i][j])) { a=1; b=0; c=0; d=0; visit[i][j]=1; lx=i; ly=j; x=i; y=j+1; while((y<m)&&(map[x][y]==map[i][j])&&!visit[x][y]) { a++; visit[x][y]=1; y++; } x=i+1; y--; ry=y; while((y>=0)&&(x<n)&&(map[x][y]==map[i][j])&&!visit[x][y]) { b++; visit[x][y]=1; x++; } x=i+1; y=j; while((x<n)&&(map[x][y]==map[i][j])&&!visit[x][y]) { c++; visit[x][y]=1; x++; } x--; y=j+1; rx=x; while((x>0)&&(y<m)&&(map[x][y]==map[i][j])&&!visit[x][y]) { d++; visit[x][y]=1; y++; } if((a==(d+2))&&(b==c)&&a>2&&b>1) { flag=1; for(x=lx+1;x<rx;x++) { for(y=ly+1;y<ry;y++) if(map[x][y]!='.') { flag=0; break; } if(!flag) break; } if(flag) p[map[i][j]-'A']=1; } } } void out() { int i; for(i=0; i<26; i++) if(p[i]) printf("%c",'A'+i); printf("\n"); } int main(void) { int i; while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) break; for(i=0; i<n; i++) scanf("%s",map[i]); solve(); out(); } return 0; }