B. Putting Plates
题目传送门;
题面:
题目大意:
意思是说一个矩阵长宽为 h , w h,w h,w,内部先全部变成0,只能在最外围那一圈把0变成1,规则是这个1九宫格里其他的几个不能为1。
代码:
#include<bits/stdc++.h>
using namespace std;
int a[22][22];
int n, m;
int check(int x, int y) {
if (a[x + 1][y + 1] == 1 && x + 1 < n && y + 1 < m)return 0;
if (a[x + 1][y - 1] == 1 && y - 1 >= 0 && x + 1 < n)return 0;
if (a[x - 1][y + 1] == 1 && y + 1 < m && x - 1 >= 0)return 0;
if (a[x - 1][y - 1] == 1 && x - 1 >= 0 && y - 1 >= 0)return 0;
if (a[x + 1][y] == 1 && x + 1 < n)return 0;
if (a[x - 1][y] == 1 && x - 1 >= 0)return 0;
if (a[x][y + 1] == 1 && y + 1 < m)return 0;
if (a[x][y - 1] == 1 && y - 1 >= 0)return 0;
return 1;
}
int main() {
int T;
cin >> T;
while(T--) {
memset(a, 0, sizeof a);
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i == 0 || i == n - 1 || j == 0 || j == m - 1) {
if (check(i, j) == 1) {
a[i][j] = 1;
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << a[i][j];
}
cout << endl;
}
cout << endl;
}
}