\(题目大意:\)
求一个\(h\times w\)的\(01\)矩阵,并且使矩阵中的每两个点\((x1,y1)(x2,y2)\)满足\(|x1-x2|+|y1-y2|\gt 1\).
\(Solution:\)
本题是贪心思想,考虑枚举每一个点,如果满足要求,则赋值为\(1\),否则为\(0\).最终得到的矩阵即为所求,输出即可\(AC\)。
\(Code:\)
#include <bits/stdc++.h>
#define ll long long
#define _for(i,a,b) for(int i=(a);i<=(b);i++)
namespace FastIOstream{
template <typename T> inline void read(T &x){//Fast read
x=0;int f=1;char c=getchar();
for(;!isdigit(c);){if(c=='-')f=-1;c=getchar();}
for(;isdigit(c);){x=(x<<1)+(x<<3)+c-48;c=getchar();}
x*=f;return;
}
template <typename T> void print(T x){//Fast print
if(x<0){x=-x;putchar('-');}if(x>9)print((x>>1)/5);
putchar(x%10+48);return;
}
}
using namespace std;
using namespace FastIOstream;
int t,f[25][25],tf[25][25];
bool check(int x,int y,int n,int m){
if(f[x][y]||f[x-1][y]||f[x][y-1]||f[x+1][y]||f[x][y+1]||f[x+1][y+1]||f[x-1][y-1]||f[x-1][y+1]||f[x+1][y-1])return 0;
return 1;
}
signed main(){
read(t);
for(int T=1;T<=t;T++){
memset(tf,0,sizeof(tf));
int n,m,ans=0;
read(n);
read(m);
for(int i=1;i<=n;i++){
tf[i][1]=1;
tf[i][m]=1;
}
for(int j=1;j<=m;j++){
tf[1][j]=1;
tf[n][j]=1;
}
int x=n>>1;
if(n&1)x++;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(tf[i][j]&&check(i,j,n,m)){
ans++;
f[i][j]=1;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cout << f[i][j];
f[i][j]=0;
}
puts(" ");
}
}
return 0;
}