time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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 test(s)
input
5 2
output
YES
SSSSS
LLLLL
SSSSS
LLLLL
SSSSS
input
5 25
output
NO
这道题的意思是,输入两个数,N和M,N指的是矩阵的大小,即N*N的矩阵,在这个矩阵内划分出M个小岛,如果两个有沙子的小岛在一起,横向或纵向连在一起就算是一个小岛,斜对角线的不算,所以相当于把这个矩阵当做是国际象棋的棋盘一样看,黑白间隔,不用像样例那样形成面积最大的小岛,只要数量够就行了,所以只要考虑行数和列数的奇偶性就可以了。
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char a[110][110];
int n,k;
while(cin>>n>>k)
{
if(n%2!=0&&k>(n*n/2+1)) cout<<"NO"<<endl;
else if(n%2==0&&k>n*n/2) cout<<"NO"<<endl;
else
{
int flag=0;
cout<<"YES"<<endl;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i%2!=0)
{
if(j%2==0) a[i][j]='S';
else if(j%2!=0&&flag<k)
{
a[i][j]='L';
flag++;
}
else a[i][j]='S';
}
else
{
if(j%2!=0) a[i][j]='S';
else if(j%2==0&&flag<k)
{
a[i][j]='L';
flag++;
}
else a[i][j]='S';
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
cout<<a[i][j];
cout<<endl;
}
}
}
return 0;
}