CodeForces - 544B Sea and Islands

A map of some object is a rectangular field consisting of n rows and n columns. Each cell is initially occupied by the sea but you can cover some some cells of the map with sand so that exactly k islands appear on the map. We will call a set of sand cells to be island if it is possible to get from each of them to each of them by moving only through sand cells and by moving from a cell only to a side-adjacent cell. The cells are called to be side-adjacent if they share a vertical or horizontal side. It is easy to see that islands do not share cells (otherwise they together form a bigger island).

Find a way to cover some cells with sand so that exactly k islands appear on the n × n map, or determine that no such way exists.

Input

The single line contains two positive integers n, k (1 ≤ n ≤ 100, 0 ≤ k ≤ n2) — the size of the map and the number of islands you should form.

Output

If the answer doesn't exist, print "NO" (without the quotes) in a single line.

Otherwise, print "YES" in the first line. In the next n lines print the description of the map. Each of the lines of the description must consist only of characters 'S' and 'L', where 'S' is a cell that is occupied by the sea and 'L' is the cell covered with sand. The length of each line of the description must equal n.

If there are multiple answers, you may print any of them.

You should not maximize the sizes of islands.

Example
Input
5 2
Output
YES
SSSSS
LLLLL
SSSSS
LLLLL
SSSSS
Input
5 25
Output

NO

题意:给一个n*n的地图,问你能在上面存在k 个小岛吗?如果这两个岛有公共的边,就是同一个岛。

思路:手动模拟几个样例就发现规律了,先判断最多能得到的岛屿的数量

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
    int n,k;
    int sum;
    while(scanf("%d%d",&n,&k)!=EOF){
        sum=(n+1)/2;//奇数行的个数
        sum*=(n+1)/2;//奇数行最多的岛屿
      //  printf("sum=%d\n",sum);
        sum+=(n-(n+1)/2)*(n/2);//偶数行最多的岛屿
       // printf("sum=%d k=%d\n",sum,k);
        if(sum>=k){
            printf("YES\n");
            char a[105][105];
            int i,j,cas=0;
            for(i=1;i<=n;i++){
                for(j=1;j<=n;j++){
                    if(i%2==1){
                        if(j%2==1 &&cas<k){//只打出K个岛屿
                             a[i][j]='L';
                             cas++;
                        }

                        else
                            a[i][j]='S';
                    }
                    else{
                       if(j%2==0 && cas<k){
                         a[i][j]='L';
                         cas++;
                       }
                        else
                            a[i][j]='S';
                    }
                }
            }
            for(i=1;i<=n;i++){
                for(j=1;j<=n;j++){
                    printf("%c",a[i][j]);
                }
                printf("\n");
            }

        }
        else
            printf("NO\n");

    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值