zoukankan      html  css  js  c++  java
  • CF11C How Many Squares?

    【题目链接】

    题意分析

    这道题感觉应做的话可以 但是巧法锻炼思维

    首先 我们枚举正方形的左上角坐标 然后依次扫描四条边的长度 注意这里扫描有两种方式 平行于边以及平行于对角线

    判断四条边的长度是否相等 这是第一个指标

    然后 我们还需要判断这是否是一个独立的正方形

    这里 我们可以使用搜索判断联通的1的个数

    然后看看这数量是否和正方形的1的数量相等 这是第二个指标

    这样的话 我们就可以找到全部的正方形了

    CODE:

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    #define N 255
    using namespace std;
    int T,ans;
    int num[N][N];
    int n,m;
    int tx[10]={0,1,0,-1,0,1,1,-1,-1};
    int ty[10]={0,0,1,0,-1,1,-1,-1,1};
    queue<pair<int,int> > que;
    bool safe(int x,int y)
    {return x>=1&&x<=n&&y>=1&&y<=n;}
    int check1(int x,int y)
    {
    	int cnt1=0,cnt2=0,cnt3=0,cnt4=0;
    	int nowx=x,nowy=y;
    	while(998244353)
    	{
    //		printf("now is at %d %d1\n",nowx,nowy);
    		int tox=nowx+tx[1],toy=nowy+ty[1];
    		if(safe(tox,toy)&&num[tox][toy])
    		{
    			++cnt1;
    			nowx=tox;nowy=toy;
    		}
    		else break;
    	}
    	while(19260817)
    	{
    //		printf("now is at %d %d2\n",nowx,nowy);
    		int tox=nowx+tx[2],toy=nowy+ty[2];
    		if(safe(tox,toy)&&num[tox][toy])
    		{
    			++cnt2;
    			nowx=tox;nowy=toy;
    		}
    		else break;
    	}
    	while(20020529)
    	{
    //		printf("now is at %d %d3\n",nowx,nowy);
    		int tox=nowx+tx[3],toy=nowy+ty[3];
    		if(safe(tox,toy)&&num[tox][toy])
    		{
    			++cnt3;
    			nowx=tox;nowy=toy;
    		}
    		else break;
    	}
    	while(1000000007)
    	{
    //		printf("now is at %d %d4\n",nowx,nowy);
    		int tox=nowx+tx[4],toy=nowy+ty[4];
    		if(safe(tox,toy)&&num[tox][toy])
    		{
    			++cnt4;
    			nowx=tox;nowy=toy;
    		}
    		else break;
    	}
    	if(cnt1==cnt2&&cnt2==cnt3&&cnt3==cnt4&&cnt4==cnt1) return cnt1;
    	else return 0; 
    }
    int check2(int x,int y)
    {
    	int cnt1=0,cnt2=0,cnt3=0,cnt4=0;
    	int nowx=x,nowy=y;
    	while(998244353)
    	{
    //		printf("now is at %d %d5\n",nowx,nowy);
    		int tox=nowx+tx[5],toy=nowy+ty[5];
    		if(safe(tox,toy)&&num[tox][toy])
    		{
    			++cnt1;
    			nowx=tox;nowy=toy;
    		}
    		else break;
    	}
    	while(19260817)
    	{
    //		printf("now is at %d %d6\n",nowx,nowy);
    		int tox=nowx+tx[6],toy=nowy+ty[6];
    		if(safe(tox,toy)&&num[tox][toy])
    		{
    			++cnt2;
    			nowx=tox;nowy=toy;
    		}
    		else break;
    	}
    	while(20020529)
    	{
    //		printf("now is at %d %d7\n",nowx,nowy);
    		int tox=nowx+tx[7],toy=nowy+ty[7];
    		if(safe(tox,toy)&&num[tox][toy])
    		{
    			++cnt3;
    			nowx=tox;nowy=toy;
    		}
    		else break;
    	}
    	while(1000000007)
    	{
    //		printf("now is at %d %d8\n",nowx,nowy);
    		int tox=nowx+tx[8],toy=nowy+ty[8];
    		if(safe(tox,toy)&&num[tox][toy])
    		{
    			++cnt4;
    			nowx=tox;nowy=toy;
    		}
    		else break;
    	}
    	if(cnt1==cnt2&&cnt2==cnt3&&cnt3==cnt4&&cnt4==cnt1) return cnt1;
    	else return 0; 
    }
    int dfsclear(int x,int y)
    {
    	que.push(make_pair(x,y));int cnt=0;
    	num[x][y]=0;
    	while(!que.empty())
    	{
    		int nowx=que.front().first,nowy=que.front().second;que.pop();
    		++cnt;
    		for(int i=1;i<=8;++i)
    		{
    			int tox=nowx+tx[i],toy=nowy+ty[i];
    			if(safe(tox,toy)&&num[tox][toy]) 
    			{
    				que.push(make_pair(tox,toy));
    				num[tox][toy]=0;
    			}
    		}
    	}
    	return cnt;
    }
    int main()
    {
    	scanf("%d",&T);
    	while(T--)
    	{
    		scanf("%d%d",&n,&m);ans=0;
    		for(int i=1;i<=n;++i)
    		 for(int j=1;j<=m;++j)
    		  scanf("%1d",&num[i][j]);
    		for(int i=1;i<=n;++i)
    		 for(int j=1;j<=m;++j)
    		 {
    		 	if(num[i][j]==0) continue;
    //		 	printf("now at %d %d\n",i,j);
    			int t1=check1(i,j),t2=check2(i,j);
    //		 	if(t1!=0||t2!=0) printf("now is %d %d\n",t1,t2);
    		 	int tmp=dfsclear(i,j);
    //		 	if(t1!=0||t2!=0) printf("now is (%d %d) %d\n",t1,t2,tmp);
    		 	if(t1*4==tmp||t2*4==tmp) ++ans;
    		 } 
    		printf("%d\n",ans);  
    	} 
    	return 0;
    }
    
  • 相关阅读:
    Java8 Stream Function
    PLINQ (C#/.Net 4.5.1) vs Stream (JDK/Java 8) Performance
    罗素 尊重 《事实》
    小品 《研发的一天》
    Java8 λ表达式 stream group by max then Option then PlainObject
    这人好像一条狗啊。什么是共识?
    TOGAF TheOpenGroup引领开发厂商中立的开放技术标准和认证
    OpenMP vs. MPI
    BPMN2 online draw tools 在线作图工具
    DecisionCamp 2019, Decision Manager, AI, and the Future
  • 原文地址:https://www.cnblogs.com/tcswuzb/p/14308822.html
Copyright © 2011-2022 走看看