就是最简单能想到的那种拿到两个形状然后匹配。 但是有好多好多
/*
ID: modengd1
PROG: starry
LANG: C++
*/
#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <vector>
using namespace std;
struct pic
{
pair<int,int> LU;
pair<int,int> RB;
char shap[100][100];
};
vector<pic> marked;
char Map[100][100];
bool vis[100][100];
int N,M;
void findPic(int x,int y,pic& P)
{
vis[x][y]=true;
P.shap[x][y]=Map[x][y];
P.LU.first=min(x,P.LU.first);
P.LU.second=min(y,P.LU.second);
P.RB.first=max(x,P.RB.first);
P.RB.second=max(y,P.RB.second);
for(int i=x-1;i<=x+1;i++)
{
for(int j=y-1;j<=y+1;j++)
{
if(i<0||i>=N||j<0||j>=M)
continue;
if(vis[i][j])
continue;
if(Map[i][j]!='1')
continue;
findPic(i,j,P);
}
}
}
void floodFill(int x,int y,char ch)
{
vis[x][y]=true;
Map[x][y]=ch;
for(int i=x-1;i<=x+1;i++)
{
for(int j=y-1;j<=y+1;j++)
{
if(i<0||i>=N||j<0||j>=M)
continue;
if(vis[i][j])
continue;
if(Map[i][j]!='1')
continue;
floodFill(i,j,ch);
}
}
}
bool match(pic P,char& ch)
{
int h=P.RB.first-P.LU.first+1;
int w=P.RB.second-P.LU.second+1;
for(int i=0;i<marked.size();i++)
{
int H=marked[i].RB.first-marked[i].LU.first+1;
int W=marked[i].RB.second-marked[i].LU.second+1;
if(!((W==w&&H==h)||(W==h&&H==w)))
continue;
//和图一比
bool ret =true;
for(int x=0;x<H;x++)
{
for(int y=0;y<W;y++)
{
if((marked[i].shap[marked[i].LU.first+x][marked[i].LU.second+y]=='1')^(P.shap[P.LU.first+x][P.LU.second+y]=='1'))
ret=false;
}
}
if(ret)
{
ch=i+'a';
return true;
}
//和图二比
ret = true;
for(int x=0;x<H;x++)
{
for(int y=0;y<W;y++)
{
if((marked[i].shap[marked[i].LU.first+x][marked[i].LU.second+y]=='1')^(P.shap[P.LU.first+y][P.LU.second+x]=='1'))
ret=false;
}
}
if(ret)
{
ch=i+'a';
return true;
}
//和图三比
ret = true;
for(int x=0;x<H;x++)
{
for(int y=0;y<W;y++)
{
if((marked[i].shap[marked[i].LU.first+x][marked[i].LU.second+y]=='1')^(P.shap[P.RB.first-x][P.RB.second-y]=='1'))
ret=false;
}
}
if(ret)
{
ch=i+'a';
return true;
}
//和图四比
ret = true;
for(int x=0;x<H;x++)
{
for(int y=0;y<W;y++)
{
if((marked[i].shap[marked[i].LU.first+x][marked[i].LU.second+y]=='1')^(P.shap[P.RB.first-y][P.RB.second-x]=='1'))
ret=false;
}
}
if(ret)
{
ch=i+'a';
return true;
}
//和图五比
ret = true;
for(int x=0;x<H;x++)
{
for(int y=0;y<W;y++)
{
if((marked[i].shap[marked[i].LU.first+x][marked[i].LU.second+y]=='1')^(P.shap[P.RB.first-x][P.LU.second+y]=='1'))
ret=false;
}
}
if(ret)
{
ch=i+'a';
return true;
}
//和图六比
ret = true;
for(int x=0;x<H;x++)
{
for(int y=0;y<W;y++)
{
if((marked[i].shap[marked[i].LU.first+x][marked[i].LU.second+y]=='1')^(P.shap[P.RB.first-y][P.LU.second+x]=='1'))
ret=false;
}
}
if(ret)
{
ch=i+'a';
return true;
}
//和图七比
ret = true;
for(int x=0;x<H;x++)
{
for(int y=0;y<W;y++)
{
if((marked[i].shap[marked[i].LU.first+x][marked[i].LU.second+y]=='1')^(P.shap[P.LU.first+x][P.RB.second-y]=='1'))
ret=false;
}
}
if(ret)
{
ch=i+'a';
return true;
}
//和图八比
ret = true;
for(int x=0;x<H;x++)
{
for(int y=0;y<W;y++)
{
if((marked[i].shap[marked[i].LU.first+x][marked[i].LU.second+y]=='1')^(P.shap[P.LU.first+y][P.RB.second-x]=='1'))
ret=false;
}
}
if(ret)
{
ch=i+'a';
return true;
}
}
ch=marked.size()+'a';
marked.push_back(P);
return false;
}
int main()
{
freopen("starry.in","r",stdin);
freopen("starry.out","w",stdout);
scanf("%d",&M);
scanf("%d",&N);
getchar();
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
scanf("%c",&Map[i][j]);
}
getchar();
}
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
if(Map[i][j]=='1')
{
memset(vis,false,sizeof(vis));
pic P;
P.LU.first=0x7fffffff;
P.LU.second=0x7fffffff;
P.RB.first=0;P.RB.second=0;
memset(P.shap,'0',sizeof(P.shap));
char ch;
char &c=ch;
pic& p=P;
findPic(i,j,p);
memset(vis,false,sizeof(vis));
match(P,c);
floodFill(i,j,ch);
}
}
}
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
printf("%c",Map[i][j]);
cout<<endl;
}
return 0;
}
细节需要注意。