思想
我们可以看见包含符合要求的矩阵有很多个,但是要求最小的矩阵,因此只有红色矩阵才是我们要的答案
我们可以两点确定一个矩阵:
假设(x1,y1)是(1,1),(x2,y2)是(3,3),那么矩阵的长为3-1+1=3,宽为3-1+1=3
code
#include<bits/stdc++.h>
using namespace std;
int n,m;
const int INF=0x3f3f3f3f;
int maxrow=-INF,maxcol=-INF,minrow=INF,mincol=INF;
int main()
{
cin.tie(nullptr)->sync_with_stdio(false);
cin>>n>>m;
int t;cin>>t;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
int x;cin>>x;
if(x>t)continue;
maxrow=max(maxrow,i);
minrow=min(minrow,i);
maxcol=max(maxcol,j);
mincol=min(mincol,j);
}
}
cout<<maxrow-minrow+1<<endl;
cout<<maxcol-mincol+1<<endl;
return 0;
}