CodeForces - 1621A Stable Arrangement of Rooks

A. Stable Arrangement of Rooks
time limit per test1 second
memory limit per test256 megabytes

You have an n×n chessboard and k rooks. Rows of this chessboard are numbered by integers from 1 to n from top to bottom and columns of this chessboard are numbered by integers from 1 to n from left to right. The cell (x,y) is the cell on the intersection of row x and collumn y for 1≤x≤n and 1≤y≤n.

The arrangement of rooks on this board is called good, if no rook is beaten by another rook.

A rook beats all the rooks that shares the same row or collumn with it.

The good arrangement of rooks on this board is called not stable, if it is possible to move one rook to the adjacent cell so arrangement becomes not good. Otherwise, the good arrangement is stable. Here, adjacent cells are the cells that share a side.
在这里插入图片描述

Such arrangement of 3 rooks on the 4×4 chessboard is good, but it is not stable: the rook from (1,1) can be moved to the adjacent cell (2,1) and rooks on cells (2,1) and (2,4) will beat each other.
Please, find any stable arrangement of k rooks on the n×n chessboard or report that there is no such arrangement.

Input
The first line contains a single integer t (1≤t≤100) — the number of test cases.

The first line of each test case contains two integers n, k (1≤k≤n≤40) — the size of the chessboard and the number of rooks.

Output
If there is a stable arrangement of k rooks on the n×n chessboard, output n lines of symbols . and R. The j-th symbol of the i-th line should be equals R if and only if there is a rook on the cell (i,j) in your arrangement.

If there are multiple solutions, you may output any of them.

If there is no stable arrangement, output −1.

Example
input
5
3 2
3 3
1 1
5 2
40 33
output
…R

R…
-1
R

R…

…R

-1

Note
In the first test case, you should find stable arrangement of 2 rooks on the 3×3 chessboard. Placing them in cells (3,1) and (1,3) gives stable arrangement.

In the second test case it can be shown that it is impossbile to place 3 rooks on the 3×3 chessboard to get stable arrangement.

问题链接CodeForces - 1621A Stable Arrangement of Rooks
问题简述:(略)
问题分析:(略)

AC的C语言程序如下:

/* CodeForces - 1621A Stable Arrangement of Rooks */

#include <stdio.h>

int main()
{
    int t, n, k;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &n, &k);
        if (2 * k - n > 1) puts("-1");
        else {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++)
                    if (i == j && i % 2 == 1 && k) {
                        putchar('R');
                        k--;
                    } else
                        putchar('.');
                putchar('\n');
            }
        }
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值