poj 1021

23 篇文章 0 订阅
/*2316K	0MS*/
#include <iostream>
//#include <fstream>
#include <queue>
using namespace std;

#define SIZE 100
typedef struct _point
{
    int _x;
    int _y;
}point;

queue<point>  q;
int w,h,n;
//fstream fin;
bool  map[SIZE][SIZE];

class subgraph
{
    public:
      bool data[SIZE][SIZE],use;
      int count;
      int height,width;
      subgraph *next;
      subgraph(){
         count=height=width=0;
         memset(data,0,sizeof(data));
         next=0;
      }
      subgraph(int w,int h)
      {
          count=0;
          width=w;
          height=h;
          use=false;
          next=0;
          memset(data,0,sizeof(data));  
      }
      void  add(int x,int y)
      {
          data[x][y]=true;
      }
      ~subgraph(){delete []data;}
};

class graph
{
    public:
        subgraph *start,*end;
        int len;
        int total;
        graph()
        {
            start=0;
            end=0;
            len=total=0;
        }
        void add(subgraph *t)
        {
            if(!start)
            {
               start=t;
               end=t;  
            }
            else
            {
                end->next=t;
                end=t;
            }
        }
        
        //¼ÆËãlenºÍtotal 
        void cul()
        {
           for(subgraph *a=start;a!=0;a=a->next)
           {
               len++;
               total+=a->count;
           } 
        } 
        
        //±È½ÏÁ½¸öͼÐÎÊDz»ÊÇ¿ÉÒÔͨ¹ýת»¯µÃµ½ 
        
        bool cmp(subgraph *s1,subgraph *s2)
        {
             if(s1->count!=s2->count)  return false;
             queue<int> q1,q2;
             
             for(int i=0;i<s2->width;i++)
               for(int j=0;j<s2->height;j++)
                  q1.push(s2->data[i][j]);
             
             int len=q1.size();
             int *p2=new int[len];
             for(int i=0;i<len;i++)
             {
                 p2[i]=q1.front();
                 q1.pop();
             }  
                
             //Ö±½Ó±È½Ï 
             bool flag=true;
             if(s1->width==s2->width&&s1->height==s2->height)
             {
                 for(int i=0;i<s1->width;i++)
                   for(int j=0;j<s1->height;j++)
                   {
                       if(s1->data[i][j]!=s2->data[i][j])
                       {
                           flag=false;
                           break;
                       }
                       if(!flag)  break;
                   }
                 if(flag)  return true;  
             }    
             
             //ˮƽ·­×ª
             flag=true;
             if(s1->width==s2->width&&s1->height==s2->height)
             {
                 for(int i=0;i<s1->width;i++)
                   for(int j=0;j<s1->height;j++)
                   {
                       if(s1->data[i][j]!=s2->data[s1->width-1-i][j])
                       {
                           flag=false;
                           break;
                       }
                       if(!flag) break;
                   }
                   if(flag)   return true;
             } 
             
             //´¹Ö±·­×ª 
             
             flag=true;
             if(s1->width==s2->width&&s1->height==s2->height)
             {
                 for(int i=0;i<s1->width;i++)
                   for(int j=0;j<s1->height;j++)
                   {
                       if(s1->data[i][j]!=s2->data[i][s1->height-1-j])
                       {
                           flag=false;
                           break;
                       }
                       if(!flag) break;
                   }
                   if(flag)  return true;
             } 
             
             //Ðýת90¶È
             flag=true;
             if(s1->width==s2->height&&s1->height==s2->width)
             {
                 for(int j=0;j<s1->height;j++)
                   for(int i=s1->width-1;i>=0;i--)
                   {
                       q2.push(s1->data[i][j]);
                   }
                   
                 for(int i=0;i<len;i++)
                 {
                      if(flag){
                      int t=q2.front();
                      if(t!=p2[i])
                          flag=false;
                      }    
                      q2.pop();
                 }
                 if(flag)  return true;  
             }
             
             //Ðýת180¶È
             flag=true;
             if(s1->width==s2->width&&s1->height==s2->height)
             {
                 for(int i=s1->width-1;i>=0;i--)
                   for(int j=s1->height-1;j>=0;j--)
                   {
                       q2.push(s1->data[i][j]);
                   }
                   
                 for(int i=0;i<len;i++)
                 {
                      if(flag){
                      int t=q2.front();
                      if(t!=p2[i])
                          flag=false;
                      }    
                      q2.pop();
                 }
                 if(flag)  return true;  
             }
             
             //Ðýת270¶È 
             flag=true;
             if(s1->width==s2->height&&s1->height==s2->width)
             {
                 for(int j=s1->height-1;j>=0;j--)
                   for(int i=s1->width-1;i>=0;i--)
                   {
                       q2.push(s1->data[i][j]);
                   }
                   
                 for(int i=0;i<len;i++)
                 {
                      if(flag){
                      int t=q2.front();
                      if(t!=p2[i])
                          flag=false;
                      }    
                      q2.pop();
                 }
                 if(flag)  return true;  
             }
             
             //Ðýת90¶È´¹Ö±·­×ª
             flag=true;
             if(s1->width==s2->height&&s1->height==s2->width)
             {
                 for(int j=0;j<s1->height;j++)
                   for(int i=0;i<s1->width;i++)
                   {
                       q2.push(s1->data[i][j]);
                   }
                   
                 for(int i=0;i<len;i++)
                 {
                      if(flag){
                      int t=q2.front();
                      if(t!=p2[i])
                          flag=false;
                      }    
                      q2.pop();
                 }
                 if(flag)  return true;  
             } 
             
             //Ðýת180¶È´¹Ö±·­×ª
             flag=true;
             if(s1->width==s2->width&&s1->height==s2->height)
             {
                 for(int i=s1->width-1;i>=0;i--)
                   for(int j=0;j<s1->height;j++)
                   {
                       q2.push(s1->data[i][j]);
                   }
                   
                 for(int i=0;i<len;i++)
                 {
                      if(flag){
                      int t=q2.front();
                      if(t!=p2[i])
                          flag=false;
                      }    
                      q2.pop();
                 }
                 if(flag)  return true;  
             } 
             
             //Ðýת270¶È´¹Ö±·­×ª
              
             flag=true;
             if(s1->width==s2->height&&s1->height==s2->width)
             {
                 for(int j=s1->height-1;j>=0;j--)
                   for(int i=0;i<s1->width;i++)
                   {
                       q2.push(s1->data[i][j]);
                   }
                   
                 for(int i=0;i<len;i++)
                 {
                      if(flag){
                      int t=q2.front();
                      if(t!=p2[i])
                          flag=false;
                      }    
                      q2.pop();
                 }
                 if(flag)  return true;  
             }  
             
             //Ðýת90¶Èˮƽ·­×ª
             flag=true;
             if(s1->width==s2->height&&s1->height==s2->width)
             {
                 for(int j=s1->height-1;j>=0;j--)
                   for(int i=s1->width-1;i>=0;i--)
                   {
                       q2.push(s1->data[i][j]);
                   }
                   
                 for(int i=0;i<len;i++)
                 {
                      if(flag){
                      int t=q2.front();
                      if(t!=p2[i])
                          flag=false;
                      }    
                      q2.pop();
                 }
                 if(flag)  return true;  
             }   
             
             //Ðýת180¶Èˮƽ·­×ª
             flag=true;
             if(s1->width==s2->height&&s1->height==s2->width)
             {
                 for(int i=0;i<s1->width;i++)
                   for(int j=s1->height-1;j>=0;j--)
                   {
                       q2.push(s1->data[i][j]);
                   }
                   
                 for(int i=0;i<len;i++)
                 {
                      if(flag){
                      int t=q2.front();
                      if(t!=p2[i])
                          flag=false;
                      }    
                      q2.pop();
                 }
                 if(flag)  return true;  
             }    
             
             
             //Ðýת270¶Èˮƽ·­×ª
             flag=true;
             if(s1->width==s2->height&&s1->height==s2->width)
             {
                 for(int j=0;j<s1->height;j++)
                   for(int i=s1->width-1;i>=0;i--)
                   {
                       q2.push(s1->data[i][j]);
                   }
                   
                 for(int i=0;i<len;i++)
                 {
                      if(flag){
                      int t=q2.front();
                      if(t!=p2[i])
                          flag=false;
                      }    
                      q2.pop();
                 }
                 if(flag)  return true;  
             } 
             
             delete [] p2;
             return false;   
        }
        
        //Á½¸öͼÐÎµÄ±È½Ï 
        bool compare(graph &g2)
        {
            if(len!=g2.len||total!=g2.total)  return false;
            else{
			for(subgraph *t1=g2.start;t1!=NULL;t1=t1->next)
			{
			  bool flag=false;
              for(subgraph *t2=start;t2!=NULL;t2=t2->next)
			  {
				  if(t1->use==false&&t2->use==false)
				     if(cmp(t2,t1))
					 {
					    t2->use=t1->use=true;
						flag=true;
						break;
					 }	 
      
                /*  for(int i1=0;i1<t1->width;i1++)
                  {
                         for(int i2=0;i2<t1->height;i2++)
                            cout<<t1->data[i1][i2]<<" "; 
                     cout<<endl;
                  }
                  cout<<"--------------"<<endl; */
			  }
			  if(!flag) return false;
			}
			return true;
		}
        }
};


void detect(int x,int y)
{
    if(x<0||y<0||x>=w||y>=h)  return;
    if(!map[x][y])  return;
    point p;
    p._x=x;
    p._y=y;
    q.push(p); 
    map[x][y]=false;
    detect(x-1,y);
    detect(x+1,y);
    detect(x,y-1);
    detect(x,y+1);
}

int main()
{
    //¶ÁÈ¡Êý¾Ý
    //fin.open("1021.txt",ios::in);
    
    int t; 
    cin>>t;
    
    while(t--)
    {
        memset(map,0,sizeof(map));
        cin>>w>>h>>n;
        int i,j;
        for(i=0;i<n;i++)
        {
            int x,y;
            cin>>x>>y;
            map[x][y]=true;
        }
        
      /*for(i=0;i<w;i++)
        {
             for(j=0;j<h;j++)
         {
             if(map[i][j])
               cout<<"1"<<" ";
             else
               cout<<"0"<<" ";
         }
          cout<<endl;
       }  */  
        //¶ÁÈ¡graph1 ºÍgraph2
        graph graph1,graph2;
        
        //graph1 
        for(i=0;i<w;i++)
          for(j=0;j<h;j++)
          {
              if(map[i][j])
              {
                  detect(i,j);
                  int len=q.size();
                  point *p=new point[len];
                  int index=0;
                  while(!q.empty())
                  {
                      p[index++]=q.front();
                      q.pop();   //³ö¶Ó 
                  }
                  
                  int leftx=100000;
                  int topy=100000;
                  int rightx,undery;
                  rightx=undery=-1;
                  
                  for(int k=0;k<len;k++)
                  {
                      if(leftx>p[k]._x)  leftx=p[k]._x;
                      if(topy>p[k]._y)  topy=p[k]._y;
                      if(rightx<p[k]._x)  rightx=p[k]._x;
                      if(undery<p[k]._y)  undery=p[k]._y;
                  }
          
                  subgraph *temp=new subgraph(rightx-leftx+1,undery-topy+1);
                  //cout<<temp->width<<" "<<temp->height<<endl; 
                  for(int l=0;l<len;l++)
                  {
                      int x,y;
                      x=p[l]._x-leftx;
                      y=p[l]._y-topy;
                      temp->add(x,y);
                  } 
                  
                 temp->count=len;
                 //cout<<"count:"<<temp->count<<endl; 
                 graph1.add(temp);
                 
                /* for(int i1=0;i1<temp->width;i1++)
                  {
                         for(int i2=0;i2<temp->height;i2++)
                            cout<<temp->data[i1][i2]<<" "; 
                     cout<<endl;
                  }
                  cout<<"--------------"<<endl;*/
                 delete []p;
              }
          }
          
          
          //graph2
          memset(map,0,sizeof(map));
          for(i=0;i<n;i++)
          {
            int x,y;
            cin>>x>>y;
            map[x][y]=true;
          }
          
        /*  for(i=0;i<w;i++)
         {
             for(j=0;j<h;j++)
            {
             if(map[i][j])
               cout<<"1"<<" ";
             else
               cout<<"0"<<" ";
            }
          cout<<endl;
        } */ 
          
          for(i=0;i<w;i++)
           for(j=0;j<h;j++)
           {
              if(map[i][j])
              {
                  detect(i,j);
                  int len=q.size();
                  point *p=new point[len];
                  int index=0;
                  while(!q.empty())
                  {
                      p[index++]=q.front();
                      q.pop();   //³ö¶Ó 
                  }
                  
                  int leftx=100000;
                  int topy=100000;
                  int rightx,undery;
                  rightx=undery=-1;
                  
                  for(int k=0;k<len;k++)
                  {
                      if(leftx>p[k]._x)  leftx=p[k]._x;
                      if(topy>p[k]._y)  topy=p[k]._y;
                      if(rightx<p[k]._x)  rightx=p[k]._x;
                      if(undery<p[k]._y)  undery=p[k]._y;
                  }
                  
                  subgraph *temp1=new subgraph(rightx-leftx+1,undery-topy+1);
                  //cout<<temp1->width<<" "<<temp1->height<<endl;
                  
                       
                  for(int l=0;l<len;l++)
                  {
                      int x,y;
                      x=p[l]._x-leftx;
                      y=p[l]._y-topy;
                      temp1->add(x,y);
                  } 
                 temp1->count=len; 
                 
                  /*for(int i1=0;i1<temp1->width;i1++)
                  {
                         for(int i2=0;i2<temp1->height;i2++)
                            cout<<temp1->data[i1][i2]<<" "; 
                     cout<<endl;
                  } 
                  cout<<"------------------"<<endl;  */
                 graph2.add(temp1);
                 
                 delete []p;
              }
           }
           
           graph1.cul();
           graph2.cul();
           //cout<<"len:"<<graph1.len<<"total:"<<graph1.total<<endl;
          // cout<<"len1:"<<graph2.len<<"total1:"<<graph2.total<<endl;
           if(graph1.compare(graph2))
            cout<<"YES"<<endl;
           else
             cout<<"NO"<<endl;
       }    
    system("pause");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值