B. Sea and Islands
Time Limit: 1000msMemory Limit: 262144KB64-bit integer IO format: %I64d Java class name: (Any)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.
Sample Input
Input5 2OutputYES
SSSSS
LLLLL
SSSSS
LLLLL
SSSSSInput5 25OutputNO题意:L代表陆地,S代表海洋,L上下左右存在另一块L的话只能连通算一块,要求我们输出含有n块陆地的任意答案
#include <stdio.h> #include "string.h" int main() { int m,n,sum=0; scanf("%d%d",&m,&n); if(m%2==0) if(n>(m/2*m)) printf("NO\n"); else{ printf("YES\n"); for(int i=1;i<=m;i++) { for(int j=1;j<=m;j++) { if(sum==n) { printf("S"); continue; } if(j%2==1) if(i%2==1) { printf("L"); sum++; }else printf("S"); else if(i%2==1) printf("S"); else{ printf("L"); sum++; } } printf("\n"); } } else{ if(n>(m/2*(m-1)/2+(m/2+1)*((m+1)/2))) printf("NO\n"); else { printf("YES\n"); for(int i=1;i<=m;i++) { for(int j=1;j<=m;j++) { if(sum==n){ printf("S"); continue; } if(j%2==1) if(i%2==1){ printf("L"); sum++; } else printf("S"); else if(i%2==1) printf("S"); else { printf("L"); sum++; } } printf("\n"); } } } }Distributed under GPLv3. | Project Homepage | Developer: 51isoft crccw | Current Style: Cerulean.
Codeforces PROB training 31.MAY
最新推荐文章于 2024-07-24 13:02:49 发布