In this question, we can see that the data range is very small, so we can use violent enumeration to traverse all the lengths and widths that can form a rectangle. At the same time, we must meet the condition that there are no obstacles in this rectangle to obtain the maximum perimeter.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
string mp[30];
bool solve(int i ,int j ,int k,int l){
for(int m=i;m<=k;m++){
for(int n=j;n<=l;n++){
if(mp[m][n]=='1') return false;
}
}
return true;
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++){
cin>>mp[i];
}
int res=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
for(int k=i;k<n;k++){
for(int l=j;l<m;l++){
if(solve(i,j,k,l)&&((k-i+1)*2+2*(l-j+1))>res){
res=(k-i+1)*2+2*(l-j+1);
}
}
}
}
}
cout<<res<<endl;
}
First of all, the special judgment first, when m=1 and n=1 and n<=m, it may cause the game to never end, and then we traverse
2~m, if n%i can be satisfied, the game may never end.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define maxn 1000000
int main()
{
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int f=0;
if(n==1||m==1){
cout<<"YES"<<endl;
}else if(m>=n) cout<<"NO"<<endl;
else{
for(int i=2;i<=m&&i<=n/i;i++){
if(n%i==0){
f=1;
}
}
if(f==0) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
}