time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output
You are given two even integers nn and mm. Your task is to find any binary matrix aa with nn rows and mm columns where every cell (i,j) has exactly two neighbours with a different value than ai,j.
Two cells in the matrix are considered neighbours if and only if they share a side. More formally, the neighbours of cell (x,y) are: (x−1,y), (x,y+1), (x+1,y) and (x,y−1).
It can be proven that under the given constraints, an answer always exists.
Input
Each test contains multiple test cases. The first line of input contains a single integer tt (1≤t≤100) — the number of test cases. The following lines contain the descriptions of the test cases.
The only line of each test case contains two even integers nn and mm (2≤n,m≤50) — the height and width of the binary matrix, respectively.
Output
For each test case, print n lines, each of which contains m numbers, equal to 0 or 1 — any binary matrix which satisfies the constraints described in the statement.
It can be proven that under the given constraints, an answer always exists.
Example
input
3 2 4 2 2 4 4
output
1 0 0 1 0 1 1 0 1 0 0 1 1 0 1 0 0 0 1 1 1 1 0 0 0 1 0 1
Note
White means 0 black means 1.
The binary matrix from the first test case
The binary matrix from the second test case
The binary matrix from the third test case
这题依旧是找规律题,首先我们按2*2展开到2*4到4*4这样我们就能够看到一个规律
每两行一个循环
第一行是1001......
第二行是0110.....
ok,我们直接写代码就行。
#include <iostream>
using namespace std;
int main()
{
int t;
cin >> t;
while (t > 0)
{
int n, m;
cin >> n >> m;
int maze[50][50];
int index1 = 0;
for (int i = 0; i < n; i = i + 2)
{
int index2 = 0;
if (index1 % 2 == 0)
{
for (int j = 0; j < m; j = j + 2)
{
if (index2 % 2 == 0)
{
maze[i][j] = 1;
maze[i][j + 1] = 0;
}
else
{
maze[i][j] = 0;
maze[i][j + 1] = 1;
}
if (index2 % 2 == 0)
{
maze[i + 1][j] =0;
maze[i + 1][j + 1] =1;
}
else
{
maze[i + 1][j] =1;
maze[i + 1][j + 1] =0;
}
index2++;
}
}
else
{
for (int j = 0; j < m; j = j + 2)
{
if (index2 % 2 == 0)
{
maze[i][j] = 0;
maze[i][j + 1] = 1;
}
else
{
maze[i][j] = 1;
maze[i][j + 1] = 0;
}
if (index2 % 2 == 0)
{
maze[i + 1][j] =1;
maze[i + 1][j + 1] =0;
}
else
{
maze[i + 1][j] =0;
maze[i + 1][j + 1] =1;
}
index2++;
}
}
index1++;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << maze[i][j] << " ";
}
cout << endl;
}
t--;
}
}