B. Almost Ternary Matrix

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--;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值