zoukankan      html  css  js  c++  java
  • Codeforces Round #302(Div. 2)——B—— 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.

    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
    大意:岛和海,问你布局,只要行相邻为1,列相邻为1,花式模拟
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int a[110][110];
    int j;
    int main()
    {
        int n,k;
        memset(a,0,sizeof(a));
        scanf("%d%d",&n,&k);
        int temp = 0;
        if(n%2 == 1){
            if( k > (1+n)*(1+n)/4 + (n-1)*(n-1)/4){
                printf("NO
    ");
                return 0;
            }
            else {
                printf("YES
    ");
              int i = 1;
                while(k > temp){
                  if(i%2 == 1)  
                     j = 1;
                  else  j = 2;
                  if(j == 1){
                      for(j = 1; j <= n ;j += 2){
                    a[i][j] = 1;
                    temp++;
                    if(temp == k)
                        break;
                      }
                      i++;
                  }
                  else {
                      for(j = 2; j <= n-1 ; j+=2){
                    a[i][j] = 1;
                    temp++;
                    if(temp == k)
                        break;
                      }
                      i++;
                   }
                }
            }
       }
        else {
            if(k > n*n/2){
            printf("NO
    ");
            return 0;
            }
            else {
            printf("YES
    ");
            int i = 1;
            while(k > temp){
                if(i%2 == 1)
                 j = 1;
                else j = 2;
                if(j == 1){
                    for(j = 1; j <= n - 1; j+=2){
                        a[i][j] = 1;
                        temp++;
                        if(temp == k) break;
                    }
                    i++;
                   }
                else {
                    for(j = 2; j <= n ; j+=2){
                        a[i][j] = 1;
                        temp++;
                        if(temp == k) break;
                    }
                    i++;
                }
            }
        }
        }
       
                for(int i = 1; i <= n ;i++){
                    for(int j = 1; j <= n ;j++){
                        if(a[i][j] == 0)
                        printf("S");
                        else if(a[i][j] == 1)
                        printf("L");
                    }
                    printf("
    ");
                }
                return 0;
            }
    

      

  • 相关阅读:
    《Effective Java 第三版》——第五章 泛型
    线性代数在数据科学中的十个强大应用(一)
    ​知识图谱里的知识存储:neo4j的介绍和使用
    一份从入门到精通NLP的完整指南 | NLPer
    NeurIPS审稿引发吐槽大会,落选者把荒唐意见怼了个遍:“我谢谢你们了”
    知识图谱与机器学习|KG入门 -- Part2 建立知识图谱
    知识图谱与机器学习 | KG入门 -- Part1-b 图深度学习
    ​知识图谱与机器学习 | KG入门 -- Part1 Data Fabric
    使用特定领域的文档构建知识图谱 | 教程
    ICCV 2019|70 篇论文抢先读,含目标检测/自动驾驶/GCN/等(提供PDF下载)
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4488474.html
Copyright © 2011-2022 走看看