CF Sea and Islands

本文介绍了一种解决特定地图覆盖问题的方法,旨在通过算法确定如何在n×n的地图上放置沙子以形成k个岛屿。文章提供了一段C++代码示例,用于计算并输出是否有可能实现目标岛屿数量,如果可能,则输出具体的地图布局。
Sea and Islands
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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
YES
SSSSS
LLLLL
SSSSS
LLLLL
SSSSS
input
5 25
output
NO



判下奇偶再输出就行了。人品爆发居然打到了200+,希望这样的狗屎运常来一些。
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <queue>
 5 #include <cstring>
 6 #include <string>
 7 #include <cstdlib>
 8 #include <cmath>
 9 #include <cctype>
10 #include <map>
11 #include <ctime>
12 using    namespace    std;
13 
14 int    main(void)
15 {
16     int    n,k;
17     int    max_land;
18 
19     cin >> n >> k;
20     
21     if(n % 2)
22         max_land = (n / 2 + 1 + n / 2) * (n / 2) + n / 2 + 1;
23     else
24         max_land = n * n / 2;
25     if(max_land < k)
26         puts("NO");
27     else
28     {
29         puts("YES");
30         if(n % 2 == 0)
31         {
32             int    flag = 0;
33             int    count = 0;
34             for(int i = 0;i < n;i ++)
35             {
36                 for(int j = 0;j < n;j ++)
37                     if(count < k && (j + flag) % 2== 0)
38                     {
39                         cout << 'L';
40                         count ++;
41                     }
42                     else
43                         cout << 'S';
44                 flag = !flag;
45                 cout << endl;
46             }
47         }
48         else
49         {
50             int    flag = 0;
51             int    count = 0;
52             for(int i = 0;i < n;i ++)
53             {
54                 for(int j = 0;j < n;j ++)
55                 {
56                     if(count < k && !flag && j % 2 == 0)
57                     {
58                         cout << 'L';
59                         count ++;
60                     }
61                     else    if(count < k && flag && j % 2)
62                     {
63                         cout << 'L';
64                         count ++;
65                     }
66                     else
67                         cout << 'S';
68                 }
69                 flag = flag == 1 ? 0 : 1;
70                 cout << endl;
71             }
72             
73         }
74     }
75 
76     return    0;
77 }

 

转载于:https://www.cnblogs.com/xz816111/p/4506926.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值