Section 5.1 Starry Night

/*
ID: niepeng1
PROG: starry
LANG: C++
*/
/*
记录把每个矩形块记录,然后按照四个方向匹配,其实也不复杂奥,就是,难道是我想的太少了。

其实细节方面处理也很麻烦的。
只是为什么我写的用char的一直会错误。
而用int的就会正确的呢?
*/
#include <stdio.h>
struct node{
 int w;
 int h;
 int con[101][101];
}model[40];
int before[100][101],after[100][101],temp[100][101];
int lx,ly,rx,ry,w,h,modnum,now;
bool same(int t,int th,int tw)
{
 int i,j;
 bool can;
 if(! ( (model[t].h==th&&model[t].w==tw) || (model[t].w==th&&model[t].h==tw)) )
  return false;
 if( model[t].h==th&&model[t].w==tw ){
  can=true;
  for(i=0;i<model[t].h;i++){
   for(j=0;j<model[t].w;j++)
    if(model[t].con[i][j]!=temp[i][j] && (model[t].con[i][j]=='0' || temp[i][j]=='0') )
    {
     can=false;
     break;
    }
    if(!can)
     break;
  }
  if(can)  return true;

  can=true;
  for(i=0;i<model[t].h;i++){
   for(j=0;j<model[t].w;j++)
    if( model[t].con[i][j]!=temp[model[t].h-1-i][model[t].w-1-j] && (model[t].con[i][j]=='0' || temp[model[t].h-1-i][model[t].w-1-j]=='0')){
     can=false;
     break;
    }
    if(!can)
     break;
  }
  if(can)  return true;
  
  can=true;
  for(i=0;i<model[t].h;i++){
   for(j=0;j<model[t].w;j++)
    if( model[t].con[i][j]!=temp[model[t].h-1-i][j] && (model[t].con[i][j]=='0' || temp[model[t].h-1-i][j]=='0')){
     can=false;
     break;
    }
    if(!can)
     break;
  }
  if(can)  return true;

  can=true;
  for(i=0;i<model[t].h;i++){
   for(j=0;j<model[t].w;j++){
    if( model[t].con[i][j]!=temp[i][model[t].w-1-j] && (model[t].con[i][j]=='0' || temp[i][model[t].w-1-j]=='0')){
     can=false;
     break;
    }
   }
   if(!can)
    break;
  }
  if(can)  return true;

 }

 if( model[t].w==th&&model[t].h==tw ){
  can=true;
  for (i=0;i<model[t].h;i++)
  {
   for (j=0;j<model[t].w;j++)
    if (model[t].con[i][j]!=temp[j][i]&&(model[t].con[i][j]=='0'||temp[j][i]=='0'))
    {
     can=false;
     break;
    }
   if (!can) break;
  }
  if (can) return true;

  can=true;
  for(i=0;i<model[t].h;i++){
   for(j=0;j<model[t].w;j++)
    if(model[t].con[i][j]!=temp[model[t].w-j-1][i] && (model[t].con[i][j]=='0'||temp[model[t].w-j-1][i]=='0'))
    {
     can=false;
     break;
    }
    if(!can) break;
  }
  if(can) return true;
  
  can=true;
  for (i=0;i<model[t].h;i++)
  {
   for (j=0;j<model[t].w;j++)
    if (model[t].con[i][j]!=temp[j][model[t].h-i-1]&&(model[t].con[i][j]=='0'||temp[j][model[t].h-i-1]=='0'))
    {
     can=false;
     break;
    }
   if (!can) break;
  }
  if (can) return true;

  can=true;
  for (i=0;i<model[t].h;i++)
  {
   for (j=0;j<model[t].w;j++)
    if (model[t].con[i][j]!=temp[model[t].w-j-1][model[t].h-i-1]&&(model[t].con[i][j]=='0'||temp[model[t].w-j-1][model[t].h-i-1]=='0'))
    {
     can=false;
     break;
    }
   if (!can) break;
  }
  if (can) return true;

 }
 return false;

}
int find(int th,int tw)
{
 int i,j,t;
 for(t=0;t<=modnum;t++)
  if(same(t,th,tw)) return t+'a';
 modnum++;
 model[modnum].w=tw;
 model[modnum].h=th;
 for(i=0;i<th;i++)
  for(j=0;j<tw;j++)
   model[t].con[i][j]=temp[i][j];
 return t+'a';
}
void flood_fill(int x,int y,int num)
{
 before[x][y]=num+'0';
 if(x-1>=0&&y-1>0&&before[x-1][y-1]=='1') {flood_fill(x-1,y-1,num);   if(x-1<lx) lx=x-1;if(y-1<ly) ly=y-1;}
 if(x-1>=0&&before[x-1][y]=='1')    {flood_fill(x-1,y,num);  if(x-1<lx) lx=x-1;}
 if(x-1>=0&&y+1<w&&before[x-1][y+1]=='1') {flood_fill(x-1,y+1,num); if(x-1<lx) lx=x-1;if(y+1>ry) ry=y+1;}

 if(x+1<h && before[x+1][y]=='1')   {flood_fill(x+1,y,num);  if(x+1>rx) rx=x+1;}
 if(x+1<h&&y-1>=0&&before[x+1][y-1]=='1') {flood_fill(x+1,y-1,num); if(x+1>rx) rx=x+1;if(y-1<ly) ly=y-1;}
 if(x+1<h&&y+1<w&&before[x+1][y+1]=='1')  {flood_fill(x+1,y+1,num); if(x+1>rx) rx=x+1;if(y+1>ry) ry=y+1;}
 
 if(y-1>=0&&before[x][y-1]=='1')    {flood_fill(x,y-1,num);  if(y-1<ly) ly=y-1;}
 if(y+1<w &&before[x][y+1]=='1')    {flood_fill(x,y+1,num);  if(y+1>ry) ry=y+1;}
}
void oper(int x,int y,int num)
{
 int i,j;
 int ch;
 lx=rx=x;ly=ry=y;
 flood_fill(x,y,num);
 for(i=lx;i<=rx;i++)
  for(j=ly;j<=ry;j++)
   if(before[i][j]!='0'+num) temp[i-lx][j-ly]='0';//这个非常的精辟,我又忽略了
   else temp[i-lx][j-ly]='0'+num;
 ch=find(rx-lx+1,ry-ly+1);
 for(i=lx;i<=rx;i++)
  for(j=ly;j<=ry;j++)
   if(before[i][j]=='0'+num) after[i][j]=ch;
}


int main()
{
 freopen("starry.in","r",stdin);
 freopen("starry.out","w",stdout);
 char c;
 int i,j;
 scanf("%d %d",&w,&h);
 c=getchar();
 for(i=0;i<h;i++)
 {
  for (j=0;j<w;j++)
   scanf("%c",&before[i][j]);

  c=getchar();
 }
 modnum=-1;now=2;

 for(i=0;i<h;i++)
  for(j=0;j<w;j++)
  {
   if(before[i][j]=='0') after[i][j]='0';
   else if(before[i][j]=='1') oper(i,j,now++);
  }
 for(i=0;i<h;i++)
 {
  for(j=0;j<w;j++)
   printf("%c",after[i][j]);
  printf("/n");
 }
 return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值