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
    ",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
    ",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
    ",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
    ",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
    ",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
    ",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
    ",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
    ",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
    ",i,j);
    			int t1=check1(i,j),t2=check2(i,j);
    //		 	if(t1!=0||t2!=0) printf("now is %d %d
    ",t1,t2);
    		 	int tmp=dfsclear(i,j);
    //		 	if(t1!=0||t2!=0) printf("now is (%d %d) %d
    ",t1,t2,tmp);
    		 	if(t1*4==tmp||t2*4==tmp) ++ans;
    		 } 
    		printf("%d
    ",ans);  
    	} 
    	return 0;
    }
    
  • 相关阅读:
    2020-2021-1 20209324 《Linux内核原理与分析》第八周作业
    2020-2021-1 20209324《Linux内核原理与分析》第七周作业
    2019-2020-1 20209324《Linux内核原理与分析》第六周作业
    2020-2021-1 20209324《Linux内核原理与分析》第五周作业
    2019-2020-1 20209324《Linux内核原理与分析》第四周作业
    2019-2020-1 20209324《Linux内核原理与分析》第三周作业
    2019-2020-1 20209324《Linux内核原理与分析》第二周作业
    T-Sql 递归查询
    JS浏览器兼容问题
    IE && FireFox在javascript上的区别
  • 原文地址:https://www.cnblogs.com/LovToLZX/p/14308822.html
Copyright © 2011-2022 走看看