问题 A: Square Cover

题目链接
题目描述
You are given a matrix A of size N×M. You should partition the matrix in square submatrices such that each submatrix contains cells having the same value. Even more, all the cells having the same value should be part of the same square. Find out if it’s possible or not to do this.
输入
The first line contains two integers N and M.
Each of the next N lines contains M integers representing the elements of the matrix A.
1<= N,M <=300
0<= Ai,j <=1e5

输出
Print 1 if there is a solution, otherwise print 0.
样例输入 Copy
3 3
1 1 2
1 1 3
4 5 6
4 4
1 1 3 3
1 1 3 3
2 2 4 4
2 2 4 4
2 5
1 1 1 2 2
1 1 1 2 2
3 3
1 1 1
1 2 1
1 1 1
3 3
1 1 2
1 1 3
4 5 1

题意:给你n*m的矩阵,让你可以任意构成一个子矩阵,并且子矩阵的每个位置的值都相同,问你是否能完成,能输出1,不能输出0;
解题思路:数据比较小,我们可以直接暴力枚举,先找矩阵,然后标记,使得我们找到的每个子矩阵的值都不相同,一旦相同必然不行。

#include<stdio.h>
#include<string.h>
#include<map>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int mp[1005][1005];
int vis[100005];
int vvs[1005][1005];
int n,m;
int ddg()
{
    for(int i=1; i<=n; i++)//输入矩阵
    {
        for(int j=1; j<=m; j++)//
        {
            if(vis[mp[i][j]]==0)//标记
            {
                vis[mp[i][j]]=1;
                int hh,gg;
                int kk=mp[i][j];标记一个值
                int qw=0,we=0;
                for(hh=i; hh<=n; hh++)//查找子矩阵
                {
                    if(mp[hh][j]!=kk)//不相同则说明找到子矩阵
                    {
                        break;
                    }
                    else
                    {
                        qw++;
                    }
                }
                for(gg=j; gg<=m; gg++)
                {
                    if(mp[i][gg]!=kk)
                    {
                        break;
                    }
                    else
                    {
                        we++;
                    }
                }
                qw--;
                we--;
                if(qw==we)
                {
                    for(int hj=i; hj<=i+qw; hj++)
                    {
                        for(int hk=j; hk<=j+we; hk++)
                        {
                            if(mp[hj][hk]!=kk)
                            {
 
                                return 0;
                            }
                            else
                            {
                                vvs[hj][hk]=1;
                            }
                        }
                    }
                }
                else
                {
                    return 0;
                }
 
            }
            else
            {
                if(vvs[i][j]==0)
                {
                    return 0;
                }
            }
        }
    }
    return 1;
}
int main()
{
 
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(mp,0,sizeof(mp));
        memset(vis,0,sizeof(vis));
        memset(vvs,0,sizeof(vvs));
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                scanf("%d",&mp[i][j]);
            }
        }
        printf("%d\n",ddg());
    }
    return 0;
全部评论

相关推荐

07-09 19:25
门头沟学院 Java
这是要把每一个投校招的都开盒吗?
26届之耻将大局逆转:裁人的时候一次性追回餐费
点赞 评论 收藏
分享
06-26 22:20
门头沟学院 Java
码农索隆:让你把简历发给她,她说一些套话,然后让你加一个人,说这个人给你改简历,然后开始卖课
我的求职精神状态
点赞 评论 收藏
分享
lllllkin:感觉可以精简到一页简历,有些排版感觉不是必须的。 时间线越早的,你自己越熟悉的放前面。描述可以更精简些,一些问题解决感觉可以不用写具体技术栈,卖个关子,等面试官问。
点赞 评论 收藏
分享
07-10 11:08
门头沟学院 Java
投递京东等公司9个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务