题解:
暴力出奇迹
先枚举每一个矩阵,然后一个个进行判断
求出每个数字的联通块,之后判断一下有没有被包含
#include
#include
#include
using namespace std;
struct node
{
int x1,x2,y1,y2;
}a[50005];
int n,i1,i2,j1,j2,num,ans;
int map[25][25],c[10005],color[26];
bool b[25][25];
char ch;
void dg(int x,int i,int j,int i1,int j1,int i2,int j2)
{
b[i][j]=true;
if (i<i2&&b[i+1][j]==false&&map[i+1][j]==x) dg(x,i+1,j,i1,j1,i2,j2);
if (j<j2&&b[i][j+1]==false&&map[i][j+1]==x) dg(x,i,j+1,i1,j1,i2,j2);
if (i>i1&&b[i-1][j]==false&&map[i-1][j]==x) dg(x,i-1,j,i1,j1,i2,j2);
if (j>j1&&b[i][j-1]==false&&map[i][j-1]==x) dg(x,i,j-1,i1,j1,i2,j2);
}
bool pd(int xx1,int yy1,int xx2,int yy2)
{
int i,j,sum;
sum=0;
memset(b,false,sizeof(b));
memset(color,0,sizeof(color));
memset(c,0,sizeof(c));
for (i=xx1;i<=xx2;i++)
for (j=yy1;j<=yy2;j++)
if (b[i][j]==false)
{
if (color[map[i][j]]==0)
{
sum++;
c[sum]=map[i][j];
}
if (sum>2) return false;
color[map[i][j]]++;
dg(map[i][j],i,j,xx1,yy1,xx2,yy2);
}
if ((color[c[1]]==1&&color[c[2]]>1)||(color[c[2]]==1&&color[c[1]]>1)) return true;
return false;
}
bool judge(int x)
{
int i;
for (i=1;i<=num;i++)
if (i!=x&&a[i].x1<=a[x].x1&&a[i].y1<=a[x].y1&&a[i].x2>=a[x].x2&&a[i].y2>=a[x].y2) return false;
return true;
}
int main()
{
freopen("where.in","r",stdin);
freopen("where.out","w",stdout);
scanf("%d",&n);
for (i1=1;i1<=n;i1++)
for (j1=1;j1<=n;j1++)
{
cin>>ch;
map[i1][j1]=ch-'A';
}
for (i1=1;i1<=n;i1++)
for (i2=i1;i2<=n;i2++)
for (j1=1;j1<=n;j1++)
for (j2=j1;j2<=n;j2++)
if (pd(i1,j1,i2,j2)==true)
{
num++;
a[num].x1=i1;
a[num].y1=j1;
a[num].x2=i2;
a[num].y2=j2;
}
for (i1=1;i1<=num;i1++)
if (judge(i1)==true) ans++;
printf("%d\n",ans);
fclose(stdin);
fclose(stdout);
return 0;
}