Codeforces#302-B-Sea and Islands-水题

http://codeforces.com/contest/544/problem/B

题意:

给你n,k;

在n*n的矩阵插入m个L,使得最后有k块 连在一起的L集合;

如果2个L在上下左右的边上相邻则算为连在一起 (对角线不算)

最大的块数就是

 if (n%2)
     big=n/2 * n + (n+1)/2;
    else
        big=n*n/2;

然后 间隔地插入就好了。。。插够了剩下的空位直接全部输出


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <set>
#include <vector>
using namespace std;

int main()
{
    
    int i,j;
    int n,m; 
    scanf("%d%d",&n,&m);
 int big;
    if (n%2)
     big=n/2 * n + (n+1)/2;
    else
        big=n*n/2;
    if ( m>big)
    {
        printf("NO\n");
        return 0;
    }
    printf("YES\n");
    int cun=0;
    j=0;
    for (i=1;i<=n;i++)
    {
        if (m==cun) break;
        if (i%2)
        {   
            for (j=1;j<=n;j++)
            {
                if (j%2) 
                {
                    printf("L");
                    cun++;
                    if (cun==m) break;
                }
                else
                    printf("S");
                
                
            }
            if (cun==m) break;
        }
        else
        {
                for (j=1;j<=n;j++)
            {
                if (j%2==0) 
                {
                    printf("L");
                    cun++;
                    if (cun==m) break;
                }
                else
                    printf("S");
                
                
            }
            if (cun==m) break;

        }
        printf("\n");
    }

        for (j++;j<=n;j++)
        {
            printf("S");
        }
        printf("\n");
    for (i++;i<=n;i++)
    {
        for (j=1;j<=n;j++)
        {
            printf("S");
        }
        printf("\n");

    }

    
    
    
    
    
    
    
    
    
    
    
    
    return 0;
    
}


你可能感兴趣的:(Codeforces#302-B-Sea and Islands-水题)