Codeforces Round #302(Div. 2)——B—— 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 nk (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.

Sample test(s)

input

5 2

output

YESSSSSSLLLLLSSSSSLLLLLSSSSS

input

5 25

output

NO大意:岛和海,问你布局,只要行相邻为1,列相邻为1,花式模拟
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[110][110];
int j;
int main()
{
    int n,k;
    memset(a,0,sizeof(a));
    scanf("%d%d",&n,&k);
    int temp = 0;
    if(n%2 == 1){
        if( k > (1+n)*(1+n)/4 + (n-1)*(n-1)/4){
            printf("NO\n");
            return 0;
        }
        else {
            printf("YES\n");
          int i = 1;
            while(k > temp){
              if(i%2 == 1)
                 j = 1;
              else  j = 2;
              if(j == 1){
                  for(j = 1; j <= n ;j += 2){
                a[i][j] = 1;
                temp++;
                if(temp == k)
                    break;
                  }
                  i++;
              }
              else {
                  for(j = 2; j <= n-1 ; j+=2){
                a[i][j] = 1;
                temp++;
                if(temp == k)
                    break;
                  }
                  i++;
               }
            }
        }
   }
    else {
        if(k > n*n/2){
        printf("NO\n");
        return 0;
        }
        else {
        printf("YES\n");
        int i = 1;
        while(k > temp){
            if(i%2 == 1)
             j = 1;
            else j = 2;
            if(j == 1){
                for(j = 1; j <= n - 1; j+=2){
                    a[i][j] = 1;
                    temp++;
                    if(temp == k) break;
                }
                i++;
               }
            else {
                for(j = 2; j <= n ; j+=2){
                    a[i][j] = 1;
                    temp++;
                    if(temp == k) break;
                }
                i++;
            }
        }
    }
    }

            for(int i = 1; i <= n ;i++){
                for(int j = 1; j <= n ;j++){
                    if(a[i][j] == 0)
                    printf("S");
                    else if(a[i][j] == 1)
                    printf("L");
                }
                printf("\n");
            }
            return 0;
        }

  

时间: 2024-11-11 12:05:15

Codeforces Round #302(Div. 2)——B—— Sea and Islands的相关文章

Codeforces Round #302 (Div. 2) A B C

Codeforces Round #302 (Div. 2) A. Set of Strings 字符串 q 被称为 "beautiful" 当且仅当 q 可以被拆分成 k 个子串 (s1, s2, s3, ... , sk) 并且任意两个字串满足首字母不一样. 直接模拟,对 q 的每个字符进行判断,如果该字符在之前没有出现过,那么从它开始就可以组成一个新的字符串,并且计数,如果到了k 了则把之后的都归为一个字符串. #include <cstring> #include

Codeforces Round #302 (Div. 2) -- (A,B,C)

题目传送:Codeforces Round #302 (Div. 2) A. Set of Strings 思路:注意开头字母都不相同 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include &

水题 Codeforces Round #302 (Div. 2) A Set of Strings

题目传送门 1 /* 2 题意:一个字符串分割成k段,每段开头字母不相同 3 水题:记录每个字母出现的次数,每一次分割把首字母的次数降为0,最后一段直接全部输出 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <cstring> 8 #include <string> 9 #include <algorithm> 10 using namespace std; 11 12 con

完全背包 Codeforces Round #302 (Div. 2) C Writing Code

题目传送门 1 /* 2 题意:n个程序员,每个人每行写a[i]个bug,现在写m行,最多出现b个bug,问可能的方案有几个 3 完全背包:dp[i][j][k] 表示i个人,j行,k个bug dp[0][0][0] = 1 表示不选择人的时候所有的bug的种类犯错误都只有一种 4 dp[i][j][k] += dp[i%2][j-1][k-a[i]]: 5 错误示范:dp[i][j][k] += dp[i-1][j-l][k-l*a[i]]; 其实要从上一行的状态推出,即少一行 6 内存限制,

Codeforces Round #302 (Div. 2) (ABCD题解)

比赛链接:http://codeforces.com/contest/544 A. Set of Strings time limit per test:1 second memory limit per test:256 megabytes You are given a string q. A sequence of k strings s1,?s2,?...,?sk is called beautiful, if the concatenation of these strings is

Codeforces Round #302 (Div. 1 D)

http://codeforces.ru/contest/543/problem/D Problem 给一棵树,n个节点,n-1条边.把每个点当作首都,输出一个结果,一共要输出n个结果.边有两种形态,一种是好边,一种是坏边,当把一个点当作首都的时候,要求这个点到每个点的路径上坏边总数<=1,问有多少种树的形态. n: 10^5级别 Solution 很显然树形dp.设dp[i]表示,以 i 为根的子树的方案数.如果只需要算dp[1],直接dfs一遍,回溯的时候,dp[i]=∏(dp[j]+1),

Codeforces Round #302 (Div. 1 A)

http://codeforces.ru/contest/543/problem/A Problem N个人写代码,a[i]表示第i个人,写一行代码会出现a[i]个bug(不多也不少).现在问,N个人总共写M行,出现bug总数不超过b的方案数(允许有人一行也不写) 数据范围 N,M,b,a[i] : [1,500] Solution 先考虑最直接的dp.dp[i][j][k]表示前i个人,写了j 行,出现k个bug的方案数,那么转移,dp[i][j+j1][k+j1*a[i]]+=dp[i][j

Codeforces Round #302 (Div. 1)

A题是个背包问题.给你n个一行代码,第 i 代码写的时候会产生 ai 个bug,要写 m 行,总的bug不能超过 b 个,问有多少种方案,对mod取模. dp[i][j][k] = (dp[i-1][j][k] + dp[i][j-1][k-a[i]]) % mod; 表示不选第 i 个的话就有 dp[i-1][j][k], 选第 i 个就有 dp[i][j-1][k-a[i]]种方案.滚动数组节省空间 1 #include <iostream> 2 #include <cstdio&g

Codeforces Round #302 (Div. 2) D. Destroying Roads 最短路 删边

题目:有n个城镇,m条边权为1的双向边让你破坏最多的道路,使得从s1到t1,从s2到t2的距离分别不超过d1和d2. #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <queue> #include <stack> #in