Codeforces 544B - Sea and Islands(构造)
2017-02-13 14:29
627 查看
B. Sea and Islands
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.
Examples
input
5 2
output
YES
SSSSS
LLLLL
SSSSS
LLLLL
SSSSS
input
5 25
output
NO
题意:
给出地图大小和连通块的个数,要求构造出这样的地图.
解题思路:
01交互放置,那么0就是连通块的个数,把0填上则连通块个数减一.
AC代码:
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.
Examples
input
5 2
output
YES
SSSSS
LLLLL
SSSSS
LLLLL
SSSSS
input
5 25
output
NO
题意:
给出地图大小和连通块的个数,要求构造出这样的地图.
解题思路:
01交互放置,那么0就是连通块的个数,把0填上则连通块个数减一.
AC代码:
#include <bits/stdc++.h> using namespace std; const int maxn = 1e2+5; bool a[maxn][maxn]; int main() { int n, k; cin >> n >> k; int cnt = 0; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) a[i][j] = (i + j) & 1, cnt += !a[i][j]; if(cnt < k) cout << "NO"; else { cout << "YES" << endl; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) if(cnt > k && !a[i][j]) a[i][j] = 1, cnt--; if(cnt == k) break; } for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) printf("%c",a[i][j] ? 'S':'L'); cout << endl; } } return 0; }
相关文章推荐
- codeforces-544B-Sea and Islands【构造】
- 构造 Codeforces Round #302 (Div. 2) B Sea and Islands
- Codeforces Round #302 (Div. 2) B. Sea and Islands 构造
- 544B. Sea and Islands
- CodeForces 459C-G - Pashmak and Buses-构造-K进制
- codeforces 459C - Pashmak and Buses 【构造题】
- codeforces 765 D Artsem and Saunders(构造)
- CodeForces 673D - Bear and Two Paths(构造)
- codeforces 743-C. Vladik and fractions(构造)
- codeforces 746 D. Green and Black Tea (构造)
- CodeForces 323 A.Black-and-White Cube(构造)
- CodeForces 313 C.Ilya and Matrix(构造)
- Codeforces 894C:Marco and GCD Sequence(构造)
- Codeforces 362D Fools and Foolproof Roads 构造题
- Codeforces#302-B-Sea and Islands-水题
- Codeforces 658 C. Bear and Forgotten Tree 3(树的构造)
- Codeforces 765D Artsem and Saunders 构造
- Codeforces 476D Dreamoon and Sets 规律+构造
- CodeForces 449 C.Jzzhu and Apples(构造+数论)
- Codeforces544B:Sea and Islands