CF 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.
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.
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.
5 2
YES
SSSSS
LLLLL
SSSSS
LLLLL
SSSSS
5 25
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 }